1

Spring と組み合わせて Struts2 を使用してファイルをアップロードしようとしています。しかし、どういうわけか、アクション クラスに到達した後、ファイル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; 
        }
}

tiles-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

私の問題は解決しました。クラスパスだけでなく、RADの「展開アセンブリ」にもjar(commons fileuploadとcommons io)を追加する必要がありました。

于 2013-03-09T19:11:19.470 に答える