Created
May 26, 2020 11:57
-
-
Save mengjiann/a9140445d41cc05c5b71cb9e8e62028c to your computer and use it in GitHub Desktop.
Upload large file using Apache HttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://hc.apache.org/httpcomponents-client-ga/