当前位置:   article > 正文

【日常笔记】java文件下载返回数据流形式

java文件下载返回文件流
    @RequestMapping("/downloadFile")
    @ResponseBody
    public void download(String uploadPathUrl, HttpServletRequest request,    HttpServletResponse resp) throws Exception {
        
        //获取服务器绝对路径  这里获取的是配置文件中所配置的地址
        String path =PropertiesUtil.getInstance().getSysPro("uploadPath")+"//"+uploadPathUrl;
        log.info("下载路径:"+path);
        // 输出响应正文的输出流
        OutputStream out;
        // 读取本地文件的输入流
        InputStream in;
        // 获得本地输入流
        File file = new File(path);
        in = new FileInputStream(file);
        // 设置响应正文的MIME类型
        resp.setContentType("Content-Disposition;charset=GB2312");
        resp.setHeader("Content-Disposition", "attachment;" + " filename="+ new String(path.getBytes(), "ISO-8859-1"));
        // 把本地文件发送给客户端
        out = resp.getOutputStream();
        int byteRead = 0;
        byte[] buffer = new byte[512];
        while ((byteRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, byteRead);
        }
        in.close();
        out.flush();
        out.close();
    }

日常笔记 文件下载

转载于:https://www.cnblogs.com/miskis/p/5505225.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/207143
推荐阅读
相关标签
  

闽ICP备14008679号