【发布时间】:2011-04-19 02:57:02
【问题描述】:
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 -------------------------------------- HTTP/1.1 200 正常 响应内容长度:241 分块?:假 响应:上传:
类型:
大小:0 Kb
存储在:数组 ( [bin] => 数组 ( [名称] => File.tar.lzma [类型] => [tmp_name] => [错误] => 1 [大小] => 0 ) )
我已经通过正常的 http 上传(通过网络浏览器,相同的文件)对其进行了测试,它可以正常工作。
我做错了什么?
编辑:f3 是一个文件(我知道存在,(/home/me/Desktop/File.tar.lzma))
【问题讨论】:
-
我不知道你的 Java 出了什么问题,但我确定不是你的 PHP。
-
你能告诉我们更多关于
f3的信息吗? -
编辑:f3 是一个文件(我知道存在,(/home/me/Desktop/File.tar.lzma))
-
在这种情况下,如果你对http协议足够了解,安装wireshark并读取数据传输。你会发现 http post 命令缺少了一些东西。对于这种情况,wireshark 是最好的调试工具。
-
我不太懂http,但我一定会试试这个。
标签: java php file-upload