2012-05-07 26 views
0

私はStruts2 WEBアプリケーションでExcelのみのファイルをアップロードしようとしています。ファイルの内容を表示したresponse.jspページで Excelの実行方法struts2でファイルをアップロードするには?

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

と次のようにタイプです::だからJSPページに次のコードを使用してStruts.xmlで

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

を:

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

を、私はこの作業を行うには新しいです。

+0

このstrutsファイルのアップロード[docs](http://struts.apache.org/2.0.14/docs/file-upload.html)はまだチェックされていますか? – CoolBeans

答えて

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 
+0

アップロードファイルのリアルパスを取得するにはどうすればよいですか? – shiva

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

この下に書かれたとして、アクションクラスのプロパティ。

+0

ありがとうございました。あなたのコードを試しましたが、ブラウザで次のエラーメッセージが表示されます。私はエラーが私のクラスのファイルオブジェクトにあると思う。だから私を助けてください。 – shiva

+0

このエラーは、フォームのenctypeタイプを "multipart/form-data"に設定しなかったために発生します。 http://www.mkyong.com/struts2/struts-2-upload-multiple-files-example/ –

関連する問題