fileupload using Chunks and MultipartFileupload

2020-07-27 04:16发布

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.

标签: java spring
1条回答
够拽才男人
2楼-- · 2020-07-27 04:31

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

查看更多
登录 后发表回答