0

我有一个 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;



}

不知道我在这里做错了什么。将不胜感激任何帮助/提示

--

4

2 回答 2

0

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

于 2012-07-25T05:52:14.243 回答
0

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

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

2 - 检查您是否正确定义了 setter 和 getter 方法。struts2文件上传问题

希望这有帮助。

于 2016-12-21T12:03:12.863 回答