Java component fileUpload upload upload file implementation code

Generally, when dealing with form elements with servlet, the form elements are some simple text, and servlet is easy to use request Getparameter () can handle it. However, when the form contains more than some simple text, such as the upload file field, it is still a very complex task for the servlet to parse each sub part of the composite form directly from the HttpServletRequest object In order to simplify the processing of "multipart / form data" type data, corresponding components can be used for processing, which can save a lot of coding, support reuse and have high efficiency. There are also some Java components: fileUpload, SmartUpload and COS. This article will explain them with Apache's fileUpload.

To use fileUpload, you should first download the corresponding components: 1 FileUpload package: http://commons.apache.org/fileupload/ 2. IO software package: http://commons.apache.org/io/ After downloading, unzip the zip package and add commons-fileupload-1.2 1. Jar and commons-io-1.4 Copy the jar to webapp / WEB-INF / lib of Tomcat.

1、 Form page (enctype = "multipart / form data" of the form to be specified) - upload html

2、 The servlet for processing forms -- uploadservlet

The servlet is on the web XML is configured as:

Now you have completed a simple file upload function - visit the form page, select the file and click upload file. If you want to save the file to the database while uploading the file to the server, you can save the file name to the database after obtaining the file name, so that you can select the user's file according to the file name in the future!

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