文件打包zip 下载

試著忘記壹切 提交于 2020-02-25 21:44:49
/**
* 可用HTTP地址获取图片二进制 输出 
*/
@RequestMapping(value = "/test")
    @ResponseBody
    public void test(HttpServletRequest request, HttpServletResponse response) {
        String t = "http://127.0.0.1:39090/oa/orderContractFile/202001/553f0479-6e8e-4ce0-a1b5-fbee8bf0fb4b.png";

        //生成打包下载后的zip文件:Papers.zip
        String papersZipName = "Papers.zip";
        //zip文件保存路径
        String zipPath = "d:/" + papersZipName;
        try {
            @Cleanup
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
            int j = 3;
            for (int i = 0; i < 2; i++) {
                //获得下载文件完整路径
                String downloadPath = t;

                @Cleanup
                FileInputStream fis = new FileInputStream("d:/test.png");
                out.putNextEntry(new ZipEntry("测试" + ++j + ".png"));
                //写入压缩包
                HttpResponse httpResponse = HttpRequest.get(t)
                        .execute();
                byte[] bytes = httpResponse.bodyBytes();
                out.write(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/zip;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=Papers.zip");

            //开始下载
            @Cleanup
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(zipPath)));
            @Cleanup
            BufferedOutputStream outt = new BufferedOutputStream(response.getOutputStream());
            // deviceCamera/test
            byte[] buff = new byte[1024];
            int lenn = 0;
            while ((lenn = is.read(buff, 0, buff.length)) != -1) {
                outt.write(buff, 0, lenn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        File file = new File(zipPath);
        file.delete();
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!