struts2中的文件上传与下载

3.struts2中文件上传与下载

    1.上传
        浏览器端:
            1.method=post
            2.<input type="file" name="xx">
            3.encType="multipart/form-data";
            
        服务器端:
            commons-fileupload组件
            1.DiskFileItemFactory
            2.ServletFileUpload
            3.FileItem
        
        struts2中文件上传:
            默认情况下struts2框架使用的就是commons-fileupload组件.
            struts2它使用了一个interceptor帮助我们完成文件上传操作。
             <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
            
        在action中怎样处理文件上传?
            页面上组件:<input type="file" name="upload">
            
            在action中要有三个属性:
                private File upload;
                private String uploadContentType;
                private String uploadFileName;
                
            在execute方法中使用commons-io包下的FileUtils完成文件复制.            
                FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
                
        ------------------------------------------------------------------------
        关于struts2中文件上传细节:
            1.关于控制文件上传大小
                在default.properties文件中定义了文件上传大小
                struts.multipart.maxSize=2097152 上传文件默认的总大小 2m
            
            2.在struts2中默认使用的是commons-fileupload进行文件上传。
                # struts.multipart.parser=cos
                # struts.multipart.parser=pell
                struts.multipart.parser=jakarta
                
                如果使用pell,cos进行文件上传,必须导入其jar包.
            
            3.如果出现问题,需要配置input视图,在页面上可以通过<s:actionerror>展示错误信息.
             问题:在页面上展示的信息,全是英文,要想展示中文,国际化
            
                struts-messages.properties 文件里预定义 上传错误信息,通过覆盖对应key 显示中文信息
                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}

                修改为
                    struts.messages.error.uploading=上传错误: {0}
                    struts.messages.error.file.too.large=上传文件太大: {0} "{1}" "{2}" {3}
                    struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3}
                    struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3}
                    
                {0}:<input type=“file” name=“uploadImage”>中name属性的值
                {1}:上传文件的真实名称
                {2}:上传文件保存到临时目录的名称
                {3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
    

            4.关于多文件上传时的每个上传文件大小控制以及上传文件类型控制.
                
                1.多文件上传
                    服务器端:
                        只需要将action属性声明成List集合或数组就可以。
                        
                        private List<File> upload;
                        private List<String> uploadContentType;
                        private List<String> uploadFileName;
                        
                2.怎样控制每一个上传文件的大小以及上传文件的类型?
                    在fileupload拦截器中,通过其属性进行控制.
                    
                    maximumSize---每一个上传文件大小
                    allowedTypes--允许上传文件的mimeType类型.
                    allowedExtensions--允许上传文件的后缀名.
                
                    <interceptor-ref name="defaultStack">
                        <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
                    </interceptor-ref>
    ----------------------------------------------------------------------------------------------                
    2.下载
        文件下载方式:
            1.超连接
            2.服务器编码,通过流向客户端写回。
                
                1.通过response设置  response.setContentType(String mimetype);
                2.通过response设置  response.setHeader("Content-disposition;filename=xxx");
                3.通过response获取流,将要下载的信息写出。
                
                
                
        struts2中文件下载:        
            通过<result type="stream">完成。
            
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            在StreamResult类中有三个属性:
                  protected String contentType = "text/plain"; //用于设置下载文件的mimeType类型
                  protected String contentDisposition = "inline";//用于设置进行下载操作以及下载文件的名称
                  protected InputStream inputStream; //用于读取要下载的文件。
            
            在action类中定义一个方法
                public InputStream getInputStream() throws FileNotFoundException {
                    FileInputStream fis = new FileInputStream("d:/upload/" + filename);
                    return fis;
                }
            
            <result type="stream">
                <param name="contentType">text/plain</param>
                <param name="contentDisposition">attachment;filename=a.txt</param>
                <param name="inputStream">${inputStream}</param> 会调用当前action中的getInputStream方法。
            </result>
        
            
            问题1:<a href="${pageContext.request.contextPath}/download?filename=捕获.png">捕获.png</a>下载报错
                原因:超连接是get请求,并且下载的文件是中文名称,乱码。
                
        
            问题2:下载捕获文件时,文件名称就是a.txt    ,下载文件后缀名是png,而我们在配置文件中规定就是txt?            
                <result type="stream">
                    <param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
                    <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
                    <param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
                </result>
                
        在struts2中进行下载时,如果使用<result type="stream">它有缺陷,例如:下载点击后,取消下载,服务器端会产生异常。
            在开发中,解决方案:可以下载一个struts2下载操作的插件,它解决了stream问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值