码迷,mamicode.com
首页 > 编程语言 > 详细

java 文件下载

时间:2017-02-27 18:53:49      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:pos   utf-8   cat   dna   auto   except   public   tty   nload   

@RequestMapping(value = "/downLoad")
    public void downLoad(HttpServletResponse response, String value,String oldName ,String dateFileName) throws ParseException{
        
        String path =uploadsDir+FOLDER+"/attach/"+dateFileName+"/"+value;
        
         File file = new File(path);

            InputStream fis;
            try {
            String filename = new String(oldName.getBytes("ISO-8859-1"),"utf-8");
                fis = new BufferedInputStream(new FileInputStream(path));
            
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(buffer);
            os.flush();
            os.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
   

java 文件下载

标签:pos   utf-8   cat   dna   auto   except   public   tty   nload   

原文地址:http://www.cnblogs.com/zhangmh/p/6475484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!