2013-05-05 51 views
-1

我想上傳&重命名一個java程序中的文件。Java文件上傳和用php重命名

我的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 
    ) 
) 

回答

0

在你的PHP文件,只需鍵入:

print_r($_FILES); 

,看看您是否接收 「uniekeNaam」 變量或不。

+0

陣列 ( [文件] =>數組 ( [名稱] => motoplus.pdf [型] =>應用/八位字節流 [tmp_name的值] =>的/ tmp/php0V4SCS [錯誤] = > 0 [size = 5] 1010671 ) )沒有收到它 – Joachim 2013-05-05 12:41:35

+0

以下答案有您需要的解決方案。 http://stackoverflow.com/questions/8965022/android-upload-file-with-filling-out-post-body-together – 2013-05-05 20:22:14

+0

謝謝!它現在有效 – Joachim 2013-05-08 12:35:52