简体   繁体   English

如何处理服务器上的文件上传块(Plupload / Spring MVC)?

[英]How to handle file upload chunks on the server (Plupload/Spring MVC)?

On our client, it is possible to be uploading large files. 在我们的客户端上,可以上传大文件。 We want to use chunking to reduce the size of the requests. 我们希望使用分块来减少请求的大小。 We are using Plupload , so it's easy to send up the files in chunks. 我们正在使用Plupload ,所以很容易以块的形式发送文件。 However, I'm not sure how to handle the chunks. 但是,我不知道如何处理这些块。 We are using Spring MVC on the server, and I current have the following for my controller method: uploadMedia(@RequestBody MultipartFile file) where MultipartFile is org.springframework.web.multipart.MultipartFile . 我们在服务器上使用Spring MVC,目前我的控制器方法有以下内容: uploadMedia(@RequestBody MultipartFile file) ,其中MultipartFileorg.springframework.web.multipart.MultipartFile This works just fine when not doing chunking. 这样可以在不进行分块时正常工作。 When I turn on the chunking on the client side, it still gets into this method just fine, but I don't see anything on the file that gets passed in that identifies what file the chunk is a part of. 当我打开客户端的分块时,它仍然可以很好地进入这个方法,但是我没有看到传递的文件中的任何内容,它标识了chunk所属的文件。 Hopefully I'm just missing something. 希望我只是遗漏了一些东西。

It seems like this is a common workflow, but I can't seem to find any good examples of how this is done on the server side. 看起来这是一个常见的工作流程,但我似乎无法找到在服务器端如何完成这一任务的任何好例子。 A solution with just Spring would be great, but if another library is needed, that is fine too. 只有Spring的解决方案会很棒,但是如果需要另一个库,那也没关系。 I looked some at Apache Commons FileUpload , but I couldn't find anything about chunking on there. 我看了一下Apache Commons FileUpload ,但我在那里找不到任何有关分块的信息。 Any help with this would be great. 对此的任何帮助都会很棒。 Thanks. 谢谢。

I debugged through this some more and added a HttpServletRequest to my controller method to see if there was anything available in there. 我通过这个调试了一些,并在我的控制器方法中添加了一个HttpServletRequest ,以查看是否有任何可用的东西。 I found that the request I received was a org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest which has a field called multipartParameters which is just a HashMap . 我发现我收到的请求是org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest ,它有一个名为multipartParameters的字段,它只是一个HashMap I found that this map had keys of name , chunks , and chunk . 我发现这张地图上有namechunkschunk键。 So, I decided to try the following as my signature: 所以,我决定尝试以下作为我的签名:

uploadMedia(@RequestBody MultipartFile file, @RequestParam String name, @RequestParam int chunks, @RequestParam int chunk)

Sure enough, those @RequestParam parameters got populated with the name of the file, the number of chunks, and the chunk number (zero-based) respectively. 果然,这些@RequestParam参数分别填充了文件名,块数和块号(从零开始)。 Having this information with each request will make it pretty easy to assemble the chunks into the final file. 在每个请求中包含此信息将使得将块组装到最终文件中变得非常容易。

One thing to know also, is that if the file does not need to be chunked (the file size is less than the chunk size), the chunks and chunk parameters are not sent up. 还有一点需要注意的是,如果文件不需要分块(文件大小小于块大小),则不会发送chunkschunk参数。 So, my final signature ended up looking like this: 所以,我的最终签名最终看起来像这样:

uploadMedia(@RequestBody MultipartFile file, @RequestParam String name, @RequestParam(required=false, defaultValue="-1") int chunks, @RequestParam(required=false, defaultValue="-1") int chunk)

Then I can check for -1 to see if I need to worry about chunking at all. 然后我可以检查-1,看看我是否需要担心分块。

So, in the controller method itself I have something like this: 所以,在控制器方法本身我有这样的事情:

Media media = new Media();
//set stuff on my Media object for storing info about the file in the DB
//....
if (chunks > 0 && chunk > 0)
{
    //Need to append the bytes in this chunk
    mediaRepository.appendBytes(media, file.getBytes());
    if (chunk == chunks - 1)
    {
        //last chunk, upload is done
        onUploadFinished(media);
    }
}
else
{
    //Write out the first set of bytes
    mediaRepository.saveBytes(media, file.getBytes());
    if (chunks <= 0)
    {
        //no chunks were needed, all the bytes have been written out, upload is done
        onUploadFinished(media);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM