下载文件chrome和火狐文件名乱码

public static boolean isMSIE(){
return  request.getHeader( "USER-AGENT" ).toLowerCase().indexOf( "msie" ) >  0  ?  true  :  false ;
}



public void downLoadFile(InputStream ins, String fileName){
//1.判断ie
boolean isMSIE = isMSIE();
//2.文件名处理
if (isMSIE){// IE
fileName = URLEncoder.encode(fileName, "utf-8");
fileName = fileName.replace("+", "%20");// 处理空格变“+”的问题
} else {// FF
fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
}
//3.准备响应头
//这里filename用“"”英文的双引号扣起来,可以防止firefox空格节点文件名的问题
response.setHeader("content-disposition", "attachment;filename=\"" + fileName + "\"");
response.setContentType("application/octet-stream");
//4.下载,注意IO异常的处理
os = response.getOutputStream();
byte [] cache = new btye[1024];
int length = ins.read(cache);
while (length != -1){
os.wirt(cache,0,length);
length = ins.read(cache);
}

}




chrome  火狐

  OutputStream os = null;
        BufferedOutputStream bos = null;
        try {
            os = response.getOutputStream();
            bos = new BufferedOutputStream(os);
            workbook.write(bos);
            // 对文件名进行编码处理中文问题
            String fileNameEnc = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
            // inline在浏览器中直接显示,不提示用户下载
            // attachment弹出对话框,提示用户进行下载保存本地
            // 默认为inline方式
            response.setHeader("Content-Disposition", "attachment; filename=" + fileNameEnc);
            return true;
        } catch (IOException e) {
            LOG.error(FormatService.logFormat("downloadExcelFile fail."), e);
        } finally {
            // 特别重要
            // 1. 进行关闭是为了释放资源
            // 2. 进行关闭会自动执行flush方法清空缓冲区内容
            if (null != bos) {
                bos.close();
                bos = null;
            }
            if (null != os) {
                os.close();
                os = null;
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值