Spring boot 文件下載接口

@RequestMapping("/xzwj")
複製代碼
public void xzwj(HttpServletResponse res,String id) throws IOException { 
String fileName = "12345.png";
res.setHeader("content-type", "application/octet-stream");        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = res.getOutputStream();
//            bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\86188\\IdeaProjects\\sq_activity_demo\\src\\main\\resources\\"
//                    + fileName)));
            bis=new BufferedInputStream(processEngine.getTaskService().getAttachmentContent(id));//
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}複製代碼
相關文章
相關標籤/搜索