Java basics --- File Upload (commons-FileUpload component) and asp File Upload Component
The previous article explains how to upload the smartupload component. In this article, we will explain how to upload the commons-FileUpload component.
FileUpload is a free Upload Component provided by the Apache organization (www.apache.org), can be downloaded directly from the Apache site (: http://commons.apache.org/fileupload/), the version used in this article is 1.2.1, but the FileUpload component itself also depends on the commons component, so when downloading this component from Apache, you also need to download it together with the commons component's IO package (: http://commons.apache.org/io) the commons-fileUpload Upload Component is the most widely used component currently, and does not contain Chinese garbled characters when uploading files, configuring the commons-fileupload-1.2.1.jar and commons-io-1.4.jar to the ³ at_home/lib/directory for upload FileUpload is more complex than SmartUpload. Next let's take a look at the basic steps of FileUpload upload:
Upload PrincipleAfter the fileupload component is used to receive complete data, all the data is saved in the List set, you need to use Iterator to output each, however, because there are both common text data and uploaded files, each uploaded content is represented by a FileItem class object. Therefore, when you use Iterator to extract each FileItem object in sequence, you can use the isFormField () method in the FileItem class to determine whether the content of the current operation is normal text or upload files, if the object is uploaded, the content of the object is taken out in sequence. if the object is plain text, the specific information is obtained through the getString () method. Component upload code example 1 html code
<Html>
Jsp code
<% @ Page contentType = "text/html" pageEncoding = "GBK" %> <% @ page import = "java. util. * "%> <% @ page import =" org. apache. commons. fileupload. * "%> <% @ page import =" org. apache. commons. fileupload. disk. * "%> <% @ page import =" org. apache. commons. fileupload. servlet. * "%>
Component upload code example 2
<Html>
JSP code
<% @ Page contentType = "text/html" pageEncoding = "GBK" %> <% @ page import = "java. util. *, java. io. * "%> <% @ page import =" org. apache. commons. fileupload. * "%> <% @ page import =" org. apache. commons. fileupload. disk. * "%> <% @ page import =" org. apache. commons. fileupload. servlet. * "%> <% @ page import =" cn. mldn. lxh. util. * "%>
Note: When creating a project, this code must create an upload folder under the project name to run properly.
Inconvenience caused by the FileUpload component: