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

    

原文地址:https://www.cnblogs.com/feifeiyun/p/6635785.html