Sending multiple files to a servlet with a single connection

天大地大妈咪最大 提交于 2021-02-20 13:31:40

问题


I'm writing a Java desktop client which will send multiple files over the wire to a servlet using a post request. In the servlet I'm getting the input stream from the request to receive the files. The servlet will write the files to disk, one by one as they're read from the stream.

The implementation has a couple of requirements:

  • Only one HTTP request must be used to the server (so only a single stream)
  • The servlet must use a reasonable fixed amount of memory, no matter what the size of the files.

I had considered inserting markers into the stream so I know when one file ends and the next one begins. I'd then write some code to parse the stream in the servlet, and start writing the next file as appropriate.

Here's the thing... surely there's a library to do that. I've looked through apache commons and found nothing. Commons File Upload is interesting but since the upload comes from a Java app, not a browser it only solves the receiving end, not the sending.

Any ideas for a library which easily allows multiple file transfers across a single stream with fixed memory expectations even for very large files?

Thanks.


回答1:


Just use HTTP multipart/form-data encoding on the POST request body. It's described in RFC-2388 and a standard way of uploading (multiple) files by HTTP.

You can do it with just java.net.URLConnection as described in this mini-tutorial, although it would generate lot of boilerplate code. A more convenienced approach would be using Apache Commons HttpClient.

In the servlet side you can then just use Apache Commons Fileupload to process the uploaded files the usual HTTP way (or when you're already on Servlet 3.0, the HttpServletRequest#getParts(), see also this answer for examples).



来源:https://stackoverflow.com/questions/4495229/sending-multiple-files-to-a-servlet-with-a-single-connection

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