Commit e9650d7c by guanguan1009

修改

parent 825483d3
......@@ -62,6 +62,10 @@
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -7,10 +7,13 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
......@@ -23,11 +26,14 @@ public class AppListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private ConverterService converterService;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.err.println("正在处理 " + filepath);
System.err.println("处理开始 " + new Date());
System.err.println("处理开始 " + LocalDateTime.now());
converterService.converterPdf2Pptx(filepath);
System.err.println("处理完成 " + new Date());
System.err.println("处理完成 " + LocalDateTime.now());
JOptionPane.showMessageDialog(new JFrame().getContentPane(),
"文件转换完成!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
package com.jtyjy.pdf2pptx.application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class ShutdownContext{
@Autowired
private ApplicationContext appContext;
public void initiateShutdown(int returnCode){
SpringApplication.exit(appContext, () -> returnCode);
}
}
......@@ -115,6 +115,7 @@ public class Pdf2Png {
ImageIO.write(image, "png", dstFile);
}
logger.info("PDF文档:{} 转PNG图片成功!{}页-{}页", PdfFilePath, startPage + 1, endPage);
System.err.println("PDF文档:[" + PdfFilePath + "] 转PNG图片成功![" + startPage + 1 + "]页-[" + endPage + "]页");
}
private static boolean createDirectory(String folder) {
......
package com.jtyjy.pdf2pptx.converter;
import com.alibaba.fastjson.JSONObject;
import com.jtyjy.pdf2pptx.application.ShutdownContext;
import com.jtyjy.pdf2pptx.support.Constant;
import com.jtyjy.pdf2pptx.support.FileUtil;
import com.jtyjy.pdf2pptx.support.PdfTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
......@@ -33,11 +38,16 @@ public class Pdf2Pptx {
private static ExecutorService executorService;
@Autowired
private ShutdownContext shutdownContext;
public void converte(String path) {
conver(path);
conver(path, null);
// shutdownContext.initiateShutdown(0);
}
private static void conver(String path) {
private static void conver(String path, ConfigurableApplicationContext context) {
System.setProperty("java.awt.headless","false");
//读取指定文件夹下所有文件并转换为图片
long startTime = System.currentTimeMillis();
List<String> allFilePathName = new ArrayList<>();
......@@ -53,13 +63,13 @@ public class Pdf2Pptx {
logger.info("转换pdf:{} 为图片开始", file.getAbsolutePath());
long begin = System.currentTimeMillis();
executorService = Executors.newFixedThreadPool(6);
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 300,executorService);
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 300, executorService);
long end = System.currentTimeMillis();
logger.info("转换pdf:{} 为图片完成,耗时{}ms", file.getAbsolutePath(), end - begin);
while (exeFlag) {
if (((ThreadPoolExecutor) executorService).getActiveCount() == 0) {
executorService.shutdown();
exeFlag=false;
exeFlag = false;
System.out.println("转换图片已经完成");
}
}
......@@ -71,13 +81,13 @@ public class Pdf2Pptx {
}
} else {
logger.info("文件:{} 已经转换", file.getAbsolutePath());
System.err.println("文件:[" + file.getAbsolutePath() + "] 已经转换");
}
}
}
//将图片插入PPT
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(path, allDirPathName);
......@@ -101,7 +111,7 @@ public class Pdf2Pptx {
while (exeFlag) {
if (((ThreadPoolExecutor) executorService).getActiveCount() == 0) {
executorService.shutdown();
exeFlag=false;
exeFlag = false;
System.out.println("转换ppt已经完成");
logger.info("转换所有ppt已经完成");
}
......@@ -114,7 +124,6 @@ public class Pdf2Pptx {
logger.info("删除文件夹:{}及其文件成功", dirPathName);
}
}
}
/**
......@@ -145,16 +154,8 @@ public class Pdf2Pptx {
}
public static void main(String[] args) {
//读取指定文件夹下所有文件并转换为图片
// conver(Constant.SOURCE_PATH);
//删除所有生成的图片文件
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(Constant.SOURCE_PATH, allDirPathName);
for (String dirPathName : allDirPathName) {
if (dirPathName.endsWith("_图片") && FileUtil.delFolder(dirPathName)) {
logger.info("删除文件夹:{}及其文件成功", dirPathName);
}
}
int processors = Runtime.getRuntime().availableProcessors();
System.out.println(processors);
}
}
......@@ -5,7 +5,7 @@ public class Constant {
/**
* 待读取文件根目录
*/
public static String SOURCE_PATH = "F:\\测试文档\\高二";
public static String SOURCE_PATH = "F:\\document\\高二\\难卷";
// public static String SOURCE_PATH = "F:\\高二";
}
# 注意斜杠
file:
path: D:\\document
#启用shutdown
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: shutdown,info,health
base-path: /manage
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment