Java 使用apache';HTTP客户端库不工作
此问题与非常类似,但即使是Java 使用apache';HTTP客户端库不工作,java,php,Java,Php,此问题与非常类似,但即使是MultipartEntity也无法正确上载文件。以下是客户端的MWE: import java.io.*; import java.util.*; import org.apache.http.entity.mime.*; import org.apache.http.client.*; import org.apache.http.message.*; import org.apache.http.*; import org.apache.http.client.
MultipartEntity
也无法正确上载文件。以下是客户端的MWE:
import java.io.*;
import java.util.*;
import org.apache.http.entity.mime.*;
import org.apache.http.client.*;
import org.apache.http.message.*;
import org.apache.http.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.*;
import org.apache.http.impl.client.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
// Emulate the post behavior of curl in java except post a string.
// https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily
public class Foo{
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
// TODO: Fix and test this method.
private static void PostData() throws Exception {
String url = "http://localhost/index.php";
DefaultHttpClient httpclient = new DefaultHttpClient();
// create the post request.
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
ContentBody body = new FileBody(new File("/tmp/HelloWorld"),
org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM);
entity.addPart("file", body);
httppost.setEntity(entity);
// execute request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStream stream = resEntity.getContent();
System.out.println(response.getStatusLine());
System.out.println(convertStreamToString(stream));
}
public static void main(String[] args) throws Exception {
PostData();
}
}
/tmp/HelloWorld'的内容是
HelloWorld`
下面是index.php
的外观:
<?php
echo empty($_FILES);
print_r($_REQUEST);
print_r($_POST);
print_r($_GET);
print_r($_FILES);
>?
我的猜测是我在客户端代码中做了一些愚蠢的事情,但我不确定它是什么。我深入研究了PHP源代码,显然,
内容配置
行上需要一个文件名
。因此,在客户机中添加以下代码from可以解决问题
FormBodyPart customBodyPart = new FormBodyPart("file", body) {
@Override
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
buffer.append("; filename=\"-\"");
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
};
entity.addPart(customBodyPart);
FormBodyPart customBodyPart = new FormBodyPart("file", body) {
@Override
protected void generateContentDisp(final ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
buffer.append("; filename=\"-\"");
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
};
entity.addPart(customBodyPart);