【日常笔记】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