0

Struts2 WEB アプリケーションで Excel のみのファイルをアップロードしようとしています。したがって、Jsp ページで次のコードを使用します。

<s:label value="File Name : *" />
<s:file name="fileUpload" label="Select a File to upload"/>
<br>

<br>
<s:submit value="Add" name="add" tabindex="8" /> 



    </s:form>

ファイルの内容を表示する Response.jsp ページでは、次のようにタイプされます。

   <s:form  action="saveBulkStores.action" method="get" >

<h4>
   File Name : <s:property value="fileUploadFileName"/> 
</h4> 

<h4>
   Content Type : <s:property value="fileUploadContentType"/> 
</h4> 

<h4>
   File : <s:property value="fileUpload"/> 
</h4> 

<br>
    </s:form>

Struts.xml で:

            <action name="bulkStores" class="com.action.FilesUploadAction"
        method="loadBulkStoresPage"> 
        <result name="input">/viewfile.jsp</result>
        <result name="success">/uploadfile.jsp</result> 
    </action> 

            <action name="saveBulkStores" class="com.action.FilesUploadAction"
        method="saveBulkStores"> 
        <interceptor-ref name="exception"/>
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="fileUpload">
            <param name="allowedTypes">text/plain</param>
            <param name="maximumSize">10240</param>
         </interceptor-ref> 
        <interceptor-ref name="params">
            <param name="excludeParams">dojo\..*,^struts\..*</param>
        </interceptor-ref>
        <interceptor-ref name="validation">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref> 
        <result name="input">/uploadfile.jsp</result>
        <result name="success">/success.jsp</result> 
    </action>

イン アクション クラス:

         public String loadBulkStoresPage(){
    System.out.println("FILES BULK UPLOADS.........");
    return SUCCESS;
     }
         private File fileUpload;
     private String fileUploadContentType; 
     private String fileUploadFileName;
         //Getters and Setters for above Fields.

次のようにファイル名、コンテンツ タイプを表示します。

         public String saveBulkStores(){
    System.out.println("check Bulk upload file");

    System.out.println("fileName:"+fileUploadFileName);
    System.out.println("content type:"+fileUploadContentType);
    System.out.println("fileupload:"+fileUpload); 

    return SUCCESS;
     }

出力:

   It's displaying NUll value only for my display statements. So anyone help me to fix this issue. thanks in Advance. 

私はこの仕事をするのが初めてです。

4

2 に答える 2

0

<s:file id="myFile" name="myFile" disabled="true" ></s:file>jspにタグを入れてファイルをアップロードするために使用されるタグです。

そして、私が以下に書いたようにアクションクラスのプロパティ

    private File myFile;
         private String myFileContentType;
         private String myFileFileName;

        /**
         * @return the myFile
         */
        public File getMyFile() {
            return myFile;
        }
        /**
         * @return the myFileContentType
         */
        public String getMyFileContentType() {
            return myFileContentType;
        }
        /**
         * @return the myFileFileName
         */
        public String getMyFileFileName() {
            return myFileFileName;
        }
        /**
         * @param myFile the myFile to set
         */
        public void setMyFile(File myFile) {
            this.myFile = myFile;
        }
        /**
         * @param myFileContentType the myFileContentType to set
         */
        public void setMyFileContentType(String myFileContentType) {
            this.myFileContentType = myFileContentType;
        }
        /**
         * @param myFileFileName the myFileFileName to set
         */
        public void setMyFileFileName(String myFileFileName) {
            this.myFileFileName = myFileFileName;
        }

and In execute method insert this code



      String destaddress=getText("ipaddress");
           if(myFile!=null){    
            File destDir= new File(destaddress+myFileFileName);
                    destDir.createNewFile();
                FileUtils.copyFile(myFile, destDir);
         }

you add ip address in apllication properties file as show below

ipaddress=\\\\192.168.105.68\\Shared2\\Files\\

and the following interceptors to your struts. XML file

    <interceptor-ref name="fileUpload">
                  <param name="maximumSize">52428800</param>
                </interceptor-ref>
                <interceptor-ref name="basicStack"/>

ipaddress=\\\\192.168.105.68\\Shared2\\Files\\

これで問題は解決します。

于 2012-05-07T06:44:18.137 に答える
0
FileUtils is not part of the standard JDK, it a class in the Apache Commons IO library.It contains Methods like

FileUtils.readFileToString(file);
FileUtils.copyDirectoryToDirectory(source, destination);
FileUtils.deleteDirectory(source);......................which make copying files from one directory to other directory in most easier way in word it reduces voluminous amount of code as entire code is written by Apache people
please refer this URL for reference http://www.koders.com/java/fid002DB6BD79CABA7B6AA0F2669061424E3B9776D3.aspx
于 2012-05-07T09:26:16.827 に答える