Use of the FileUpload component and Multifile upload

Source: Internet
Author: User

Use of the FileUpload component and Multifile upload
Significance of File Upload
Uploading files frequently in projects: uploading images via qq, uploading images through photo albums, sharing data ......

Steps for File Upload
1. specify the form type as a file upload form.
You must specify the form attribute enctype = "multipart/form-data"
<Form action = "" method = "post" enctype = "multipart/form-data>
<Input type = "file" name = "files"/>
2. The submission method must be post.
Process uploaded files manually
// Obtain the file in the form
// Obtain the input stream
InputStream in = request. getInputStream ();
// Convert the stream
InputStreamReader inReader = new InputStreamReader (in );
// Define the buffer stream
BufferedReader br = new BufferedReader (inReader );
Process data files
Content-Disposition: form-data; name = "files"; filename = "D: \ 01readme.txt"
Content-Type: text/plain
To process files, you must process Content-Disposition.

Use the File Upload Component During Development to upload files
The biggest feature of a good group: simplicity, ease of use, and security, followed by powerful functions
The fileupload component is usually used to upload the package to be introduced.
1. commons-fileupload-1.2.1.jar
2. commons-io-1.4.jar
// Create a file upload Project
FileItemFactory fileItemFactory = new DiskFileItemFactory ();
FileItemFactory encapsulates each form request object into a FileIteam object
Fac. serRepository (repository) sets the temporary directory for File Upload
The default is the temp directory of the server.
The maximum memory of a virtual machine is 1 GB in Windows and 16 GB in linux.

// Create a file first and upload the core Class Object
ServletFileUpload upload = new ServletFileUpload (fileItemFactory );
Purpose: obtain all FileItem objects.
Upload. parseRequest (request); Return Value Type List, get all file upload items
Boolean isMultipartContent (request) determines the form type
Upload. setFileSizeMax (fileSize) sets the maximum value for a single file
Upload. setSizeMax (fileSize) sets the maximum value of the uploaded file (all files)
Upload. setHeaderEncoding (encoding method); equivalent to request. setContentType (encoding method );
// Determine whether it is a file upload form
If (ServletFileUpload. isMultipartContent (request )){
// Convert the request data to a FileIteam object set
Try {
// Each upload item placed in the list
List <FileItem> list = upload. parseRequest (request );
// Traverse each upload item
For (FileItem fileItem: list ){
// Determine whether it is a normal form or a file upload item
If (fileItem. isFormField ()){
// Normal Form
String fileName = fileItem. getFieldName ();
// Text box Value
String str = fileItem. getString ();
} Else {
// File form
// Form element name
String fileName = fileItem. getFieldName ();
// File type
String contentType = fileItem. getContentType ();
// Obtain the file name
String name = fileItem. getName ();
// Obtain the value of the text box, that is, the file content.
String str = fileItem. getString ();

// Set the request encoding.
FileItem. getString ("encoding method ");
// Write the file to the specified directory
FileItem. write (new File ("specify directory "))
// Delete a temporary file
FileItem. delete ()
// File stream
InputStream in = fileItem. getInputStream ();
// File Size
Long size = fileItem. getSize ();
}
}
} Catch (FileUploadException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
} Else {
System. out. println ("not a file upload form ");
}

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.