Skip to content

Instantly share code, notes, and snippets.

@oksuz
Last active June 28, 2016 19:56
Show Gist options
  • Save oksuz/3bfe382092a2ebac57ec44e30e88d5f0 to your computer and use it in GitHub Desktop.
Save oksuz/3bfe382092a2ebac57ec44e30e88d5f0 to your computer and use it in GitHub Desktop.
It can't upload file to apache2.2/php-fcgi, but it works with nginx/php5-fpm well
public Response fileUpload(URL url, String fieldName, String fileName, InputStream content, List<BasicNameValuePair> params, List<Header> headers) throws IOException {
HttpClient client = getHttpClient();
HttpPost post = new HttpPost(url.toString());
if (null != headers) {
for (Header h : headers) {
post.addHeader(h);
}
}
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if (null != params) {
for (NameValuePair pair : params) {
entityBuilder.addPart(pair.getName(), new StringBody(pair.getValue(), ContentType.TEXT_PLAIN));
}
}
entityBuilder.addBinaryBody(fieldName, content, ContentType.DEFAULT_BINARY, fileName);
post.setEntity(entityBuilder.build());
if (null != context) {
return new Response(client.execute(post, context));
}
return new Response(client.execute(post));
}
@oksuz
Copy link
Author

oksuz commented Jun 28, 2016

I replaced

InputStream content  // argument to
byte[] content // applicable to entityBuilder.addBinaryBody

It worked fuuu!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment