1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package com.hrp.util;
import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.util.GraphicsRenderingHints; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.*; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
@Component public class ImageUtils {
public static final String FORMAT_NAME = "png";
public static final String PNG_SUFFIX = ".png";
public static final String ZIP_SUFFIX = ".zip";
public static void pdfToImage(MultipartFile file, HttpServletResponse response) throws Exception { File zipFile = generateImageFile(file); downloadZipFile(zipFile, response); }
private static File generateImageFile(MultipartFile file) throws Exception { String fileName = file.getOriginalFilename(); Document document = new Document(); document.setByteArray(file.getBytes(), 0, file.getBytes().length, fileName);
List<File> fileList = new ArrayList<>(); for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, 0F, 2.5F); File imageFile = new File((i + 1) + PNG_SUFFIX); ImageIO.write(image, FORMAT_NAME, imageFile); image.flush(); fileList.add(imageFile); } document.dispose();
String directoryName = fileName.substring(0, fileName.lastIndexOf(".")); File zipFile = new File(directoryName + ZIP_SUFFIX); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); zipFile(fileList, zos); zos.close(); return zipFile; }
private static void downloadZipFile(File zipFile, HttpServletResponse response) throws IOException { FileInputStream fis = new FileInputStream(zipFile); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close();
response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8")); OutputStream out = response.getOutputStream(); out.write(bytes); out.flush(); out.close(); zipFile.delete(); }
private static void zipFile(List<File> inputFiles, ZipOutputStream zos) throws IOException { byte[] buffer = new byte[1024]; for (File file : inputFiles) { if (file.exists()) { if (file.isFile()) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); zos.putNextEntry(new ZipEntry(file.getName())); int size = 0; while ((size = bis.read(buffer)) > 0) { zos.write(buffer, 0, size); } zos.closeEntry(); bis.close(); file.delete(); } else { File[] files = file.listFiles(); List<File> childrenFileList = Arrays.asList(files); zipFile(childrenFileList, zos); } } } }
}
|