首页 > 编程语言 > 详细

java 文件下载

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

@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 文件下载

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

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