Artur K.
Artur K.

Reputation: 649

Jetty file upload via multipart request directly farward file to FileInputStream

I am currently implementing a file upload service in java using jetty as my servelet container. I am facing an issue I want to get fixed.
I have an endpoint for the file upload which is a post endpoint which takes multipart form data.
This works fine for small files but gives me a headache when a user uploads a big file. If I am not wrong then Jetty Buffers the uploaded file before forwarding it to my FileInputStream.
By this I mean that it fills its internal buffer first and then writes it to the FileInputStream. Is there a way to stop this and tweak jetty that it directly relays the data to the FileInputStream before buffering it?
I already tried to wrap the input stream into a buffered one but still, Jetty first consumes the file, buffers it and then writes it to the InputStream.
After some research, I saw a comment suggesting to use put instead and then access the raw data to achieve this direct forwarding. But I was wondering if there is another maybe even better way.

Regards Artur

Upvotes: 0

Views: 936

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49495

When working with large files on multipart/form-data make sure you are using the most recent RFC7578 as it's the most performant.

Server server = new Server();

HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setMultiPartFormDataCompliance(MultiPartFormDataCompliance.RFC7578);
ServerConnector connector = new ServerConnector(server, 
    new HttpConnectionFactory(httpConfig));
server.addConnector(connector)

Next, when you are using the HttpServletRequest.getPart(String) method, you should know that Jetty internally has to extract the contents of the multipart/form-data to be accessible by both the .getPart() and .getParameter() methods.

Accessing large files this way is actually a unit test in Jetty itself.

See: HugeResourceTest.java

Upvotes: 0

Related Questions