首页 > 编程语言 > 详细

java后台下载方法

时间:2019-09-24 17:24:29      阅读:193      评论:0      收藏:0      [点我收藏+]
    public void fileDownload() throws UnsupportedEncodingException {
        String fileidd= this.getParameter("获取ID");
        SysFile sysFile = sysFileManager.getById(根据id(fileidd)查询);
        File file = new File(AppUtil.getAppAbsolutePath()+sysFile.getPicpath()//获取路径);
        if (file.exists()) {
            this.getResponse().setContentType("application/force-download");// 设置强制下载不打开
            this.getResponse().addHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(sysFile.getFilename()//获取文件名, "UTF-8"));// 设置文件名

            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = this.getResponse().getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("success");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

 

java后台下载方法

原文:https://www.cnblogs.com/yanchaohui/p/11579392.html

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