[JAVAWEB learning notes] 29_file upload ------ commons-fileupload, commonsfileupload

Source: Internet
Author: User

[JAVAWEB learning notes] 29_file upload ------ commons-fileupload, commonsfileupload

Today's content:

File Upload ------ commons-fileupload

 

Object uploading and downloading: Object copying

File Upload: to copy a file from a local file to a server disk, the client needs to write a file upload form ----> the server needs to write code to accept the uploaded file

File Download: to copy a file from a server disk to a local disk, you need to write the server code. The download of the client is implemented by the browser kernel.

 

I. File Upload

1, File upload steps:

(1) You need to write a file upload form.

(2) Write server-side code to receive uploaded files

 

2Client Encoding

Note:

 

3The server receives the form data uploaded by the file.

Principles of file upload and receiving

 

Apache Commons-fileupload.jar-a convenient and quick tool for File Upload

 

Procedure:

(1) download file upload jar package fileuplaod with commons-io.jar

 

(2) Use APIs in the fileupload tool for encoding

4, FileUpload tool three core objects

DiskFileItemFactory: disk file item factory -- some related configuration settings cache size temporary directory location

ServletFileUplaod: a core class for File Upload

FileItem: represents each form item

 

5. Quick Start

6File Upload API details

(1) DiskFileItemFactory

Set the cache size. The unit of factory. setSizeThreshold () is byte.

Set the directory of the temporary File: factory. setRepository (File)

 

(2) ServletFileUpload

Determine whether the form is a file upload: ServletFileUpload. isMultipartContent (request );

Parse the request to obtain the set of form items: upload. parseRequest (request );

Set the encoding method of the uploaded file name: upload. setHeaderEncoding ("UTF-8 ");

 

(3) FileItem

Determine whether it is a normal form item: item. isFormField ();

Get the name attribute value of the form: item. getFieldName ();

Get the value of the form: item. getString ("UTF-8") --- UTF-8 represents the encoding when the Chinese character is obtained

Get the name of the uploaded file: item. getName ()

Obtain the uploaded file: item. getInputStream ();

Delete temporary files: item. delete ();

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.