0

My application requires images, that I store on somthing like Amazon S3, until now I was getting images by retrieving URL from the client and downloading the file. However that solution is highly dependent on clients infrastructure, so the idea now is for client to send bytes to my API. I’m writing the code in Spring Boot and it seems like the best way is to use MultipartFile class in Controller as a wrapper for the data, at least that’s the only thing that internet is saying, however I can’t figure out why not use byte[], wouldn’t it be quicker if I don’t need anything else, just bytes that create the image?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 3
    If the file in question is, say, 2GB, you're going to run into `OutOfMemoryError`, or even if you don't, your system is _very_ constrained, needing __2GB__ for every concurrently running upload process. There are a few more reasons but that's one of the significant ones. – rzwitserloot Aug 16 '23 at 23:13
  • 1
    You can use `byte[]`, but it could be less convenient: 1) you probably have to pass a filename. How would you wrap it together? 2) It could be more difficult to implement on a client side – gdomo Aug 16 '23 at 23:16
  • More info: [How does HTTP file upload work?](https://stackoverflow.com/questions/8659808/how-does-http-file-upload-work) – Valijon Aug 16 '23 at 23:27
  • That is how browsers implement multipart file upload using HTML forms. If you are just creating your own REST interface to transfer single files, I don't see any problems in just sending the entire file as the payload in a normal file transfer. I would recommend using `Input/OutputStream` over `byte[]` though, as that gives you more flexibility and less memory constraints (ie you can stream directly to/from disk, with a minimal byte buffer for better performance). – Harald K Aug 18 '23 at 08:21

0 Answers0