7

I have a backend application (Java 8) and an Angular frontend. Now, I added a file upload using multipart/formdata. We use openapi to generate the api code. Here's the part of the code used to generate it:

"post": {
   ...
   "consumes" : [
      "multipart/form-data"
   ],
   "produces": [
      "application/json"
   ],
   "parameters": [
      {
         "name": "updateFile",
         "in": "formData",
         "type": "file",
         "required": true,
      }
   ],
   ...
}

And the frontend prepares the file as such:

  const formData = new FormData();
  formData.append('updateFile', updateFile);

This actually works quite well and provides us a Part-Object. Unfortunately the machine this code will run on in production is quite weak and the uploaded file will be too large to be handled in the memory of it (~130mb). So, I need a workaround for this. I think accessing the incoming stream would be an elegant way to write the file to some temp directory "on-the-fly" and I've come across code samples doing exactly that. I can't figure out how to configure my endpoint to provide a stream instead of the "finished" Part-Object.

Unfortunately this project uses a quite old openapi version 2.

fhueser
  • 599
  • 3
  • 16

1 Answers1

1

The workaround I've found now is taking the ServletInpuStream directly from the HttpServletRequest and leaving the body in the API description empty to prevent the stream from being consumed before I actively do so. This of course forces me to parse the content myself. I found MultipartStream of apache.commons to be very helpful for this.

This solution feels a bit hacky but is at least viable until we updated to a more recent version that directly supports something like that.

fhueser
  • 599
  • 3
  • 16