SSH-文件的上傳與下載


主體的工作還是由struts2完成

1.ssh整合的

  >導入必要的jar包

  >spring中配置hibernate的sessionFactory的bean,並將service包中的類的方法聲明成事務

  >web.xml中配置contextListener(監聽到web啟動),這樣web應用啟動就自動創建spring的applicationContext並放入到application域中
2.文件上傳部分(2個問題)

  >表單使用post方式提交,並且enctype="multipart/form-data",表單項使用<s:file/>。

  >在Action類中定義屬性

    [File Name] : File - File類型,屬性名為<s:file/>中的name。是上傳文件在服務器的一個臨時文件

    [File Name]ContentType : String - String類型,屬性名為<s:file/>中的name加上ContentType。為上傳文件的類型描述

    [File Name]FileName : String - String類型,屬性名為<s:file/>中的name加FileName。為上傳文件的名字

    在對應action方法中就可以得到這三個屬性了

  >對上傳文件的限制

    單個文件類型,大小,后綴的限制(實質為修改FileUploadInterceptor攔截器的屬性)

    

        <interceptors>
            <interceptor-stack name="myStack">
                <interceptor-ref name="paramsPrepareParamsStack">
                    <!-- 單個文件的最大大小 -->
                    <param name="fileUpload.maximumSize">10485760</param>
                    <!-- 允許的上傳類型<param name="fileUpload.allowedTypes"></param> -->
                    <!-- 允許上傳的文件擴展名<param name="fileUpload.allowedExtensions"></param> -->
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="myStack"></default-interceptor-ref>

    多個文件上傳時,總的大小的限制(實質為修改struts2的常量,默認配置在default.properties中)

    (這里有兩個問題:1.若上傳的文件超出這個值太多,就無法顯示頁面;2.怎么替換默認的超出提示,為actionErrors)

        <!-- 當多個文件一塊上傳時文件總的大小是104857600字節,即100M.若上傳的文件比這個數字大太多則無法顯示頁面 -->
    <!-- -1為不限制 -->
    <!-- <constant name="struts.multipart.maxSize" value="104857600"></constant> -->
    <constant name="struts.multipart.maxSize" value="-1"></constant>

  >單個文件上傳出錯,替換默認的錯誤提示消息(默認的配置在default-message.properties中,可以參考)

  在國際化資源文件中

struts.messages.error.uploading=~Error uploading: {0}
struts.messages.error.file.too.large=~The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=~Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=~File extension not allowed: {0} "{1}" "{2}" {3}

3.怎么在struts2中配置國際化資源文件(實質為修改struts2的常量,默認配置在default.properties中,可以參考)

  

    <!-- default.properties中的鍵值對是struts的常量的配置,可以使用<constant name="" value=""></constant>修改 -->

    <!-- 配置國際化資源文件 -->
    <constant name="struts.custom.i18n.resources" value="i18n"></constant>

4.怎么下載文件(1個問題)

  參考說明文檔struts-2.3.15.3/docs/WW/docs/stream-result.html

  >下載請求經過action攔截到達一個stream類型的result,可以在這個result中添加參數,或在Action方法中動態指定即可。不需要再提供jsp頁面

    contentType - 文件的內容類型(default = text/plain).

    contentLength - the stream length in bytes (the browser displays a progress bar).可以不指定

    contentDisposition - the content disposition header value for specifing the file name

            (default = inline, values are typically attachment;filename="document.pdf".) document.pdf就是下載時文件的名字(問題:中文會不顯示)

    inputName - 指定Action類中輸入流的名字 (default = inputStream).

    bufferSize - the size of the buffer to copy from input to output (default = 1024).緩存的大小

    allowCaching - 是否使用緩存 (default = true)

    contentCharSet - if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression,               the result of evaluating the expression will be used. If not set, then no charset will be set on the header

    

       <result name="success" type="stream">
                <param name="inputName">fileStream</param>
                <param name="bufferSize">1024</param>
            </result>

    其他參數可在action方法中動態指定,但要作為Action類的屬性,並提供geter方法

    private String contentType;
    private String contentDisposition;
    private InputStream fileStream;

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContentDisposition() {
        return contentDisposition;
    }

    public void setContentDisposition(String contentDisposition) {
        this.contentDisposition = contentDisposition;
    }

    public InputStream getFileStream() {
        return fileStream;
    }

    public void setFileStream(InputStream fileStream) {
        this.fileStream = fileStream;
    }
    public String downLoad() throws Exception {
        MyFile downLoadFile = myService.downLoadMyFile(fileId);
        fileStream = new FileInputStream(
                new File(ServletActionContext.getServletContext().getRealPath(downLoadFile.getAddress())));
        contentType = downLoadFile.getContentType();
        contentDisposition = "attachment;filename=" + downLoadFile.getName();
        return SUCCESS;
    }

 

 

5.怎么得到web應用的某文件的真實路徑(因為web應用編譯好以后可以被移動,所以存文件時不能用絕對路徑)

  ServletContext servletContext = ServletActionContext.getServletContext();
  String fileStoreName = servletContext.getRealPath("/WEB-INF/files/" + fileFileName);

 

 

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM