Ver Fonte

output改造

wangliang há 2 anos atrás
pai
commit
adb8e148bd

+ 0 - 18
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfFillUtils.java

@@ -176,24 +176,6 @@ public class PdfFillUtils {
         return null;
     }
 
-    public static void saveFile(ByteArrayOutputStream baos, String fileName) {
-        FileOutputStream fileOutputStream = null;
-        try {
-            fileOutputStream = new FileOutputStream(fileName);
-            fileOutputStream.write(baos.toByteArray());
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        } finally {
-            try {
-                fileOutputStream.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
     /**
      * 填充table
      *

+ 74 - 74
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -201,80 +201,80 @@ public class PdfUtil {
         return pageSizeEnum;
     }
 
-    /**
-     * 两页打印到一页(A4 -> A3)
-     *
-     * @param streamOfPDFFile 原卷
-     * @param outputStream    输出的试卷
-     * @param paginate        是否生成A3 每页页码
-     */
-    public static void concatPDFs(InputStream streamOfPDFFile, OutputStream outputStream, boolean paginate) {
-
-        Document document = new Document();
-        try {
-
-            //读取PDF
-            PdfReader pdfReader = new PdfReader(streamOfPDFFile);
-            int totalPages = pdfReader.getNumberOfPages();
-
-            //创建PDF Writer
-            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
-            document.open();
-            BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
-            PdfContentByte contentByte = writer.getDirectContent();
-
-            Rectangle rec = pdfReader.getPageSize(1);
-            //新pdf为两个宽度
-            Rectangle newRec = new Rectangle(0, 0, rec.getWidth() * 2, rec.getHeight());
-            document.setPageSize(newRec);
-
-            PdfImportedPage page;
-            PdfImportedPage page2;
-            int currentPageNumber = 0;
-
-            //舍弃奇数的最后一页, 删除该设置后面不受影响
-//            totalPages = totalPages >> 1 << 1;
-            for (int pageIndex = 0; pageIndex < totalPages; pageIndex += 2) {
-                document.newPage();
-                currentPageNumber++;
-
-                //原始第一页设置到左边
-                page = writer.getImportedPage(pdfReader, pageIndex + 1);
-                contentByte.addTemplate(page, 0, 0);
-
-                //第二页设置到右边
-                if (pageIndex + 2 <= totalPages) {
-                    page2 = writer.getImportedPage(pdfReader, pageIndex + 2);
-                    contentByte.addTemplate(page2, rec.getWidth(), 0);
-                }
-
-                //设置页码
-                if (paginate) {
-                    contentByte.beginText();
-                    contentByte.setFontAndSize(baseFont, 13);
-                    contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + SystemConstant.ORG_SPLIT + (totalPages / 2 + (totalPages % 2 == 0 ? 0 : 1)), newRec.getWidth() / 2, 17, 0);
-                    contentByte.endText();
-                }
-            }
-
-            outputStream.flush();
-            document.close();
-            outputStream.close();
-        } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
-        } finally {
-            if (document.isOpen()) {
-                document.close();
-            }
-            try {
-                if (outputStream != null) {
-                    outputStream.close();
-                }
-            } catch (IOException ioe) {
-                log.error(SystemConstant.LOG_ERROR, ioe);
-            }
-        }
-    }
+//    /**
+//     * 两页打印到一页(A4 -> A3)
+//     *
+//     * @param streamOfPDFFile 原卷
+//     * @param outputStream    输出的试卷
+//     * @param paginate        是否生成A3 每页页码
+//     */
+//    public static void concatPDFs(InputStream streamOfPDFFile, OutputStream outputStream, boolean paginate) {
+//
+//        Document document = new Document();
+//        try {
+//
+//            //读取PDF
+//            PdfReader pdfReader = new PdfReader(streamOfPDFFile);
+//            int totalPages = pdfReader.getNumberOfPages();
+//
+//            //创建PDF Writer
+//            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
+//            document.open();
+//            BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
+//            PdfContentByte contentByte = writer.getDirectContent();
+//
+//            Rectangle rec = pdfReader.getPageSize(1);
+//            //新pdf为两个宽度
+//            Rectangle newRec = new Rectangle(0, 0, rec.getWidth() * 2, rec.getHeight());
+//            document.setPageSize(newRec);
+//
+//            PdfImportedPage page;
+//            PdfImportedPage page2;
+//            int currentPageNumber = 0;
+//
+//            //舍弃奇数的最后一页, 删除该设置后面不受影响
+////            totalPages = totalPages >> 1 << 1;
+//            for (int pageIndex = 0; pageIndex < totalPages; pageIndex += 2) {
+//                document.newPage();
+//                currentPageNumber++;
+//
+//                //原始第一页设置到左边
+//                page = writer.getImportedPage(pdfReader, pageIndex + 1);
+//                contentByte.addTemplate(page, 0, 0);
+//
+//                //第二页设置到右边
+//                if (pageIndex + 2 <= totalPages) {
+//                    page2 = writer.getImportedPage(pdfReader, pageIndex + 2);
+//                    contentByte.addTemplate(page2, rec.getWidth(), 0);
+//                }
+//
+//                //设置页码
+//                if (paginate) {
+//                    contentByte.beginText();
+//                    contentByte.setFontAndSize(baseFont, 13);
+//                    contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + SystemConstant.ORG_SPLIT + (totalPages / 2 + (totalPages % 2 == 0 ? 0 : 1)), newRec.getWidth() / 2, 17, 0);
+//                    contentByte.endText();
+//                }
+//            }
+//
+//            outputStream.flush();
+//            document.close();
+//            outputStream.close();
+//        } catch (Exception e) {
+//            log.error(SystemConstant.LOG_ERROR, e);
+//        } finally {
+//            if (document.isOpen()) {
+//                document.close();
+//            }
+//            try {
+//                if (outputStream != null) {
+//                    outputStream.close();
+//                }
+//            } catch (IOException ioe) {
+//                log.error(SystemConstant.LOG_ERROR, ioe);
+//            }
+//        }
+//    }
 
     /**
      * pdf添加水印

+ 10 - 9
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/AttachmentCommonServiceImpl.java

@@ -12,6 +12,7 @@ import com.qmth.teachcloud.common.util.ConvertUtil;
 import com.qmth.teachcloud.common.util.FileStoreUtil;
 import com.qmth.teachcloud.common.util.Zip4jUtil;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -122,8 +123,8 @@ public class AttachmentCommonServiceImpl implements AttachmentCommonService {
             }
 
             BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
-            byte[] buf = new byte[1024];
-            int len = 0;
+//            byte[] buf = new byte[1024];
+//            int len = 0;
 
             String fName = URLEncoder.encode(fileName, SystemConstant.CHARSET_NAME);
 
@@ -131,14 +132,14 @@ public class AttachmentCommonServiceImpl implements AttachmentCommonService {
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition", "attachment; filename=" + fName);
 
-            OutputStream outStream = response.getOutputStream();
-
-            while ((len = br.read(buf)) > 0) {
-                outStream.write(buf, 0, len);
-            }
-
+//            OutputStream outStream = response.getOutputStream();
+//
+//            while ((len = br.read(buf)) > 0) {
+//                outStream.write(buf, 0, len);
+//            }
+            IOUtils.copy(br, response.getOutputStream());
             br.close();
-            outStream.close();
+//            outStream.close();
         } catch (IOException e) {
             log.error(SystemConstant.LOG_ERROR, e);
         }

+ 9 - 8
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/TeachcloudCommonServiceImpl.java

@@ -21,6 +21,7 @@ import com.qmth.teachcloud.common.enums.*;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.*;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -823,8 +824,8 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
             }
 
             BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
-            byte[] buf = new byte[1024];
-            int len = 0;
+//            byte[] buf = new byte[1024];
+//            int len = 0;
 
             String fName = new String(fileName.getBytes(), "ISO-8859-1");
 
@@ -832,14 +833,14 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition", "attachment; filename=" + fName);
 
-            OutputStream outStream = response.getOutputStream();
-
-            while ((len = br.read(buf)) > 0) {
-                outStream.write(buf, 0, len);
-            }
+//            OutputStream outStream = response.getOutputStream();
 
+//            while ((len = br.read(buf)) > 0) {
+//                outStream.write(buf, 0, len);
+//            }
+            IOUtils.copy(br, response.getOutputStream());
             br.close();
-            outStream.close();
+//            outStream.close();
         } catch (FileNotFoundException e) {
             log.error(SystemConstant.LOG_ERROR, e);
         } catch (IOException e) {

+ 25 - 63
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ConvertUtil.java

@@ -2,6 +2,7 @@ package com.qmth.teachcloud.common.util;
 
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import org.apache.commons.io.IOUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPageTree;
 import org.apache.pdfbox.rendering.PDFRenderer;
@@ -40,10 +41,11 @@ public class ConvertUtil {
     // inputStream转outputStream
     public static ByteArrayOutputStream parse(final InputStream in) throws Exception {
         final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
-        int ch;
-        while ((ch = in.read()) != -1) {
-            swapStream.write(ch);
-        }
+//        int ch;
+//        while ((ch = in.read()) != -1) {
+//            swapStream.write(ch);
+//        }
+        IOUtils.copy(in, swapStream);
         return swapStream;
     }
 
@@ -54,22 +56,6 @@ public class ConvertUtil {
         return new ByteArrayInputStream(baos.toByteArray());
     }
 
-    public static void inputStream2File(InputStream is, File file) throws IOException {
-        OutputStream os = null;
-        try {
-            os = new FileOutputStream(file);
-            int len = 0;
-            byte[] buffer = new byte[8192];
-
-            while ((len = is.read(buffer)) != -1) {
-                os.write(buffer, 0, len);
-            }
-        } finally {
-            os.close();
-            is.close();
-        }
-    }
-
     /**
      * 转换,解析时间
      *
@@ -293,32 +279,6 @@ public class ConvertUtil {
         }
     }
 
-    public File saveLocal(InputStream inputStream, String dirPath) throws IOException {
-        File desFile = new File(dirPath);
-        if (!desFile.getParentFile().exists()) {
-            desFile.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
-        }
-        byte[] bytes = new byte[1024]; // 开辟一个拷贝缓冲区
-        OutputStream outputStream = null;
-        try {
-            outputStream = new FileOutputStream(desFile);
-            int length;
-            while ((length = inputStream.read(bytes)) != -1) { //当读到尽头后,返回值为-1这个时候停止输出,拷贝结束
-                outputStream.write(bytes, 0, length);
-            }
-            return desFile;
-        } catch (Exception e) {
-            throw e;
-        } finally {
-            if (inputStream != null) {
-                inputStream.close();
-            }
-            if (outputStream != null) {
-                outputStream.close();
-            }
-        }
-    }
-
     /**
      * 判断里面参数只要有一个为空 就是true 全部不为空时 false
      *
@@ -340,8 +300,8 @@ public class ConvertUtil {
 
     public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
         try {
-            byte[] buf = new byte[1024];
-            int len = 0;
+//            byte[] buf = new byte[1024];
+//            int len = 0;
 
             String fName = new String(fileName.getBytes(), "ISO-8859-1");
 
@@ -349,14 +309,15 @@ public class ConvertUtil {
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition", "attachment; filename=" + fName);
 
-            OutputStream outStream = response.getOutputStream();
+//            OutputStream outStream = response.getOutputStream();
 
-            while ((len = inputStream.read(buf)) > 0) {
-                outStream.write(buf, 0, len);
-            }
+//            while ((len = inputStream.read(buf)) > 0) {
+//                outStream.write(buf, 0, len);
+//            }
 
+            IOUtils.copy(inputStream, response.getOutputStream());
             inputStream.close();
-            outStream.close();
+//            outStream.close();
         } catch (IOException e) {
             log.error(SystemConstant.LOG_ERROR, e);
         }
@@ -370,9 +331,10 @@ public class ConvertUtil {
             response.setContentType("application/x-msdownload");
             response.setHeader("Content-Disposition", "attachment; filename=" + fName);
 
-            OutputStream outStream = response.getOutputStream();
-            baos.writeTo(outStream);
-            outStream.close();
+//            OutputStream outStream = response.getOutputStream();
+//            baos.writeTo(outStream);
+//            outStream.close();
+            IOUtils.write(baos.toByteArray(), response.getOutputStream());
         } catch (IOException e) {
             throw new Exception("下载失败");
         }
@@ -382,7 +344,7 @@ public class ConvertUtil {
         PDDocument doc = null;
         ByteArrayOutputStream os = null;
         InputStream stream = null;
-        OutputStream out = null;
+//        OutputStream out = null;
         List<File> fileList = new ArrayList<>();
         try {
             // pdf路径
@@ -396,7 +358,6 @@ public class ConvertUtil {
                 BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
                 os = new ByteArrayOutputStream();
                 ImageIO.write(bim, "jpg", os);
-                byte[] dataList = os.toByteArray();
                 // jpg文件转出路径
                 File file = new File(targetPath + SystemConstant.HYPHEN + (i + 1) + SystemConstant.JPG_PREFIX);
                 if (!file.getParentFile().exists()) {
@@ -404,8 +365,9 @@ public class ConvertUtil {
                     file.getParentFile().mkdirs();
                     file.createNewFile();
                 }
-                out = new FileOutputStream(file);
-                out.write(dataList);
+//                out = new FileOutputStream(file);
+                IOUtils.write(os.toByteArray(), new FileOutputStream(file));
+//                out.write(dataList);
                 fileList.add(file);
             }
         } catch (Exception e) {
@@ -421,9 +383,9 @@ public class ConvertUtil {
             if (stream != null) {
                 stream.close();
             }
-            if (out != null) {
-                out.close();
-            }
+//            if (out != null) {
+//                out.close();
+//            }
         }
         return fileList;
     }