各个浏览器下载乱码问题


<pre name="code" class="java">public static void downloadAttachment(AttachmentContent attachment) {

        InputStream is = null;
        OutputStream out = null;
        try {

        	HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            String fileType = attachment.getType();
            if (StringUtils.hasLength(fileType)) {
                response.setContentType(fileType);
            } else {
                response.setContentType(MimeTypeConstants.MIMETYPE_STREAM);
            }

           /* String fileName = attachment.getName();
            fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING);
            response.addHeader(Content_Disposition,
                               attachment_filename + fileName);*/
            /**
             * fileName = new String(fileName.getBytes(Constants.DEFAULT_ENCODING),"ISO8859-1");谷歌、360极速浏览器
             * fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING); IE浏览器
             * fileName = new String(fileName.getBytes("GBK"),"ISO8859-1"); IE11浏览器
             */
            String fileName = attachment.getName();
            if (RequestUtils.getRequest().getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
            	fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING);
            }else if(RequestUtils.getRequest().getHeader("User-Agent").toUpperCase().indexOf("RV:11.0") > 0){
            	fileName = new String(fileName.getBytes("GBK"),"ISO8859-1");
            } else{
            	fileName = new String(fileName.getBytes(Constants.DEFAULT_ENCODING),"ISO8859-1");
            }
            //response.addHeader(Content_Disposition,attachment_filename + fileName);
            response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName)); // 文件名外的双引号处理firefox的空格截断问题  

            out = response.getOutputStream();
            if(attachment!=null && attachment.getContent()!=null){
                is = new ByteArrayInputStream(attachment.getContent());
                IOUtils.copy(is, out);
            }

            // out.flush();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e.getMessage(), e);
        } finally {
            FileUtils.close(is);

        }
    }



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值