我试图用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 /'文件夹。你能解释一下你认为应该是什么文件夹吗?它存在吗? –