In response to business needs, we need to pack the printed pdf into compression packages, and find many compression tools on the Internet, and finally find a suitable use. (multiple pdf packages)
Paste the code directly:
package com.iShift.kachaexport.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZIPUtil { public static String createZipFile(ArrayList<String> fileList, String zipFileName) { //Building Compressed File File zipFile = new File(zipFileName); //Initialization of ZIP Stream ZipOutputStream out = null; try{ //Building ZIP Stream Objects out = new ZipOutputStream(new FileOutputStream(zipFile)); //Cyclic processing of incoming collections for(int i = 0; i < fileList.size(); i++){ //Get the target file File inFile = new File(fileList.get(i)); if(inFile.exists()){ //Define ZipEntry objects ZipEntry entry = new ZipEntry(inFile.getName()); //Give ZIP Stream Object Properties out.putNextEntry(entry); int len = 0 ; //buffer byte[] buffer = new byte[1024]; //Building FileInputStream stream objects FileInputStream fis; fis = new FileInputStream(inFile); while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); out.flush(); } //Close closeEntry out.closeEntry(); //Close FileInputStream fis.close(); } } }catch (IOException e) { e.printStackTrace(); }finally{ try { //Finally close the ZIP stream out.close(); } catch (IOException e) { e.printStackTrace(); } } return zipFileName; } }
Process description: divide several filenames into "pdf" and get the address of PDF respectively.
Add the address to the list set, compress it with the toolkit, and upload it to 7 bulls.
Return the download address and return to the front end.
Service Impl layer:
@Override public String exportStudentPdfZip(String[] us, int subjectId, String beginDate, String endDate, Integer practise) throws Exception { ArrayList<String> list = new ArrayList<>(); String uuid = UUIDUtil.genUUID(); String filePath = Constants.TEMP_FILE_PATH + "/" + uuid + ".zip"; for (String userIdStr : us) { Long userId = DataTypeUtil.toLong(userIdStr); String path = productDateRangPdfA41(userId, subjectId, beginDate, endDate, practise); list.add(path); } ZIPUtil.createZipFile(list, filePath); byte[] file2byte = file2byte(new File(filePath)); storeToQiniu(file2byte, uuid + ".zip"); return Constants.FILE_SERVER + uuid + ".zip"; }
Controller layer:
@RequestMapping("/exportStudentPdf") public Response<?> exportStudentPdf(@RequestParam(value="userIds")String userIds, @RequestParam(value="beginDate")String beginDate, @RequestParam(value="endDate")String endDate , @RequestParam(value="subjectId")int subjectId, @RequestParam(value="practise", required=false, defaultValue="1")Integer practise){ try { String[] us = userIds.split(","); ArrayList<String> list = new ArrayList<String>(); StudentExport studentExport = new StudentExport(); int fileType = 0; String path = null; if (us.length>1) { fileType = 1; path = wrongBookService.exportStudentPdfZip(us, subjectId, beginDate, endDate, practise); } else { Long userId = DataTypeUtil.toLong(userIds); path = wrongBookService.productDateRangPdfA4(userId, subjectId, beginDate, endDate, practise); } studentExport.setDownloadLink(path + "?attname="); studentExport.setFileLink(path); studentExport.setFileType(fileType); return Response.success("Success!", studentExport); } catch (Exception e) { logger.error("", e); return Response.error(e); } }
StudentExport class:
package com.iShift.kachaexport.entry.vo; public class StudentExport { private int FileType; //1: Compressed Packet 0:pdf private String FileLink; private String DownloadLink; public int getFileType() { return FileType; } public void setFileType(int fileType) { FileType = fileType; } public String getFileLink() { return FileLink; } public void setFileLink(String fileLink) { FileLink = fileLink; } public String getDownloadLink() { return DownloadLink; } public void setDownloadLink(String downloadLink) { DownloadLink = downloadLink; } }
Upload to 7 cattle:
public void storeToQiniu(byte[] bs, String relativePath) throws Exception { // Construct a configuration class with a specified Zone object Configuration cfg = new Configuration(Zone.zone2()); // ... other parameter reference class annotations _____________ UploadManager uploadManager = new UploadManager(cfg); // ... Generate upload credentials and prepare for upload // If the default key is not specified, the hash value of the file content is used as the file name. String key = relativePath; // byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8"); ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs); Auth auth = Auth.create(Constants.ACCESS_KEY, Constants.SECRET_KEY); String upToken = auth.uploadToken(Constants.QINIU_BUCKET); Response response = uploadManager.put(byteInputStream, key, upToken, null, null); // Analysis of successful upload results DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); if (!relativePath.equals(putRet.key)) { throw new Exception("Error uploading file! Looking forward to the document:" + relativePath + " Return path:" + putRet.key); } } private static byte[] file2byte(File file) { byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }