How to implement HTTP Post chunked upload of a big file using java httpclient?

我只是一个虾纸丫 提交于 2019-12-20 06:28:04

问题


I have an enormous file to upload and server on other side does support chunked upload. Is there any example of how exactly to do that? Or there is some other library which do that?


回答1:


Using HttpClient 4 (From Apache)

HttpPost post = new HttpPost(url);
MultipartEntity content = new MultipartEntity(HttpMultipartMode.STRICT);

//To add parameters, use StringBody
content.addPart("param", new StringBody("value"));
content.addPart("param1", new StringBody("value1"));
content.addPart("param2", new StringBody("value2"));

//To add files, use InputStreamBody
content.addPart("source", new InputStreamBody(inputStream, mimeType, fileName)); //OR
content.addPart("source", new InputStreamBody(inputStream, mimeType));

//Finally
post.setEntity(content);

Hope this helps.




回答2:


Using HttpURLConnection, just set chunked transfer mode.



来源:https://stackoverflow.com/questions/5222784/how-to-implement-http-post-chunked-upload-of-a-big-file-using-java-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!