您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

文件打包zip 下载

来源:互联网 浏览:110次 时间:2023-04-08
/*** 可用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"); //获取浏览器名(IE/Chome/firefox) String userAgent = request.getHeader("User-Agent").toUpperCase(); if (userAgent.contains("CHROME")) { // 谷歌 packageName = new String(packageName.getBytes("UTF-8"), "ISO8859-1"); } else if ((userAgent.contains("FIREFOX"))) { // firefox浏览器 packageName = new String(packageName.getBytes("UTF-8"), "ISO8859-1"); } else if (userAgent.contains("MSIE")) { // IE浏览器 packageName = URLEncoder.encode(packageName, "UTF-8");// IE浏览器// packageName = new String(packageName.getBytes("ISO8859-1"), "UTF-8"); }else if (userAgent.contains("TRIDENT")){ // ie packageName = URLEncoder.encode(packageName, "UTF-8");// IE浏览器 } // 空格变 "+" 处理// packageName = URLEncoder.encode(prefix, "utf-8").replace("+","%20"); 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(); } 87071383