1

我正在尝试使用 Struts2 和 Spring 一起上传文件。但是,不知何故,在达到我的行动课程后,我的文件filename和文件内容类型都以null. 我尝试搜索问题但没有结果。下面是我的文件上传代码。

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
 <s:actionerror />
<s:form action="uploadAction" method="POST" enctype="multipart/form-data">

<s:file name="fileUpload" label="Choose File" size="40" />

<s:submit value="Upload" name="submit" />

</s:form>

Struts.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
        <include file="struts-default.xml"/>
        <package name="a" extends="struts-default">

   <action name="resultAction" class="ManagePlanDataFileUploadActionBean"   method="executeFileUploadDemo">

            <interceptor-ref name="fileUpload">
                <param name="maximumSize">10240</param>
                <param name="allowedTypes">text/plain</param>
            </interceptor-ref>                
            <interceptor-ref name="defaultStack" />
    <result name="success">displayResultsJSP</result>

</action>
        </package>
    </struts>

applicationContext-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <bean id="ManagePlanDataFileUploadActionBean" scope="prototype"

    class="com.hix.action.planmanagement.ManagePlanDataFileUploadAction">

    </bean>
</beans>

ManagePlanUploadAction

package com.hix.action.planmgmt;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class ManagePlanUploadAction extends ActionSupport{

      private HttpServletRequest request;

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

       public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
             this.request = paramHttpServletRequest;
       }

       public HttpServletRequest getServletRequest() {
             return request;
       }

        public File getFileUpload() {
            return fileUpload;
        }

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

        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 String executeFileUploadDemo() throws Exception {
            try {
                String filePath = "C:/Myuploads2";
                System.out.println("Server path:" + filePath);
                File fileToCreate = new File(filePath, fileUploadFileName);
                FileUtils.copyFile(fileUpload, fileToCreate);
                } catch(Exception e) {
                    e.printStackTrace();
                    addActionError(e.getMessage());
                    return SUCCESS;
                }       
                    System.out.println("File :" + fileUpload);
                    System.out.println("Filename : " + fileUploadFileName);
                    System.out.println("File type : " + fileUploadContentType);
                return SUCCESS; 
        }
}

瓷砖-def.xml

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
<definition name="displayResultsJSP" template="/jsp/planmgmt/Result.jsp">

<put-attribute name="header" value="/jsp/template/defaultHeader.jsp" /> 

<put-attribute name="footer" value="/jsp/template/defaultFooter.jsp" />

</definition>

</tiles-definitions>

结果.jsp

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

<body>
   File Name : <s:property value="fileUploadFileName"/> 
  <br/>
   Content Type : <s:property value="fileUploadContentType"/> 
  <br/>
   File : <s:property value="fileUpload"/> 
</body>
</html>
4

1 回答 1

-1

我的问题现在已经解决了。我必须将 jars(commons fileupload 和 commons io)添加到我的类路径中,还要添加到 RAD 的“部署程序集”中。

于 2013-03-09T19:11:19.470 回答