Java代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://localhost/SC/upload.php");
FileBody bin = new FileBody(f3);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
httppost.setEntity(reqEntity);
System.out
.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: "
+ resEntity.getContentLength());
System.out.println("Chunked?: " + resEntity.isChunked());
System.out.println("Response: "
+ EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
PHP代码:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
print_r ($_FILES);
?>
每次运行Java代码,我总是得到:
正在执行请求post http://localhost/sc/upload.php http/1.1
——————————————————————————————————————————————————————————————————————————————————————————————————————————--
响应内容长度:241
分块的?假
回复:上传:
Type:
尺寸:0公斤
存储在:数组
(
[bin]=>数组
(
[名称]=>file.tar.lzma
[类型] = & gt;
[tmp_name]=>
[错误]=& 1;
[尺寸]=0;
)
)
我已经用普通的HTTP上传(通过Webbrowser,相同的文件)测试过了它,它工作正常。
我做错什么了?
编辑:f3是一个文件(我知道存在),(/home/me/desktop/file.tar.lzma)