我已经设置了一个 Apache 服务器,运行一个 php 脚本来处理各种 post 请求。脚本如下所示:
$uploaddir = realpath('mypath');
//realpath('./') . '/';
$uploadfile = $uploaddir . '/' . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";
这效果很好,它处理请求并将我的文件移动到我想要的位置。
但是当我使用以下 java 客户端尝试此操作时,我什么也没得到:
String URL = "http://localhost/accept.php";
File file = new File("C:/Ozer/ANPRProject/MDT/Export/test.txt");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try{
System.out.println(""+file.exists());
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file),-1);
reqEntity.setContentType("application/octet-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
System.out.println("execute 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());
}
EntityUtils.consume(resEntity);
} catch(Exception e) {
} finally {
httpclient.getConnectionManager().shutdown();
}
}
我得到的系统输出是:
true
execute request POST http://localhost/accept.php HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Response content length: 330
Chunked: false
我觉得这很奇怪,因为 chunked 尚未设置为 true?虽然我在代码中特别要求了它。但无论如何,上传都不起作用。我已将 contentType 设置为“application/octet-stream”,因为我用来请求帖子的 php 脚本给了我反馈:[type] => application/octet-stream
所以apache的日志显示连接成功,他确实收到了请求:
127.0.0.1 - - [08/Aug/2013:16:10:04 +0200] "POST /accept.php HTTP/1.1" 200 330 "-" ""Apache-HttpClient/4.2.5 (java 1.5)"
这是我使用 Php 脚本请求帖子时得到的结果:
127.0.0.1 - - [08/Aug/2013:15:54:23 +0200] "POST /accept.php HTTP/1.1" 200 419 "-" "-" ::1 - - [08/Aug/2013:15:54:23 +0200] "GET /testSendDat.php HTTP/1.1" 200 3330 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
来自 php 脚本的错误日志显示:
[08-Aug-2013 16:10:04 Europe/Berlin] PHP Notice: Undefined index: file_contents
所以我知道这意味着什么,我试图从中读取文件的数组是空的。但为什么?我没有任何线索。我猜我使用了错误的方式来发布请求,但现在已经寻找了一段时间,但还没有真正成功地找到问题所在。非常感谢您的帮助。
哦,设置 apache tomcat 服务器并使用 httpservlet 可能会更容易吗?或者这并不重要?
最佳答案
您不会创建模拟包含文件上传元素的已填写表单的 HTTP 请求。您使用该文件作为请求正文创建一个 HTTP POST 请求。
填写好的表单 POST HTTP 请求的正文如下所示:
user=Fritz&age=12
它看起来很像 GET HTTP 请求的 URL 参数。
包含文件上传的填写表单 POST 是具有多个正文的多部分 HTTP 请求。其中一个包含表单字段,另一个包含上传文件内容。
参见:how-does-http-file-upload-work
我无法提供示例来设置您的请求,但此信息可能会对您有所帮助。
关于java - Jakarta commons HTTPClient 无法使用 php 在我的 apache 服务器上发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128388/