javaweb网站下载文件中文文件名乱码解决方法

来源:互联网 发布:淘宝售假怎么申诉 编辑:程序博客网 时间:2024/06/05 09:19

主流的文件下载要分两种情况

1.火狐浏览器

2.非火狐浏览器

String fileName=office.getName()+"_"+supervisionTask.getTaskName()+".zip";OutputStream out=response.getOutputStream();         // 设置为下载application/x-download  response.setContentType("application/x-download charset=UTF-8");   // 通常解决汉字乱码方法用URLEncoder.encode(...)  String filenamedisplay = URLEncoder.encode(fileName, "UTF-8");  if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox")>=0) {    // 针对火狐浏览器处理方式不一样了     filenamedisplay = new String(fileName.getBytes("UTF-8"), "iso-8859-1");  }   response.setHeader("Content-Disposition", "attachment;filename="   + filenamedisplay);  InputStream in=new FileInputStream(new File(rootFilePath+".zip"));byte[] temp=new byte[1024];int length=0;while ((length=in.read(temp))>0){ out.write(temp, 0, length);}in.close();


0 0
原创粉丝点击