java 下载文件 兼容 ie 火狐 google 浏览器 ,解决下载文件乱码问题

来源:互联网 发布:同花顺mac 编辑:程序博客网 时间:2024/05/29 08:58
public static void downloadAttachment(HttpServletResponse response,HttpServletRequest request ,
            String filePath, String saveFileName) {
        response.reset();
        //response.setContentType("application/octet-stream;charset=utf-8");
        response.setContentType(getContentType(saveFileName));
        // response.setContentType("application/x-download");
        OutputStream outp = null;
        FileInputStream in = null;
        try {
          
            response.addHeader("Content-Disposition", "attachment;filename=\""+ encodeFilename(request,saveFileName)+"\"");//名称两边的双引号不能省略 兼容火狐 文件名中的空格
            outp = response.getOutputStream();
            in = new FileInputStream(filePath);
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = in.read(b)) > 0) {
                outp.write(b, 0, i);
            }
            outp.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
            if (outp != null) {
                try {
                    outp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                outp = null;
            }
        }
    }
    private static String encodeFilename(HttpServletRequest request,String fileName) throws UnsupportedEncodingException  {
          String agent =request.getHeader("USER-AGENT");
    
          try {
                 // IE
                  if (null != agent && -1 != agent.indexOf("MSIE")) {
                            fileName = URLEncoder.encode(fileName, "UTF-8");
                   // Firefox
                   } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
                           fileName =  new String( fileName.getBytes("UTF-8"), "ISO-8859-1" );
                   }
          } catch (UnsupportedEncodingException e) {
               try {
                         fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
               } catch (UnsupportedEncodingException e1) {
                         e1.printStackTrace();
               }
               e.printStackTrace();
          }
          return fileName;

         }


    private static String getContentType(String fileName) {
        String ext = FileUtils.getExtension(fileName).toLowerCase();
        if (ext.equals(".zip")) {
            return "application/zip";
        } else if (ext.equals(".xls") || ext.equals(".xlsx")) {
            return "application/x-excel";
        } else if (ext.equals(".doc") || ext.equals(".docx")) {
            return "application/msword";
        } else if (ext.equals(".pdf")) {
            return "application/pdf";
        } else if (ext.equals(".jpg") || ext.equals(".jpeg")) {
            return "image/jpeg";
        } else if (ext.equals(".gif")) {
            return "image/gif";
        } else if (ext.equals(".png")) {
            return "image/png";
        }else if(ext.equals(".bmp")){
            return "image/bmp";
        }
        return "application/force-download";
    }

原创粉丝点击