1.修复压缩包 二级目录无法解压问题 修改压缩包生成PDF路径

2.修复PDF 带密码缓存问题 新增PDF带密码缓存方法 userToken
3.精简OFFICE 转换代码
4.精简TIF转换代码 新增TIF转换图片缓存 修复tif错误文件不自动释放内存 等待其他修复
5.修复下载方法错 特殊符号下载错误
6.调整文件名 统一方法在FileHandlerService
7.新增判断文件名是否被URL转义
This commit is contained in:
gaoxiongzaq
2023-10-23 17:06:05 +08:00
parent c559efcceb
commit deb91728d4
15 changed files with 529 additions and 380 deletions

View File

@@ -1,26 +1,26 @@
package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import com.sun.media.jai.codec.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.keking.web.filter.BaseUrlFilter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.io.FileChannelRandomAccessSource;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
import com.sun.media.jai.codec.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
@@ -28,7 +28,6 @@ public class ConvertPicUtil {
private static final int FIT_WIDTH = 500;
private static final int FIT_HEIGHT = 900;
private final static Logger logger = LoggerFactory.getLogger(ConvertPicUtil.class);
private final static String fileDir = ConfigConstants.getFileDir();
/**
@@ -38,17 +37,14 @@ public class ConvertPicUtil {
* @param strOutputFile 输出文件的路径和文件名
* @return boolean 是否转换成功
*/
public static List<String> convertTif2Jpg(String strInputFile, String strOutputFile) {
public static List<String> convertTif2Jpg(String strInputFile, String strOutputFile, boolean forceUpdatedCache) throws Exception {
List<String> listImageFiles = new ArrayList<>();
if (strInputFile == null || "".equals(strInputFile.trim())) {
return null;
}
String baseUrl = BaseUrlFilter.getBaseUrl();
if (!new File(strInputFile).exists()) {
logger.info("找不到文件【" + strInputFile + "");
return null;
}
strInputFile = strInputFile.replaceAll("\\\\", "/");
strOutputFile = strOutputFile.replaceAll(".jpg", "");
FileSeekableStream fileSeekStream = null;
try {
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
@@ -58,20 +54,18 @@ public class ConvertPicUtil {
fileSeekStream = new FileSeekableStream(strInputFile);
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
int intTifCount = imageDecoder.getNumPages();
logger.info("该tif文件共有【" + intTifCount + "】页");
String strJpgPath = fileDir+strOutputFile;
// logger.info("该tif文件共有【" + intTifCount + "】页");
// 处理目标文件夹,如果不存在则自动创建
File fileJpgPath = new File(strJpgPath);
File fileJpgPath = new File(strOutputFile);
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
logger.error("{} 创建失败", strJpgPath);
logger.error("{} 创建失败", strOutputFile);
}
// 循环处理每页tif文件转换为jpg
for (int i = 0; i < intTifCount; i++) {
String strJpg= strJpgPath + "/" + i + ".jpg";
String strJpgUrl = strOutputFile + "/" + i + ".jpg";
String strJpg= strOutputFile + "/" + i + ".jpg";
File fileJpg = new File(strJpg);
// 如果文件不存在,则生成
if (!fileJpg.exists()) {
if (forceUpdatedCache|| !fileJpg.exists()) {
RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(i);
ParameterBlock pb = new ParameterBlock();
pb.addSource(renderedImage);
@@ -80,24 +74,22 @@ public class ConvertPicUtil {
pb.add(jpegEncodeParam);
RenderedOp renderedOp = JAI.create("filestore", pb);
renderedOp.dispose();
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
// logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
}
listImageFiles.add(strJpgUrl);
strJpg = baseUrl+strJpg.replace(fileDir, "");
listImageFiles.add(strJpg);
}
return listImageFiles;
} catch (IOException e) {
e.printStackTrace();
return null;
if (!e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
logger.error("TIF转JPG异常文件路径" + strInputFile, e);
}
throw new Exception(e);
} finally {
if (fileSeekStream != null) {
try {
fileSeekStream.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
fileSeekStream.close();
}
}
return listImageFiles;
}
/**
@@ -106,44 +98,41 @@ public class ConvertPicUtil {
* @param strJpgFile 输入的jpg的路径和文件名
* @param strPdfFile 输出的pdf的路径和文件名
*/
public static boolean convertJpg2Pdf(String strJpgFile, String strPdfFile) {
Document document = null;
public static String convertJpg2Pdf(String strJpgFile, String strPdfFile) throws Exception {
Document document = new Document();
RandomAccessFileOrArray rafa = null;
FileOutputStream outputStream = null;
try {
document = new Document();
PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(strPdfFile)));
document.open();
rafa = new RandomAccessFileOrArray(new FileChannelRandomAccessSource(new RandomAccessFile(strJpgFile, "r").getChannel()));
RandomAccessFile aFile = new RandomAccessFile(strJpgFile, "r");
FileChannel inChannel = aFile.getChannel();
FileChannelRandomAccessSource fcra = new FileChannelRandomAccessSource(inChannel);
rafa = new RandomAccessFileOrArray(fcra);
int pages = TiffImage.getNumberOfPages(rafa);
outputStream = new FileOutputStream(strPdfFile);
PdfWriter.getInstance(document, outputStream);
document.open();
Image image;
for (int i = 1; i <= pages; i++) {
try {
image = TiffImage.getTiffImage(rafa, i);
image.scaleToFit(FIT_WIDTH, FIT_HEIGHT);
document.add(image);
} catch (Exception e) {
document.close();
rafa.close();
e.printStackTrace();
}
image = TiffImage.getTiffImage(rafa, i);
image.scaleToFit(FIT_WIDTH, FIT_HEIGHT);
document.add(image);
}
document.close();
rafa.close();
return true;
} catch (Exception e) {
logger.error("图片转PDF异常图片文件路径" + strJpgFile, e);
} catch (IOException e) {
if (!e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
logger.error("TIF转JPG异常文件路径" + strPdfFile, e);
}
throw new Exception(e);
} finally {
try {
if (document != null && document.isOpen()) {
document.close();
}
if (rafa != null) {
rafa.close();
}
} catch (IOException e) {
e.printStackTrace();
if (document != null) {
document.close();
}
if (rafa != null) {
rafa.close();
}
if (outputStream != null) {
outputStream.close();
}
}
return false;
return strPdfFile;
}
}