多线程完成PDF文件转图片
系统需要将PDF文件由后台直接转为img图片,供前端页面直接展示,不需要用户下载即可预览文件内容。直接转换时如果文件过大,耗时很长,影响用户体验,后调研后使用多线程方式进行,显著加快图片转换速度。
原始版访问:https://blog.csdn.net/wmf_helloWorld/article/details/104051137
1、创建线程池
ExecutorService executorService =
new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50));
初始化线程池,因系统默认一次预览5张图片,进行分页分步预览,所以设置核心线程池大小为5.设置最后队列为new ArrayBlockingQueue(50)),可以排队50个等待任务,若大于50则不建议预览,或根据需要使用其他队列。
可访问:https://www.cnblogs.com/dafanjoy/p/9729358.html
2、主要转换方法体
2.1、全部转换
public List<String> pdfTurnImage(String filePath) throws CodeException {
List<String> fileImageList;
File file = new File(filePath);
try(PDDocument pdDocument = PDDocument.load(file)) {
PdfReader reader = new PdfReader(filePath);
int pages = reader.getNumberOfPages();
LOGGER.info(LogType.INFO, "pdf文件共有"+ pages +"页文件");
String[] imgStrArr = new String[pages];
fileImageList = Arrays.asList(imgStrArr);
List<Future<Boolean>> futureTaskList =new ArrayList<>();
for(int i=0; i<pages;


1490

被折叠的 条评论
为什么被折叠?



