简体   繁体   English

如何在Spring Web应用程序中实现文件夹上传?

[英]How to implement folder upload in a Spring web app?

I'd like to implement in my web application a file/directory upload similar to Google Drive style (I think it's the best example to explain what I want). 我想在我的网络应用程序中实现类似于Google云端硬盘样式的文件/目录上传(我认为这是解释我想要的最佳示例)。

So I would like to upload: 所以我想上传:

  • a single file 一个文件
  • multiple selected files 多个选定文件
  • a selected folder (all files contained in it) 选定的文件夹(其中包含的所有文件)

On client side I suppose I have to use HTML5, am I wrong? 在客户端,我想我必须使用HTML5,对吗? But How to handle this on server side controller. 但是如何在服务器端控制器上处理此问题。 I'm using Spring MVC 3.2.9 我正在使用Spring MVC 3.2.9

Can you suggest me the best approach? 您能建议我最好的方法吗?

The hard part is the client side upload of folders. 困难的部分是客户端上载文件夹。 According to this other answer on SO about Does HTML5 allow drag-drop upload of folders or a folder tree? 根据关于SO的这个其他答案, HTML5是否允许拖放上传文件夹或文件夹树? , The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively . HTML5规范并未说明选择上传文件夹时,浏览器应递归上传所有包含的文件

Of course it is possible, but HTML5 is not enough and you will have to use Javascript to (recursively) find all files in the folder. 当然可以,但是HTML5不够用,您将不得不使用Javascript(递归)找到文件夹中的所有文件。

As said by conFusl, you can find a nice example on viralpatel.net Spring MVC Multiple File Upload tutorial. 正如conFusl所说,您可以在viralpatel.net Spring MVC Multiple File Upload教程上找到一个不错的示例 Spring Multiple File upload example . Spring多文件上传示例 The princips are : 主要原理是:

  • on client side generate (via javascript) a form with one <input> tag per file to upload, and give them names like files[i] 在客户端生成(通过javascript)一个表单,每个文件带有一个<input>标记以上传,并给它们起类似files[i]名称files[i]
  • on server side, you then get a form containing a List<MultipartFile> that you can process as usual. 在服务器端,您将获得一个包含List<MultipartFile>的表单,可以像往常一样处理该表单。

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

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