Web page to achieve large file uploads and downloads

In the Web application development, file upload and download capabilities are very commonly used functions, today speaking about JavaWeb the file upload and download function to achieve.

Let me talk about the requirements under:

PC end of the whole platform support required to support Windows, Mac, Linux

Support all browsers.

Support Bulk upload file

Support the upload folder, and required to retain a hierarchical structure on the server. The number of folders required to support 10W.

Support for large files HTTP request to refresh your browser, restart the browser, restart the computer and still be able to continue to upload. File size requires the ability to support up to 50 G.

It supports automatic load a local file, requires the ability to automatically load the specified local file.

Support batch download files, asked not to pack the server. Because 50G file server packed in a long time.

Support folder download, asked not packaged in the server, downloaded to the local hierarchy after the required retention

File List panel supports navigation path, create a new folder

 

  For file uploads, the browser is in the process of uploading the files submitted in a stream to the server if the direct use Servlet obtain input the uploaded file stream and then parse inside the request parameters is more troublesome, it is generally selected using the apache open source tools common-fileupload this file upload component. This common-fileupload jar package upload component apache can go online to download face official, also in the struts of the lib folder found below, struts upload function is based on this realization. common-fileupload is dependent on the common-io package, so it needs to download the package.

First, the development environment to build

  Related FileUploadAndDownLoad Jar package to create a project, the addition of Apache commons-fileupload file upload component, as shown below:

 

 

Jar package as dependent

 

Second, Upload Files

Front HTML template

 

File processing logic block following

 

Save the file as a logic block

web.xml configured as follows

 

Operating results are as follows:

 

Files panel logic

Reception Code

2.3 , file upload details

  上述的代码虽然可以成功将文件上传到服务器上面的指定目录当中,但是文件上传功能有许多需要注意的小细节问题,以下列出的几点需要特别注意的

1、为保证服务器安全,上传文件应该放在外界无法直接访问的目录下,比如放于WEB-INF目录下。

  2、为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名。

  3、为防止一个目录下面出现太多文件,要使用hash算法打散存储。

  4、要限制上传文件的最大值。

  5、要限制上传文件的类型,在收到上传文件名时,判断后缀名是否合法。

  针对上述提出的5点细节问题,我们来改进一下UploadHandleServlet,改进后的代码如下:

文件数据表结构

文件夹数据表结构

下载数据表结构

针对上述提出的5点小细节问题进行改进之后,我们的文件上传功能就算是做得比较完善了。

三、文件下载

加载文件列表,注意,这里只列出上传完的文件和文件夹,没有上传完的就不列出了

数据库的处理逻辑比较简单,注意一下SQL语句中的条件即可

  我们要将Web应用系统中的文件资源提供给用户进行下载,首先我们要有一个页面列出上传文件目录下的所有文件,当用户点击文件下载超链接时就进行下载操作,编写一个ListFileServlet,用于列出Web应用系统中所有下载文件。

文件列表加载逻辑

       这里简单说一下ListFileServlet中listfile方法,listfile方法是用来列出目录下的所有文件的,listfile方法内部用到了递归,在实际开发当中,我们肯定会在数据库创建一张表,里面会存储上传的文件名以及文件的具体存放目录,我们通过查询表就可以知道文件的具体存放目录,是不需要用到递归操作的,这个例子是因为没有使用数据库存储上传的文件名和文件的具体存放位置,而上传文件的存放位置又使用了散列算法打散存放,所以需要用到递归,在递归时,将获取到的文件名存放到从外面传递到listfile方法里面的Map集合当中,这样就可以保证所有的文件都存放在同一个Map集合当中。

  编写一个用于处理文件下载的Servlet,DownLoadServlet的代码如下:

文件管理器主要逻辑如下:

文件管理器所有的逻辑都在PageFileMgr.java中实现。

另外,文件管理器有一些相关的配置在data目录下

系统配置数据如下

路径配置如下

文件管理器的脚本处理全部放在index.js中

关于JavaWeb中的文件上传和下载功能的内容就这么多。

最终实现的效果如下:

文件批量上传

文件批量下载

路径导航

更详细教程可以参考我的这篇文章:http://blog.ncmem.com/wordpress/2019/08/12/java-web-%e8%83%bd%e5%a4%9f%e5%ae%9e%e7%8e%b0%e6%95%b4%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9%e7%9a%84%e4%b8%8a%e4%bc%a0%e4%b8%8b%e8%bd%bd%e5%90%97%ef%bc%9f/

Guess you like

Origin www.cnblogs.com/songsu/p/11526219.html