qq_22118963
ChenDaShu大叔
2016-01-14 02:29

文件后缀名乱码怎么解决..

10
  • 需求
  • download
  • java
  • 乱码
  • 奇葩

今天遇到一个奇葩需求,文件后缀名是中文的.
例子:文件名.后缀名
文件名不会乱码,但是后缀名会乱码,求大神指点..
后台是java的.
而且就ie8不行,使用其他浏览器,以及高版本ie显示正常.

 @RequestMapping("/download")
    public HttpServletResponse download(String fileName, HttpServletRequest request,
            HttpServletResponse response,String fileUrl) throws Exception {
        try {
            //获得文件
            File file = new File(fileUrl);

            //获取文件名
            fileName = URLDecoder.decode(fileName,"utf-8");

            //获得文件后缀名
            String ext = fileName.substring(fileName.indexOf(".")+1).toUpperCase();

            //以流的形式下载文件
            InputStream is = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();

            //设置response header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

图片说明图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐