2014-08-29 139 views
2

我是Struts2的新手,嘗試使用fileUploader攔截器。我附上我的代碼層Fileuploader攔截器Struts2

Action類(FileUploadAction)

package com.caveofprogramming.actions; 
import java.io.File; 


import org.apache.struts2.convention.annotation.InterceptorRef; 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Result; 

import com.opensymphony.xwork2.ActionSupport; 

public class FileUploadAction extends ActionSupport{ 

    private File fileUpload; 
    private String fileUploadContentType; 
    private String fileUploadFileName; 

    public String getFileUploadContentType() { 
     return fileUploadContentType; 
    } 

    public void setFileUploadContentType(String fileUploadContentType) { 
     this.fileUploadContentType = fileUploadContentType; 
    } 

    public String getFileUploadFileName() { 
     return fileUploadFileName; 
    } 

    public void setFileUploadFileName(String fileUploadFileName) { 
     this.fileUploadFileName = fileUploadFileName; 
    } 

    public File getFileUpload() { 
     return fileUpload; 
    } 

    public void setFileUpload(File fileUpload) { 
     this.fileUpload = fileUpload; 
    } 

    @Action(value = "/fileUpload", 
      results={@Result(name="success",location="/success.jsp"), 
      @Result(name="error",location="/error.jsp"), 
      @Result(name="input",location="/error.jsp") 
    }, 
      interceptorRefs={ 
      @InterceptorRef(
        params={"allowedTypes","image/jpeg,image/jpg,application/zip", 
          "maximumSize","1024000"}, 
          value="fileUpload" 
         ), 
      @InterceptorRef("defaultStack"), 
      @InterceptorRef("validation") 
    } 
    ) 
    public String execute(){ 
     try{ 
     return SUCCESS; 
     } catch(Exception e){ 
      return ERROR; 
     } 
    } 
    public String display() { 
     return NONE; 
    } 

} 

error.jsp文件

<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<body> 
    <s:fielderror/> 
</body> 
</html> 

的success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
Success 
</body> 
</html> 

fileUpload.jsp

<%@ taglib prefix="s" uri="/struts-tags" %> 

    <html> 
    <head> 
    <s:head /> 
    </head> 

    <body> 
    <h1>Struts 2 &lt;s:file&gt; file upload example</h1> 
    <s:form method="post" enctype="multipart/form-data" action="fileUpload"> 
     <s:file label="File One" name="fileUpload" /> 
     <s:submit /> 
    </s:form> 

    </body> 
    </html> 

我不理解爲什麼我收到此錯誤

「內容類型不允許的:文件上傳 「攝影-104a.jpg」 「upload_37fbf440_169b_4687_af65_93c8c967256c_00000000.tmp」 圖像/ pjpeg「

雖然我的上傳文件格式是」.jpg「。請幫助我

+0

的*擴展名爲*'.jpg',但是這不是MIME類型。 – 2014-09-02 12:52:43

回答

0

您收到此錯誤的原因可能是因爲您不允許內容類型爲image/pjpeg的文件。使用fileUpload攔截器的參數來定義允許的MIME類型

<interceptor-ref name="fileUpload"> 
    <param name="allowedTypes">image/jpeg,image/pjpeg</param> 
</interceptor-ref>