Java basics --- File Upload (commons-FileUpload component) and asp File Upload Component

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.