Example analysis of file upload and download function implemented by Java Web

In the development of web application system, file upload and download function is a very common function. Today, let's talk about the implementation of file upload and download function in Java Web.

For file upload, the browser submits the file to the server in the form of stream during the upload process. It is troublesome to directly use servlet to obtain the input stream of the uploaded file and then parse the request parameters. Therefore, the file upload component of Apache's open source tool common fileUpload is generally selected. The jar package of the common file upload upload component can be downloaded from the Apache official website or found under the Lib folder of struts. The function of struts upload is based on this implementation. Common file upload depends on the common IO package, so you also need to download this package.

1、 Construction of development environment

Create a fileuploadanddownload project and add the jar package related to Apache's Commons fileUpload file upload component, as shown in the following figure:

2、 Realize file upload

2.1 file upload page and message prompt page

upload. The code of the JSP page is as follows:

message. The JSP code is as follows:

2.2. Handle the servlet of file upload

The code of uploadhandleservlet is as follows:

On the web Register the uploadhandleservlet in the XML file

The operation effect is as follows:

After the file is uploaded successfully, the uploaded file is saved in the upload directory under the WEB-INF directory, as shown in the following figure:

2.3 details of file upload

Although the above code can successfully upload files to the specified directory on the server, there are many small details to pay attention to in the file upload function. The following points need special attention

1. In order to ensure the security of the server, the uploaded files should be placed in a directory that cannot be directly accessed by the outside world, such as the WEB-INF directory.

2. In order to prevent file overwriting, a unique file name should be generated for the uploaded file.

3. In order to prevent too many files under a directory, hash algorithm should be used to break up the storage.

4. Limit the maximum value of uploaded files.

5. To limit the type of uploaded file, judge whether the suffix is legal when receiving the uploaded file name.

For the above five details, let's improve the uploadhandleservlet. The improved code is as follows:

After improving the above five small details, our file upload function is relatively perfect.

3、 File download

3.1 list the file resources provided for download

We want to provide file resources in the web application system to users for downloading. First, we need to have a page to list all files in the uploaded file directory. When users click the file download hyperlink, they will download, and write a listfileservlet to list all downloaded files in the web application system.

The code of listfileservlet is as follows:

Let's briefly talk about the LISTFILE method in listfileservlet. The LISTFILE method is used to list all files in the directory. Recursion is used inside the LISTFILE method. In actual development, we will certainly create a table in the database, which will store the uploaded file name and the specific storage directory of the file, We can know the specific storage directory of the file through the query table. Recursive operation is not required. This example is because the database is not used to store the uploaded file name and the specific storage location of the file, and the storage location of the uploaded file is scattered by hash algorithm. Therefore, recursion is required. During recursion, Store the obtained file name in the map set passed from the outside to the LISTFILE method, so as to ensure that all files are stored in the same map set.

On the web Configure listfileservlet in XML file

Show the LISTFILE. Of the downloaded file The JSP page is as follows:

By accessing listfileservlet, you can The file resources provided for users to download are displayed in the JSP page, as shown in the following figure:

3.2. Realize file download

Write a servlet for file download. The code of downloadservlet is as follows:

On the web Configure downloadservlet in XML file

Click the [Download] hyperlink and submit the request to the downloadservlet for processing. The file download can be realized. The operation effect is shown in the following figure:

From the running results, we can see that our file download function can download files normally.

This article has been sorted into the summary of Java upload operation skills. You are welcome to learn and read.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>