Process image and processing file with description Information upload 4 (62), file upload 62

Source: Internet
Author: User

Process image and processing file with description Information upload 4 (62), file upload 62

I. process the upload of images and processed files with instructions

 void

delete()
Delete the files stored in the temporary directory.

 

 

String

GetContentType() Obtain the document type
Returns the content type passed by the browser or null if not defined.

String

GetFieldName() Obtain the field name, that is, name = xxxx
Returns the name of the field in the multipart form corresponding to this file item.

<Input type = "file" name ="Img"/>

InputStream

GetInputStream()
Returns an InputStream that can be used to retrieve the contents of the file.

String

GetName()
ReturnsOriginalFilename in the client's filesystem, as provided by the browser (or other client software ).

Obtain the file name.

If the file obtained in IE is c: \ aaa \ xxx.jpg-the complete path.

Non-IE; the file name is only xxx.jpg

 

 

Long

GetSize() Get the file size equivalent to in. avilivable ();
Returns the size of the file item

If you upload a common text element, you can obtain the data in the element in the following ways:

<Form enctype = "multipart/form-data">

<Input type = "text" name = "name"/>

String

GetString() Is used to obtain information about common form fields.
Returns the contents of the file item as a String, using the default character encoding. (IOS-8859-1)

String

GetString(String encoding) the encoding format can be specified.
Returns the contents of the file item as a String, using the specified encoding.

Void

Write(File file) directly save the File to another file.
A convenience method to write an uploaded item to disk.

The following files determine whether a fileItem is a file (type = file) object or a text (type = text | checkbox | radio) object:

 boolean

isFormField()If it is text | checkbox | radio | select, the value is true.
Determines whether or notFileItemInstance represents a simple form field.

Code:

Public ClassUpDescServletExtendsHttpServlet {

Public VoidDoPost (HttpServletRequest request, HttpServletResponse response)

ThrowsServletException, IOException {

Request. setCharacterEncoding ("UTF-8"); // get Chinese file names

String path = getServletContext (). getRealPath ("/up ");

DiskFileItemFactory disk =

NewDiskFileItemFactory ();

Disk. setRepository (NewFile ("d:/"));

Try{

ServletFileUpload up =

NewServletFileUpload (disk );

List <FileItem> list = up. parseRequest (request );

For(FileItem file: list ){

// Step 1: determine whether it is a normal form item

If(File. isFormField ()){

String fileName = file. getFieldName (); // <input type = "text" name = "desc"> = desc

String value = file. getString ("UTF-8"); // data is read in ISO by default

System.Err. Println (fileName + "=" + value );

}Else{// Indicates a file

String fileName = file. getName ();

FileName = fileName. substring (fileName. lastIndexOf ("\") + 1 );

File. write (NewFile (path + "/" + fileName ));

System.Err. Println ("file name:" + fileName );

System.Err. Println ("file size:" + file. getSize ());

File. delete ();

}

}

}Catch(Exception e ){

E. printStackTrace ();

}

}

}

2. Process File Upload (Performance Improvement)

 

Use FileItemIterator it = up. getItemIterator (request) to process file uploads.

PackageCn. hx. servlet;

ImportJava. io. File;

ImportJava. io. IOException;

ImportJava. io. InputStream;

ImportJava. io. PrintWriter;

ImportJava. util. List;

ImportJavax. servlet. ServletException;

ImportJavax. servlet. http. HttpServlet;

ImportJavax. servlet. http. HttpServletRequest;

ImportJavax. servlet. http. HttpServletResponse;

ImportOrg. apache. commons. fileupload. FileItem;

ImportOrg. apache. commons. fileupload. FileItemIterator;

ImportOrg. apache. commons. fileupload. FileItemStream;

ImportOrg. apache. commons. fileupload. RequestContext;

ImportOrg. apache. commons. fileupload. disk. DiskFileItemFactory;

ImportOrg. apache. commons. fileupload. servlet. ServletFileUpload;

ImportOrg. apache. commons. fileupload. servlet. ServletRequestContext;

ImportOrg. apache. commons. io. FileUtils;

Public ClassFastServletExtendsHttpServlet {

Public VoidDoPost (HttpServletRequest request, HttpServletResponse response)

ThrowsServletException, IOException {

Request. setCharacterEncoding ("UTF-8 ");

String path = getServletContext (). getRealPath ("/up ");

DiskFileItemFactory disk =

NewDiskFileItemFactory ();

Disk. setRepository (NewFile ("d:/"));

Try{

ServletFileUpload up =NewServletFileUpload (disk );

// The following is the iterator Mode

FileItemIterator it = up. getItemIterator (request );

While(It. hasNext ()){

FileItemStream item = it. next ();

String fileName = item. getName ();

FileName = fileName. substring (fileName. lastIndexOf ("\") + 1 );

InputStream in = item. openStream ();

FileUtils.CopyInputStreamToFile(In,NewFile (path + "/" + fileName ));

}

}Catch(Exception e ){

E. printStackTrace ();

}

}

}

 

 

 

 

 

Related Article

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.