<!--PDF转图片-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>xmpbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>preflight</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.0</version>
</dependency>
Cotroller层
@Log(title = "文件上传", businessType = BusinessType.INSERT)
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, HttpServletRequest req) throws IOException {
//判null,文件是空后续则不进行
if (file.isEmpty() || file.getSize() <= 0) {
return AjaxResult.error("文件上传失败");
}
//判断文件格式,改功能模块只允许上传PDF文件
String extension = FileUploadUtils.getExtension(file);
if (!"pdf".equals(extension)) {
return AjaxResult.error("文件格式异常,只能上传PDF文件");
}
Map<String, Object> map = tMonitoringReportService.upload(file, req);
if (map == null) {
return AjaxResult.error("文件上传失败");
}
return AjaxResult.success(map);
}
Service层
@Override
public Map<String, Object> upload(MultipartFile file, HttpServletRequest req) throws IOException {
String userName = SecurityContextHolder.getContext().getAuthentication().getName();
//获取文件名字
String fileName = file.getOriginalFilename();
String basePath = "/file";
String fileUploadPath = "";
try {
fileUploadPath = FileUploadUtils.upload(项目相关配置.getProfile() + basePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
} catch (InvalidExtensionException e) {
e.printStackTrace();
logger.error("上传文件保存本地失败");
return null;
}
String fileUploadName = fileUploadPath.split("/")[fileUploadPath.split("/").length - 1];
//保留基础上传路径
String replace = fileUploadPath.replace("/profile", "").
replace(fileUploadName, "");
List<String> list = PdfUtil.getPdfs(replace, fileUploadName);
if (list == null) {
return null;
}
String images = StringUtils.strip(list.toString(), "[]");
Map<String, Object> map = new HashMap<>();
map.put("list", images);
map.put("fileName", fileName);
map.put("url", fileUploadPath);
map.put("userName", userName);
map.put("type", 1);
map.put("suffixName", FileUploadUtils.getExtension(file));
return map;
}
工具类
public class PdfUtil {
private static final Logger log = LoggerFactory.getLogger(PdfUtil.class);
public static List<String> getPdfs(String basePath, String filename) throws IOException {
PDDocument document = PDDocument.load(new File(LX5in1Config.getProfile() + basePath + filename));
Splitter splitter = new Splitter();
List<PDDocument> Pages = splitter.split(document);
List<String> list = new ArrayList<>();
try {
int j = 1;
for (PDDocument page : Pages) {
PDFRenderer renderer = new PDFRenderer(page);
BufferedImage image = renderer.renderImage(0);
String report_images = LX5in1Config.getProfile() + basePath + System.currentTimeMillis() + j + ".jpg";
ImageIO.write(image, "JPEG", new File(report_images));
list.add(Constants.RESOURCE_PREFIX + basePath + System.currentTimeMillis() + j + ".jpg");
j++;
}
} catch (IOException e) {
e.printStackTrace();
log.error("PDF转图片失败");
return null;
}
log.info("上传PDF已转换为图片");
document.close();
return list;
}
}