Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mengjiann/a9140445d41cc05c5b71cb9e8e62028c to your computer and use it in GitHub Desktop.
Save mengjiann/a9140445d41cc05c5b71cb9e8e62028c to your computer and use it in GitHub Desktop.
Upload large file using Apache HttpClient
HttpClientContext clientContext = HttpClientContext.create();
// clientContext.setCredentialsProvider(credentialsProvider);
// clientContext.setAuthCache(authCache);
HttpPost post = new HttpPost(endpointUri);
HttpEntity entity = MultipartEntityBuilder.create()
.addBinaryBody("file", inputStream, ContentType.create("application/octet-stream"), fileName)
.build();
post.setEntity(entity);
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()){
HttpResponse response = httpClient.execute(post,clientContext);
int httpStatus = response.getStatusLine().getStatusCode();
if (httpStatus != HttpStatus.SC_OK) {
// Handle failed request
}
}
@mengjiann
Copy link
Author

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