我正在尝试使用 HttpPost 和 MultipartEntityBuilder 的文件上传 API。以下是我使用过的代码。
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(MIME.UTF8_CHARSET);
builder.addBinaryBody(<fileFieldName>, <byteArray>, ContentType.TEXT_PLAIN, <fileName>);
Run Code Online (Sandbox Code Playgroud)
文件已正确上传。但是,当文件名包含非 ASCII 字符时,它会以名称“????.jpg”上传。尝试了此处给出的解决方案/sf/answers/1810921101/。但这并没有解决我的问题。请协助。
下面是我编写的用于将二进制文件沿某些字符串发送到PHP Web应用程序服务器的代码.
public void doRegister(final String userEmail, final String userPass, final File userPhotoId) {
HttpClient myHttpClient = new DefaultHttpClient();
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpPost registerTry = new HttpPost(Constants.oAuthRegURL);
MultipartEntity signupEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
signupEntity.addPart("email", new StringBody(userEmail, Charset.forName("UTF-8")));
signupEntity.addPart("password", new StringBody(userPass, Charset.forName("UTF-8")));
signupEntity.addPart("User[profilePicture]", new FileBody(userPhotoId, "image/jpeg"));
registerTry.setEntity(signupEntity);
HttpResponse signupHttpResp = myHttpClient.execute(registerTry);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
文件 …
我确实使用MultipartEntity将File发送到服务器,它在$_FILES超全局中正确显示
但我还需要填写POST正文以供阅读 php://stdin
我怎样才能做到这一点?
下面的当前片段:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // stream to hold image
bm.compress(CompressFormat.JPEG, 75, bos); //compress image
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("REMOTE ADDRESS");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE); // is this one causing trouble?
reqEntity.addPart("image", bab); // added image to request
// tried this with no luck
// reqEntity.addPart("", new StringBody("RAW DATA HERE"));
postRequest.setEntity(reqEntity); // set the multipart entity …Run Code Online (Sandbox Code Playgroud) 我想使用将文件(特定于图像)上传到REST服务器HTTP POST.我已经导入/添加到构建路径httpmime-4.3.1.jar和apache-mime4j-0.6.jar.我在堆栈跟踪中得到以下错误.
这有效吗? post.setHeader("enctype", "multipart/form-data");
HTTP POST代码
public void multiPartPost() throws ClientProtocolException, IOException {
File image = new File(EXTERNALSTORAGE + "/Pictures/sample.jpg");
FileBody fileBody = new FileBody(image);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("enctype", "multipart/form-data");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("sampleImage", fileBody);
post.setEntity(multipartEntity.build());
HttpResponse response = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
Log.v("multiPartPost HTTP Response", responseBody);
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
01-05 17:41:25.470: W/dalvikvm(6564): VFY: unable to resolve static field 1755 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
01-05 …Run Code Online (Sandbox Code Playgroud)