Recently, struts2.0 is used in the project, and a kindeditor editor is embedded in the page. The insert image function of kindeditor uploads images to the server through a JSP, then, the image URL is retrieved for display. However, the image cannot be uploaded to the server during encoding. The Code is as follows. After debugging, it is found that fileitems is always empty.
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "org. Apache. commons. fileupload. *" %>
<% @ Page import = "org. Apache. commons. fileupload. servlet. *" %>
<% @ Page import = "org. Apache. commons. fileupload. disk. *" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. util. arraylist" %>
<% @ Page import = "Java. util. List" %>
<% @ Page import = "Java. util. iterator" %>
<%
Try {
// Path of the file storage directory
Servletcontext SC = This. getservletcontext ();
String savepath = SC. getrealpath ("/") + "// rules // uploaded //";
System. Out. println ("savepath:" + savepath );
File myfilepath = new file (savepath );
If (! Myfilepath. exists ())
Myfilepath. mkdir ();
// File Save directory URL
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + request. getcontextpath () + "/";
String saveurl = basepath + "rules/uploaded /";
System. Out. println ("saveurl:" + saveurl );
// Define the file extension that can be uploaded
Arraylist extarr = new arraylist ();
Extarr. Add ("GIF ");
Extarr. Add ("jpg ");
Extarr. Add ("PNG ");
Extarr. Add ("BMP ");
Diskfileupload upload = new diskfileupload ();
System. Out. println ("rquest:" + request. getrequesturl ());
Upload. setheaderencoding ("UTF-8 ");
Upload. setsizemax (2*1024*1024 );
Upload. setsizethreshold (1*1024*1024 );
Upload. setrepositorypath (system. getproperty ("Java. Io. tmpdir "));
String filewidth = "";
String fileheight = "";
String fileborder = "";
String filetitle = "";
String filealign = "";
String filehspace = "";
String filevspace = "";
List fileitems = upload. parserequest (request); // fileitems is always empty
For (iterator iter = fileitems. iterator (); ITER. hasnext ();)
{
Fileitem item = (fileitem) ITER. Next ();
If (item. isformfield () & item. getname ()! = NULL ){
If (item. getname (). Equals ("imgwidth "))
Filewidth = item. getstring ();
Else if (item. getname (). Equals ("imgheight "))
Fileheight = item. getstring ();
Else if (item. getname (). Equals ("imgborder "))
Fileborder = item. getstring ();
Else if (item. getname (). Equals ("imgtitle "))
Filetitle = item. getstring ();
Else if (item. getname (). Equals ("imgalign "))
Filealign = item. getstring ();
Else if (item. getname (). Equals ("imghspace "))
Filehspace = item. getstring ();
Else if (item. getname (). Equals ("imgvspace "))
Filevspace = item. getstring ();
}
If (! Item. isformfield ()){
String filename = item. getname ();
Int Pos = filename. lastindexof (".");
If (Pos <= 0 | Pos = filename. Length ()-1)
Break;
String filetype = filename. substring (Pos + 1, filename. Length ());
If (filetype = NULL | filetype. Equals (""))
Break;
If (extarr. indexof (filetype. tolowercase () <0)
{
Out. println ("<SCRIPT type =/" text/JavaScript/"> alert (/" the File Upload extension is not allowed. /"); Parent. kinddisablemenu (); parent. kindreloadiframe (); </SCRIPT> ");
}
Long filesize = item. getsize ();
Inputstream instream = item. getinputstream ();
String newfilename = system. currenttimemillis () + "." + filetype;
Fileoutputstream Fos = new fileoutputstream (savepath + newfilename );
Int bytesread;
Byte [] Buf = new byte [4*1024]; // 4 K buffer
While (bytesread = instream. Read (BUF ))! =-1)
{
FOS. Write (BUF, 0, bytesread );
}
FOS. Flush ();
FOS. Close ();
Instream. Close ();
String fileurl = saveurl + newfilename;
Out. println ("<HTML> ");
Out. println ("Out. println ("<title> </title> ");
Out. println ("<meta http-equiv =/" Content-Type/"content =/" text/html; charset = UTF-8/"> ");
Out. println ("Out. println ("<body oncontextmenu = false> ");
Out. println ("<SCRIPT type =/" text/JavaScript/"> parent. kindinsertimage (/"" + fileurl + "/",/"" + filewidth + "/",/"" + fileheight + "/", /"" + fileborder + "/",/"" + filetitle + "/",/"" + filealign + "/",/"" + filehspace + "/", /"" + filevspace + "/"); </SCRIPT> ");
Out. println ("</body> ");
Out. println ("}
}
} Catch (exception E)
{
Out. println ("<SCRIPT type =/" text/JavaScript/"> alert (/" "+ E. getmessage () + "/"); parent. kinddisablemenu (); parent. kindreloadiframe (); </SCRIPT> ");
}
%>
For normal use of struts2.0 interceptor upload, the request is blocked, and Web. XML is modified
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
To:
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern> *. Action </url-pattern>
</Filter-mapping>
Solve the problem!