The form of enctype = "multipart/form-Data" cannot obtain commons-fileupload, a parameter other than type = file in the form, except for file.

Source: Internet
Author: User

You will find that after the form of enctype = "multipart/form-Data" is submitted, other parameters cannot be obtained using the traditional request. getparameter in the background.

I personally understand that every parameter is processed in the form of a stream and can be obtained only in a special way.

This document uses commons-fileupload to obtain parameters other than file.

HtmlCode:

   <  Form  ID  = "Uploadfileform"  Action  = "Uploadservlet"  Target  = "Hiddenframe"  Method  = "Post" Enctype  = "Multipart/form-Data"  >  
< Input ID = "Fileinput" Type = "File" Onchange = "Uploadfile ();" Name = "Upload" >
< Input Type = "Hidden" Value = "/Image /" Name = "Directory" >
</ Form >

Servlet code:

 Package  Ui. filemanage;

Import Java. Io. file;
Import Java. Io. ioexception;
Import Java. Io. inputstream;
Import Java. util. iterator;
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. disk. diskfileitemfactory;
Import Org. Apache. commons. fileupload. servlet. servletfileupload;

Public Class Uploadservlet Extends Httpservlet {

@ Suppresswarnings ( " Unchecked " )
Public Void Dopost (httpservletrequest request, httpservletresponse response)
Throws Servletexception, ioexception {
Response. setcontenttype ( " Text/html; charsets = UTF-8 " );
String errormessage = Null ;
Try {
Diskfileitemfactory fac = New Diskfileitemfactory ();
Servletfileupload upload = New Servletfileupload (FAC );
Upload. setheaderencoding ( " UTF-8 " );
List filelist = Null ;
Filelist = Upload. parserequest (request );

String dir = (Fileitem) filelist. Get ( 1 ). Getstring (); // Get directory Parameters
If (Dir = Null ){
Dir = " / " ;
}
String savepath = Getservletcontext (). getrealpath ( " /Test " + DIR );
System. Out. println ( " Save: " + Savepath );
File F1 = New File (savepath );
If ( ! F1.exists ()){
F1.mkdirs ();
}

Iterator < Fileitem > It = Filelist. iterator ();
String name = "" ;
While (It. hasnext ()){
Fileitem = It. Next ();
If ( ! Item. isformfield ()){
Name = Item. getname ();
File = New File (savepath + File. separatorchar + Name );
If (File. exists ()){
Errormessage = " This file already exists! Rename and upload again. " ;
}
Item. Write (File );
Item. Delete (); // Release output stream
}
}
If (Errormessage = Null ){
Response. getwriter (). Print ( " <SCRIPT> parent. finishuploading (1); </SCRIPT> " );
} Else {
Response. getwriter (). Print ( " <SCRIPT> parent. finishuploading (0 ,' " + Errormessage + " '); </SCRIPT> " );
}
} Catch (Exception ){
Response. getwriter (). Print ( " <SCRIPT> parent. finishuploading (0, 'Upload Failed. Please try again. '); </SCRIPT> " );
}
}
}

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.