Analysis of file upload function based on java servlet

Recently, the project needs to do a file upload function. After sharing, take it as a note. The upload function is implemented in Java in the background, and the Ajax implementation of JS is mainly used in the front end. The background also adds regular deletion of temporary files. The effect is shown in the figure

The first is the main class of upload function. Here is the code

Commons-fileupload-1.3.0 needs to be introduced 1.jar,commons-io-2.4. In the process of uploading jar, we need to obtain the upload progress and other information in real time. The introduced library adds a progresslistener interface for us. We write another class to implement this interface, and add this interface to the above class

The following is the specific implementation code of this listening class

Now you can get the upload progress and other information, but you still need a servlet to return to the front end, which is implemented below

The function code of background upload has been written. The front end of upload is realized below. The first is HTML

The interface is relatively simple. An added a tag is responsible for hiding the uploaded input. CSS files are mainly used to display the upload progress of rendering

Then comes the focus of the front end, JS file

Use ajax to send the uploaded file and obtain the upload progress, results and other information. The file API of HTML5 is used, so it must be above IE9 to be compatible. There is another problem with Firefox. Ajax requests do not return immediately until all Ajax requests are sent, and the same result is not returned until all Ajax requests are sent, which leads to the non display of upload progress. However, the upload progress information can also be obtained using the file API of HTML5, with a little code added. The value in the div of test at the bottom of the page is the progress obtained at the front end.

All uploaded files are completed, and then the temporary files after uploading are processed. Because the UUID named files are used, many files will be generated, and useless files need to be processed regularly. Using servletcontextlistener: there is a servletcontextlistener interface in the servlet API, which can monitor the life cycle of ServletContext objects, which is actually the life cycle of web applications.

When the servlet container starts or terminates the web application, it will trigger the servletcontextevent event event, which is handled by the servletcontextlistener. Two methods for handling servletcontextevent events are defined in the servletcontextlistener interface. Using its characteristics, the function of regularly deleting temporary files is realized. The code is as follows:

The above is just a listener, which is responsible for regularly calling the method of deleting temporary files. The specific implementation is the following class

Judge whether the file times out, automatically delete it when it times out, and automatically delete the folder.

The above is the whole content of this article. I hope it will help you learn java programming.

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
分享
二维码
< <上一篇
下一篇>>