文件下载 java

String fileName = request.getParameter("fileName");
        response.setContentType("application/OCTET-STREAM;charset=UTF-8");
        response.setHeader("Content-Dispositon", "attachment;filename="+fileName);
        String path = "E:\\file";
        File file = new File(path,fileName);
        FileInputStream fis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                bos.write(buffer,0,len);
                bos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            fis.close();
            bos.close();
        }

posted @ 2017-04-11 10:18  wss_Ben  阅读(181)  评论(0编辑  收藏  举报