Struts 2 FileUpload无法正常工作

时间:2012-07-20 18:50:44

标签: java-ee file-upload struts2

我有一个struts 2应用程序,其中2个页面有struts文件上传。一个完美无缺,而另一个无法工作。我给了我“字段的无效字段值”secImage“。

以下是JSP的代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>

<html>
<head>
</head>
<body>
    <%@ include file="header.jsp" %>

    <s:form action="Test" method="POST" style="width:90%;">
           Another form here......
    </s:form>
    <s:form action="Test" method="POST" enctype="multipart/form-data">
        <s:push value="secVul">
            <s:hidden name="vulId" />
            <s:hidden name="scanHistory.scanId" />
            <s:textfield name="protocol" label="Protocol" style="width: 80%;" />
            <s:textarea name="description" label="Description" style="width: 80%;" />
            <s:textarea name="solution" label="solution" />
            <more form elements here>sdfasdfasdfasd

            <s:file name="secImage" label="Image to Upload" size="40"/>
            <s:submit value="Add/Update" align="center"/>
        </s:push>
    </s:form>
<%@ include file="footer.jsp" %>
</body>
</html>

这是struts配置

<interceptor-ref name="fileUpload">
<param name="maximumSize">909715200</param>
<param name="allowedTypes">
text/xml,application/octet-stream,image/png,image/gif,image/jpeg,image/pjpeg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />

    <action name="Test" method="saveOrUpdate" class="com.Test">
        <result name="success" type="redirect">listVulsMGR</result>
        <result name="input">/WEB-INF/pages/secVuls.jsp</result>
        <result name="error">/WEB-INF/pages/secVuls.jsp</result>
    </action>

行动类代码:

public class Test extends ActionSupport implements ModelDriven<Test>, Preparable,ServletRequestAware {

private HttpServletRequest servletRequest;
private static final Logger logger = Logger.getLogger(ListVulsActions.class);


private File secImage;
private String secImageContentType;
private String secImageFileName;


private HashMap userFilters = new HashMap();
public Test() {


}

public String saveOrUpdate() {
    try {

        ServletContext context = ServletActionContext.getServletContext();
        String filePath = context.getInitParameter("UploadDirectory");
        logger.debug("Server path:" + filePath);
        logger.debug("this.secImageFileName " + this.secImageFileName);

        File fileToCreate = new File(filePath, this.secImageFileName);

        FileUtils.copyFile(this.secImage, fileToCreate);

}           
        addActionMessage("successfully added/updated.");
        return SUCCESS;
    } catch (Exception ex) {
        logger.error("Error in Test", ex);
        addActionError("Error occured in Test. Please contact your administrator.\n" + ex.getMessage());
        return ERROR;
    }

}

public File getSecImage() {
            System.out.println(secImage.getName());
    return secImage;
}

/**
 * @param secImage the secImage to set
 */
public void setSecImage(File secImage) {
    System.out.println(secImage.getName());
    this.secImage = secImage;
}

/**
 * @return the secImageContentType
 */
public String getSecImageContentType() {
    System.out.println(secImageContentType);
    return secImageContentType;
}

/**
 * @param secImageContentType the secImageContentType to set
 */
public void setSecImageContentType(String secImageContentType) {
    System.out.println(secImageContentType);
    this.secImageContentType = secImageContentType;
}

/**
 * @return the secImageFileName
 */
public String getSecImageFileName() {
    System.out.println(secImageFileName);
    return secImageFileName;
}

/**
 * @param secImageFileName the secImageFileName to set
 */
public void setSecImageFileName(String secImageFileName) {
    System.out.println(secImageFileName);
    this.secImageFileName = secImageFileName;
}

@Override
public void setServletRequest(HttpServletRequest servletRequest) {

    this.servletRequest = servletRequest;



}

不确定我在这里做错了什么。非常感谢任何帮助/提示

-

2 个答案:

答案 0 :(得分:0)

取另一个变量名是<s:file name的类型文件类型生成setter和getter private File secImage; getter &setters()FileUtils.copyFile(this.secImage, fileToCreate);

进行操作

答案 1 :(得分:0)

1 - 检查JSP中的表单行必须包含: enctype =&#34; multipart / form-data&#34;

例如:<s:form theme="simple" enctype="multipart/form-data" > 注意:如果您的JSP扩展,父级也需要包含该行。

2 - 检查您定义的setter和getter方法是否正确。 struts2 FileUpload Problem

希望得到这个帮助。