|
@@ -0,0 +1,232 @@
|
|
|
|
+package org.springblade.archive.utils;
|
|
|
|
+
|
|
|
|
+import com.itextpdf.text.Document;
|
|
|
|
+import com.itextpdf.text.pdf.PdfCopy;
|
|
|
|
+import com.itextpdf.text.pdf.PdfReader;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
+import org.apache.poi.util.Units;
|
|
|
|
+import org.springblade.common.utils.CommonUtil;
|
|
|
|
+import org.springblade.common.vo.DataVO;
|
|
|
|
+import org.springblade.core.tool.utils.IoUtil;
|
|
|
|
+
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.awt.*;
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
|
+
|
|
|
|
+public class FileUtils {
|
|
|
|
+
|
|
|
|
+ public static void batchDownloadFileToZip(List<String> urls, HttpServletResponse response){
|
|
|
|
+ // 设置压缩流:直接写入response,实现边压缩边下载
|
|
|
|
+ ZipOutputStream zipos = null;
|
|
|
|
+ // 循环将文件写入压缩流
|
|
|
|
+ DataOutputStream os = null;
|
|
|
|
+ try{
|
|
|
|
+
|
|
|
|
+ // 响应头的设置
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
|
+ // 设置压缩包的名字
|
|
|
|
+ // 解决不同浏览器压缩包名字含有中文时乱码的问题
|
|
|
|
+ String downloadName = "附件-" + System.currentTimeMillis() + ".zip";
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(downloadName, "UTF-8"));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
|
|
|
|
+ // 设置压缩方法
|
|
|
|
+ zipos.setMethod(ZipOutputStream.DEFLATED);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(zipos == null){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+
|
|
|
|
+ for(String url : urls){
|
|
|
|
+ if(StringUtils.isNotEmpty(url)){
|
|
|
|
+ try{
|
|
|
|
+ String fileName = null, symbol = "";
|
|
|
|
+ if(url.contains("@@@")){
|
|
|
|
+ String[] array = url.split("@@@");
|
|
|
|
+ url = array[0];
|
|
|
|
+ fileName = array[1];
|
|
|
|
+ symbol = url.substring(url.lastIndexOf("."));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取文件流
|
|
|
|
+ inputStream = CommonUtil.getOSSInputStream(url);
|
|
|
|
+ //转换
|
|
|
|
+ byte[] bytes = CommonUtil.InputStreamToBytes(inputStream);
|
|
|
|
+
|
|
|
|
+ if(StringUtils.isEmpty(fileName)){
|
|
|
|
+ fileName = url.substring(url.lastIndexOf("/") + 1);
|
|
|
|
+ symbol = "";
|
|
|
|
+ }
|
|
|
|
+ zipos.putNextEntry(new ZipEntry(fileName + symbol));
|
|
|
|
+
|
|
|
|
+ os = new DataOutputStream(zipos);
|
|
|
|
+ os.write(bytes);
|
|
|
|
+ zipos.closeEntry();
|
|
|
|
+
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ IoUtil.closeQuietly(inputStream);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ IoUtil.closeQuietly(os);
|
|
|
|
+ IoUtil.closeQuietly(zipos);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 压缩图片
|
|
|
|
+ */
|
|
|
|
+ public static byte[] resizeImage(byte[] srcImgData, float reduceMultiple) throws IOException {
|
|
|
|
+ BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
|
|
|
|
+ /*int width = (int) (bi.getWidth() * reduceMultiple);
|
|
|
|
+ int height = (int) (bi.getHeight() * reduceMultiple);*/
|
|
|
|
+ int width = 600;
|
|
|
|
+ int height = 800;
|
|
|
|
+ Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
+ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ Graphics g = tag.getGraphics();
|
|
|
|
+ g.setColor(Color.RED);
|
|
|
|
+ g.drawImage(image, 0, 0, null);
|
|
|
|
+ g.dispose();
|
|
|
|
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
|
|
+ ImageIO.write(tag, "JPEG", bOut);
|
|
|
|
+ return bOut.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 压缩图片2
|
|
|
|
+ */
|
|
|
|
+ public static byte[] resizeImage2(byte[] srcImgData, float reduceMultiple) throws IOException {
|
|
|
|
+ BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
|
|
|
|
+ int width = (int) (bi.getWidth() * reduceMultiple);
|
|
|
|
+ int height = (int) (bi.getHeight() * reduceMultiple);
|
|
|
|
+ Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
+ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ Graphics g = tag.getGraphics();
|
|
|
|
+ g.setColor(Color.RED);
|
|
|
|
+ g.drawImage(image, 0, 0, null);
|
|
|
|
+ g.dispose();
|
|
|
|
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
|
|
+ ImageIO.write(tag, "JPEG", bOut);
|
|
|
|
+ return bOut.toByteArray();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 图片定位
|
|
|
|
+ */
|
|
|
|
+ public static void imageOrientation(Sheet sheet, ClientAnchor anchor, DataVO dataVO) {
|
|
|
|
+ anchor.setDx1(Units.pixelToEMU(5));
|
|
|
|
+ anchor.setDy1(Units.pixelToEMU(5));
|
|
|
|
+ anchor.setCol2(anchor.getCol1());
|
|
|
|
+ anchor.setRow2(anchor.getRow1());
|
|
|
|
+ int k = getMergedRegionIndex(sheet, CommonUtil.join(dataVO.getX(), dataVO.getY(), dataVO.getX(), dataVO.getY(),","));
|
|
|
|
+ if(k>-1){
|
|
|
|
+ /*如果是合并单元格,则锚点第二坐标设置为合并区右下角单元格坐标*/
|
|
|
|
+ CellRangeAddress ca = sheet.getMergedRegion(k);
|
|
|
|
+ anchor.setCol1(ca.getFirstColumn());
|
|
|
|
+ anchor.setRow1(ca.getFirstRow());
|
|
|
|
+ anchor.setCol2(ca.getLastColumn());
|
|
|
|
+ anchor.setRow2(ca.getLastRow());
|
|
|
|
+ }
|
|
|
|
+ int dx=(int)(sheet.getColumnWidthInPixels(anchor.getCol2())+3);
|
|
|
|
+ int dy=Units.pointsToPixel(sheet.getRow(anchor.getRow2()).getHeightInPoints())-5;
|
|
|
|
+ anchor.setDx2(Units.pixelToEMU(dx));
|
|
|
|
+ anchor.setDy2(Units.pixelToEMU(dy));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int getMergedRegionIndex(Sheet sheet, String coords) {
|
|
|
|
+ for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
|
|
|
|
+ CellRangeAddress ca = sheet.getMergedRegion(i);
|
|
|
|
+ int firstColumn = ca.getFirstColumn();
|
|
|
|
+ int lastColumn = ca.getLastColumn();
|
|
|
|
+ int firstRow = ca.getFirstRow();
|
|
|
|
+ int lastRow = ca.getLastRow();
|
|
|
|
+ Matcher mu = CommonUtil.matcher("(\\d{1,3}),(\\d{1,3}),(\\d{1,3}),(\\d{1,3})",coords);
|
|
|
|
+ /*合并单元格四个角坐标,只要任意一个在落在将要合并的区域里面意味着两者重合,就必须拆分前者*/
|
|
|
|
+ List<Integer[]> corners = Arrays.asList(new Integer[]{firstColumn,firstRow},new Integer[]{lastColumn,firstRow},new Integer[]{firstColumn,lastRow},new Integer[]{lastColumn,lastRow});
|
|
|
|
+ if(mu.find()){
|
|
|
|
+ int firstColumn2 = mu.group(1) == null ? 0 : Integer.parseInt(mu.group(1));
|
|
|
|
+ int lastColumn2 = mu.group(3) == null ? 0 : Integer.parseInt(mu.group(3));
|
|
|
|
+ int firstRow2 = mu.group(2) == null ? 0 : Integer.parseInt(mu.group(2));
|
|
|
|
+ int lastRow2 = mu.group(4) == null ? 0 : Integer.parseInt(mu.group(4));
|
|
|
|
+ for(Integer[] corner:corners){
|
|
|
|
+ if(firstColumn2<=corner[0]&&corner[0]<=lastColumn2&&firstRow2<=corner[1]&&corner[1]<=lastRow2){
|
|
|
|
+ return i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 合并方法
|
|
|
|
+ */
|
|
|
|
+ public static void mergePdfPublicMethods(List<String> urlList, String localImgUrl){
|
|
|
|
+ PdfReader reader = null;
|
|
|
|
+
|
|
|
|
+ Document doc = new Document();
|
|
|
|
+ PdfCopy pdfCopy = null;
|
|
|
|
+ try{
|
|
|
|
+ pdfCopy = new PdfCopy(doc, new FileOutputStream(localImgUrl));
|
|
|
|
+ int pageCount;
|
|
|
|
+ doc.open();
|
|
|
|
+
|
|
|
|
+ for(String urlStr : urlList){
|
|
|
|
+ try{
|
|
|
|
+ //获取OSS文件输入流
|
|
|
|
+ reader = new PdfReader(CommonUtil.getOSSInputStream(urlStr));
|
|
|
|
+
|
|
|
|
+ pageCount = reader.getNumberOfPages();
|
|
|
|
+
|
|
|
|
+ for(int i = 0; i < pageCount; ++i){
|
|
|
|
+ int is = i + 1;
|
|
|
|
+ pdfCopy.addPage(pdfCopy.getImportedPage(reader,is));
|
|
|
|
+ }
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if(reader != null){
|
|
|
|
+ reader.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if(pdfCopy != null){
|
|
|
|
+ pdfCopy.flush();
|
|
|
|
+ pdfCopy.close();
|
|
|
|
+ }
|
|
|
|
+ doc.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|