Function: File Upload

Source: Internet
Author: User

public class Uoloadservlet extends HttpServlet {
Private static final long serialversionuid = 1L;

protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
1. Create a file disk factory
Diskfileitemfactory diskfileitemfactory = new Diskfileitemfactory ();
2. Create a file upload object using the file disk factory
FileUpload FileUpload = new FileUpload (diskfileitemfactory);
3. Use the parse method of the file upload object to parse the request data stream
try {
list<fileitem> lists = fileupload.parserequest (request);
for (Fileitem fileitem:lists) {
String fieldName = Fileitem.getfieldname ();
if (Fileitem.isformfield ()) {
This entry is a normal input box
GetFieldName represents the name of the parameter
Return parameter values
String value = fileitem.getstring ("Utf-8");
SYSTEM.OUT.PRINTLN ("Parameter name:" +fieldname+ "-Parameter value:" +value);
}else{
File Upload Type
Get file name
String name = Fileitem.getname ();//
File input stream
InputStream InputStream = Fileitem.getinputstream ();
FileOutputStream FileOutputStream = new FileOutputStream ("c:\\users\\huyoufu\\desktop\\upload\\" +name);
Using Ioutils
Ioutils.copy (InputStream, FileOutputStream);
Fileoutputstream.close ();
Inputstream.close ();
}
}
} catch (Fileuploadexception e) {
E.printstacktrace ();
}
4. Return to list set with parsed Fileitem

Function: File Upload

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.