Sample analysis of java web file upload and download function

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.

1. Upload a simple example

Jsp

Servlet

2. Modified upload function:

matters needing attention:

1. Upload Chinese garbled file names and Chinese garbled data setHeaderEncoding("UTF-8"); // Solve the Chinese garbled code of the uploaded file name. / / the form is a file upload, and the request code is set to be invalid. You can only manually convert 1.1 value = new string (value. GetBytes ("iso8859-1"), "UTF-8"); 1.2 String value = item. getString("UTF-8");

2. 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

3. To prevent file overwriting, a unique file name should be generated for the uploaded file

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

5. To limit the maximum value of uploaded files, you can use servletfileupload The setfilesizemax (1024) method is implemented by capturing: fileuploadbase Filesizelimitexceededexception exception to give a user-friendly prompt

6. if you want to ensure that the temporary files are deleted, you must call item. after processing the uploaded files. Delete method

7. Limit the type of uploaded file: judge whether the suffix is legal when receiving the uploaded file name

8. Monitor file upload progress:

9. Dynamically add file upload input items in the web page

Upload jsp:

Upload Servlet

3. Download function

JSP display

Download processing Servlet

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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