struts2.0關於請求jsp方式上傳的問題

來源:互聯網
上載者:User

最近在項目中用到了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>

問題解決!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.