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