3

Is Upload using chunks and MultipartFile upload same.What makes the difference? I have tried upload using Multipartfile. My requirement is to upload a file of 50 mb appx to server without making the user to wait for long time. it is taking roughly about 2 mins.

rolling stone
  • 543
  • 1
  • 8
  • 26

1 Answers1

4

Chunks and Multipart do the same in a different way

Chunks make the upload "splitting" the whole file in little pieces (chunks)

This allows to split very large file (also 1GB) in smaller pieces and send them to the server without having the connection timeout issue in a browser; then it's up to the server side processing the chunks

You can use several JS upload libraries for the chunks (e.g. https://blueimp.github.io/jQuery-File-Upload/ or http://www.plupload.com/ and so on)

Multipart, instead, uploads the file in one shot; if the file is 1MB all the 1MB is uploaded, if it's 1GB all the 1GB is loaded

Regarding to the transfer velocity i guess it's something related to the bandwidth and internet connection

I hope this can help you

SERVER SIDE CHUNKS MANAGEMENT SAMPLE

As I told you it's up to the server side to manage chunks; I would strongly avoid to store in memory chunks this because you should allocate a huge amount of memory (if the file is 1GB you should allocate 1GB and this for each upload)

In my old project I used plupload and this is an extract of how i managed the chunks on server side (note: code done by using Spring)

RequestMapping(method = { RequestMethod.POST }, value = { "/plupload" })
public ResponseEntity<List<UploadMediaResultDto>> uploadMedia(
                @RequestParam(required = true, value = "file") MultipartFile file, 
                @RequestParam(required = false, value = "name") String name, 
                @RequestParam(required = false, value = "chunks") Integer chunks, 
                @RequestParam(required = false, value = "chunk") Integer chunk)
{
boolean fullFile = false;
File theFile = new File(filePath);
if (chunk != null && chunks != null && (chunk == 0 && chunks == 1))
{
    fullFile = true;
}
if (!fullFile)
{
FileUtils.writeByteArrayToFile(theFile, file.getBytes(), append);
}
else
{
FileUtils.writeByteArrayToFile(theFile, file.getBytes());
}
}

As you can see first I check if I receive the whole file or not; if not I write the file in append mode otherwise i write it in total

I hope this is useful

Angelo

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65
  • Thanks for the clarification.How it is being save in the server for chunks. Should there be a persistance for each chunk ?or it can be saved after all the chunks are being transfered ? – rolling stone Oct 28 '16 at 08:47
  • I updated the answer; I hope that little sample code is useful to you – Angelo Immediata Oct 28 '16 at 13:26
  • yeah thats great. thank you angelo. My intention is to send it as an attachment. not going to store it anywhere. Can you please suggest in that case how to achieve it ? – rolling stone Oct 28 '16 at 14:54
  • 1
    Well I guess you maybe can use a `javax.activation.DataHandler` by passing to the constructor a `javax.activation.URLDataSource` but in this case i don't know how you can handle the chunks; other way would be to create a temp file, fill it with input stream and then send the file as attachment and then delete it – Angelo Immediata Oct 29 '16 at 07:39