Processing pictures and processing files with descriptive information upload four (62)

Source: Internet
Author: User

One, processing the picture with the description information and processing file upload

 void

delete()
Delete the files that are saved in the temp directory.

 

 

String

getContentType () Gets the type of document
Returns the content type passed by the browser or null if not defined.

String

GetFieldName () Gets the name of the field, which 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 is used to retrieve the contents of the file.

String

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

Gets the file name.

If it is in IE get the file for c:\aaa\aaa\xxx.jpg– that is the full path.

Non ie; file name only xxx.jpg

Long

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

If your upload is an ordinary text element, you can get the data in the element in the following ways

<form enctype= "Multipart/form-data" >

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

String

getString () to get information about a normal form field.
Returns the contents of the file item as a String, using the default character encoding. (ios-8859-1)

String

getString (String encoding) to specify the encoding format
Returns the contents of the file item as a String, using the specified encoding.

void

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

The following file is used to 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 this value is true.
Determines whether or not a FileItem instance represents a simple form field.

Code:

Public class Updescservlet extends HttpServlet {

Public void doPost (httpservletrequest request, httpservletresponse response)

throws Servletexception, IOException {

Request.setcharacterencoding ("UTF-8");//Can get the Chinese file name

String path = Getservletcontext (). Getrealpath ("/up");

Diskfileitemfactory disk =

New Diskfileitemfactory ();

Disk.setrepository (new File ("d:/a"));

Try {

Servletfileupload up =

New Servletfileupload (disk);

list<fileitem> list = up.parserequest (request);

for (Fileitem file:list) {

The first step: judging 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");//The data is read by default in ISO mode

System. err. println (filename+ "=" +value);

}Else{//description is a file

String fileName = File.getname ();

FileName = filename.substring (filename.lastindexof ("\ \") +1);

File.write (new File (path+ "/" +filename));

System. err. println ("filename is:" +filename);

System. err. println ("File size is:" +file.getsize ());

File.delete ();

}

}

}catch(Exception e) {

E.printstacktrace ();

}

}

}

Two-process file upload (performance can be improved relative to the previous)

The core points are Fileitemiterator it= up.getitemiterator (Request), and file uploads are processed.

Package Cn.hx.servlet;

import java.io.File;

import java.io.IOException;

import Java.io.InputStream;

Import Java.io.PrintWriter;

Import java.util.List;

import javax.servlet.ServletException;

import Javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileItemIterator;

import Org.apache.commons.fileupload.FileItemStream;

Import Org.apache.commons.fileupload.RequestContext;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

Import Org.apache.commons.fileupload.servlet.ServletRequestContext;

import org.apache.commons.io.FileUtils;

Public class Fastservlet extends HttpServlet {

Public void doPost (httpservletrequest request, httpservletresponse response)

throws Servletexception, IOException {

Request.setcharacterencoding ("UTF-8");

String path = Getservletcontext (). Getrealpath ("/up");

Diskfileitemfactory disk =

New Diskfileitemfactory ();

Disk.setrepository (new File ("d:/a"));

Try {

Servletfileupload up = new servletfileupload (disk);

The following is the iterator pattern

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,new File (path+ "/" +filename));

}

}catch(Exception e) {

E.printstacktrace ();

}

}

}

Processing pictures and processing files with descriptive information upload four (62)

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.