最近在項目中用到了struts2.0,並在頁面中嵌入了一個Kindeditor的編輯器,Kindeditor的插入圖片功能先通過一個jsp將圖片上傳到伺服器,然後擷取圖片的url進行顯示,然而編碼時圖片總是無法上傳到伺服器,代碼如下,經調試發現fileItems總是為空白
<%@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{
//檔案儲存目錄路徑
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();
//檔案儲存目錄URL
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String SaveUrl=basePath+ "rules/uploaded/";
System.out.println("SaveUrl:"+SaveUrl);
//定義允許上傳的副檔名
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總是為空白
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(/"上傳副檔名是不允許的副檔名。/");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]; // 4K 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( "<head>");
out.println( "<title></title>");
out.println( "<meta http-equiv=/"content-type/" content=/"text/html; charset=UTF-8/">");
out.println( "</head>");
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( "</html>");
}
}
}catch(Exception e)
{
out.println( "<script type=/"text/javascript/">alert(/"" +e.getMessage() + "/");parent.KindDisableMenu();parent.KindReloadIframe();</script>");
}
%>
遍尋解決方案,考慮到正常使用struts2.0的攔截器上傳並未出現此類情況,考慮請求被攔截,遂修改web.xml,由
<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>
修改為:
<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>
問題解決!