首页 > 编程语言 > 详细

java 文件下载支持中文名称

时间:2019-09-29 18:26:34      阅读:63      评论:0      收藏:0      [点我收藏+]
  /**
     * 文件下载
     * @param filePath  文件路径
     * @param fileName  文件名称
     */
    public void download(String filePath,String fileName){
        try {
       //支持中文
            fileName = URLEncoder.encode(fileName,"UTF-8");
            HttpServletResponse response = ServletActionContext.getResponse();
            HttpServletRequest request = ServletActionContext.getRequest();
            response.reset();
            response.setContentType(request.getServletContext().getMimeType(fileName));  
            response.setHeader("Content-Disposition", "attachment;filename="+fileName); 
            InputStream in = new FileInputStream(filePath);  
            OutputStream out = response.getOutputStream();  
              
            byte[] b = new byte[1024];
            int length = 0;
            while((length = in.read(b)) != -1)  {  
                out.write(b,0,length);  
            }  
            in.close();  
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

java 文件下载支持中文名称

原文:https://www.cnblogs.com/henuyuxiang/p/11609041.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!