我試圖用java在我的php服務器上傳文件,但是我的httprequest每次運行代碼失敗,我不知道它的原因是什麼,首先我認爲它可能由multipartEnttity方法,其參數我離開前面空的,但現在它填充後同樣的問題仍然ocurrs引起....通過php在java中上傳文件
//this is my java code
String path="E:\\upload.txt";
HttpClient hc=new DefaultHttpClient();
hc.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost hp=new HttpPost("http://localhost/shoolPHP/uploadFile.php");
File f=new File(path);
MultipartEntity me=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentBody cf=new FileBody(f);
me.addPart("userfile", cf);
hp.setEntity(me);
System.out.println("executing request" +hp.getRequestLine());
HttpResponse hr=hc.execute(hp);
HttpEntity he=hr.getEntity();
if(!(hr.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){
System.out.println("Uploaded");
}
else{
System.out.println("Failed");
}
System.out.println(hr.getStatusLine());
if (he != null) {
System.out.println(EntityUtils.toString(he));
}
if (he != null) {
he.consumeContent();
}
hc.getConnectionManager().shutdown();
//這被校正PHP代碼
<?php
$uploads_dir='/Home';
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
$dest=$_FILES['userfile']['name'];
echo "File" .$_FILES['userfile']['name'] ."uploaded file successfully to
$uploads_dir/$dest";
move_uploaded_file ($_FILES['userfile'] ['tmp_name'], "$uploads_dir/$dest");
} else {
echo "Possible file upload attack: ";
echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
print_r($_FILES);
}
?>
//這些是新的警告
執行已requestPOST http://localhost/shoolPHP/uploadFile.php HTTP/1.1 上傳 HTTP/1.1 200 OK Fileupload.txtuploaded成功提交到 /Home/upload.txt
警告:move_uploaded_file(/Home/upload.txt):未能打開流:在C無這樣的文件或目錄:\ XAMPP \ htdocs中\上線
警告 shoolPHP \ uploadFile.php:move_uploaded_file()以:無法移動「C:\ XAMPP \ TMP \ phpB374。 tmp'轉換爲'/Home/upload.txt'C:\ xampp \ htdoc小號\ shoolPHP \ uploadFile.php上線
你看過錯誤信息並看過它們所指的行嗎?這是基本的語法檢查。 –
糾正了'文件'錯過咒語,但仍然存在問題 –
嗯,你試圖訪問Windows機器上的'/ Home /'文件夾。你能解釋一下你認爲應該是什麼文件夾嗎?它存在嗎? –