【问题标题】:Java file upload & rename with phpJava文件上传和用php重命名
【发布时间】:2013-04-29 08:33:29
【问题描述】:

我正在尝试从 java 程序上传和重命名文件。

我的java上传方法:

public void bestandUploaden(String oudeUrl, String nieuweNaam) throws ParseException, IOException {
System.out.println("oude url: " + oudeUrl);
System.out.println("nieuwe naam: " + nieuweNaam); 
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("**Website url**/upload_file.php");
File file = new File(oudeUrl);

MultipartEntity mpEntity = new MultipartEntity();
mpEntity.addPart("file", new FileBody(file));
mpEntity.addPart("uniekeNaam", new StringBody(nieuweNaam));

httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());
if (resEntity != null) {
  System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
  resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();
}

我的php代码:

<?php
echo "Nieuwe bestandsnaam :" . $_FILES["uniekeNaam"];
$disallowedExts = array("exe");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (!in_array($extension, $disallowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("documenten/" . $_FILES["uniekeNaam"]))
      {
      echo $_FILES["uniekeNaam"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "documenten/" . $_FILES["uniekeNaam"]);
      echo "Stored in: " . "documenten/" . $_FILES["uniekeNaam"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

我的 java 控制台输出:

oude url: D:\Dropbox\Documents\youtube.txt
nieuwe naam: Een_textbestand.txt
executing request POST **Website url**/upload_file.php HTTP/1.1
HTTP/1.1 200 OK
Nieuwe bestandsnaam :Upload: youtube.txt<br>Type: application/octet-stream<br>Size: 1.8095703125 kB<br>Temp file: /tmp/phpp9igba<br> already exists. 

如您所见,“uniekeNaam”并未从我的 java 转移到我的 php 部分。 它是空的。我没有任何 php 知识并尝试了不同的东西,但似乎没有任何效果。我只想能够浏览到我的 java 程序中的文件并将该文件上传到网络服务器并重命名它。 如果我不进行重命名,它就可以工作,所以我想我只需要弄清楚如何将“uniekeNaam”从 java 转移到 php。

如果我这样做:

print_r($_FILES);

我明白了:

Array ( 
   [file] => Array ( 
      [name] => motoplus.pdf 
      [type] => application/octet-stream 
      [tmp_name] => /tmp/php0V4SCS 
      [error] => 0 [size] => 1010671 
   ) 
)

【问题讨论】:

    标签: java php uploading renaming


    【解决方案1】:

    在你的 php 文件中输入:

    print_r($_FILES);
    

    查看您是否收到“uniekeNaam”变量。

    【讨论】:

    • Array ( [file] => Array ( [name] => motoplus.pdf [type] => application/octet-stream [tmp_name] => /tmp/php0V4SCS [error] => 0 [size] => 1010671 ) ) 没有收到
    • 以下答案有您需要的解决方案。 stackoverflow.com/questions/8965022/…
    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 2016-02-28
    • 2012-05-04
    • 1970-01-01
    • 2020-05-19
    • 2019-06-10
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多