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> " );
}
}
}