Use of the File Upload component FileUpload component

Source: Internet
Author: User

The meaning of file upload
In the project often upload files: QQ upload pictures, album upload pictures, information sharing ...

Steps to implement file uploads
1. Specify the type of form for the file upload form
The form's properties must be specified enctype= "Multipart/form-data"
<form action= "" method= "Post" enctype= "multipart/form-data>
<input type= "file" name= "Files"/>
2. The submission method must be post
Manually upload file processing
Get the file in the form
Get input stream
InputStream in = Request.getinputstream ();
Convert stream
InputStreamReader Inreader = new InputStreamReader (in);
Defining buffer Streams
BufferedReader br = new BufferedReader (Inreader);
Working with Data files
Content-disposition:form-data; Name= "Files"; Filename= "D:\01readme.txt"
Content-type:text/plain
Need to deal with the file specifically, must handle content-disposition

Uploading files using the file Upload component in development
The biggest feature of a good group: easy to use security followed by powerful
A package that is typically used to upload files using the FileUpload component
1.commons-fileupload-1.2.1.jar
2.commons-io-1.4.jar
Create File Upload Project class
Fileitemfactory fileitemfactory = new Diskfileitemfactory ();
Fileitemfactory encapsulates each form request object into a single Fileiteam object
Fac.serrepository (repository) Set File Upload temp directory
The default is the TEMP directory for the server
The maximum virtual machine exists under Windows for 1g under Linux up to 16g.

Create a file Upload Core class object first
Servletfileupload upload = new Servletfileupload (fileitemfactory);
Function: Can get all the Fileitem objects
Upload.parserequest (request); Return value type list, get all file uploads
Boolean ismultipartcontent (Request) to determine the type of form
Upload.setfilesizemax (fileSize) sets the maximum value of a single file
Upload.setsizemax (fileSize) sets the maximum number of uploaded files (all files)
upload.setheaderencoding (encoding); equivalent to Request.setcontenttype (encoding);
Determine if a file is uploading a form
if (servletfileupload.ismultipartcontent (request)) {
Convert request data to Fileiteam object collection
try {
Each upload item placed in the list
list<fileitem> list = upload.parserequest (request);
Traverse every upload item
for (Fileitem fileitem:list) {
Determine whether it is a normal form or a file upload item
if (Fileitem.isformfield ()) {
Normal forms
String fileName = Fileitem.getfieldname ();
The value of the text box
String str = fileitem.getstring ();
} else {
File Form
FORM element name
String fileName = Fileitem.getfieldname ();
Types of files
String ContentType = Fileitem.getcontenttype ();
Get file name
String name = Fileitem.getname ();
Gets the value of the text box, which is the file content
String str = fileitem.getstring ();

Set the encoding processing encoding for the request
Fileitem.getstring ("coding method");
Write the file to the specified directory
Fileitem.write (New File ("specified directory"))
Delete temporary files
Fileitem.delete ()
File stream
InputStream in = Fileitem.getinputstream ();
Size of File
Long size = Fileitem.getsize ();
}
}
} catch (Fileuploadexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
} else {
System.out.println ("Not File Upload form");
}

Use of the File Upload component 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.