ie浏览器打开aspx文件乱码_ie浏览器下载文件时文件名乱码

做一个文件下载功能时,用ie浏览器下载时文件名乱码,火狐和谷歌正常,修改后ie显示正常,修改方法如下:

@RequestMapping(value = "fileDownload", method = { RequestMethod.GET })

@ResponseBody

public void fileDownload(String filepath,HttpServletResponse response,HttpServletRequest request) {

File file = new File(filepath);

String filename;

try {

int n=filepath.lastIndexOf("/");

int m = filepath.lastIndexOf("\\");

int nn = n>m?n:m;

filename = filepath.substring(nn+1);

// 获得请求头中的User-Agent

String agent = request.getHeader("User-Agent").toUpperCase();

//解决ie下载时文件名乱码的问题

if (agent.contains("MSIE") || agent.contains("TRIDENT") || agent.contains("EDGE")) {   //判断是否是ie浏览器

filename = URLEncoder.encode(filename, "utf-8");

}else{

filename = new String(filename.getBytes(), "iso-8859-1");

}

response.setContentType("application/octet-stream");

response.addHeader("Content-Disposition", "attachment; filename="+filename);

FileInputStream fileInputStream = new FileInputStream(file);

byte[] by = new byte[fileInputStream.available()];

fileInputStream.read(by);

OutputStream outputStream = response.getOutputStream();

outputStream.write(by);

fileInputStream.close();

outputStream.close();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值