add: 添加操作日志,添加PDF加载系统

This commit is contained in:
yanps
2025-03-12 10:27:32 +08:00
parent 42dd62d4a3
commit 3aad9dd705
27 changed files with 691 additions and 103 deletions

View File

@@ -0,0 +1,16 @@
package com.nuoli.PDF;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author admin
*/
@SpringBootApplication
public class GetPictureApplication {
public static void main(String[] args) {
SpringApplication.run(GetPictureApplication.class, args);
}
}

View File

@@ -0,0 +1,23 @@
/*
package com.yan.yan_first.config.handler;
*/
/**
* 全局异常类
*
* @author LENOVO
*//*
@RestControllerAdvice
public class HandlerException {
@ExceptionHandler(Exception.class)
public void handleException(Exception e) {
e.printStackTrace();
}
}
*/

View File

@@ -0,0 +1,173 @@
package com.nuoli.PDF.system.controller.secutiry;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.*;
/**
* 用户登录
*
* @author LENOVO
*/
@RestController
@RequestMapping("api/file")
public class AuthorizationController {
@Value("${material.path}")
public String materialPath;
@Value("${vehicle.path}")
public String vehiclePath;
@Value("${vehicle.post}")
public String vehicleFilePost;
@PostMapping("selectPath")
public List<Map<String, String>> selectPath(@RequestBody JSONArray json) throws IOException {
File file = new File(materialPath);
if (file.exists()) {
return selectMaterialPath(file, json);
} else {
System.out.println("文件不存在");
}
return null;
}
@PostMapping("selectFile")
public String selectFile(@RequestBody JSONObject json) {
byte[] bytes = pdf2png(json.getStr("path"), "png");
if (ObjectUtil.isNotEmpty(bytes)) {
BASE64Encoder encoder = new BASE64Encoder();
return "data:image/png;base64," + encoder.encode(bytes).replace("\r\n", "");
}
return null;
}
public List<Map<String, String>> selectMaterialPath(File directory, JSONArray materials) throws UnknownHostException {
if (directory == null || !directory.exists() || !directory.isDirectory()) {
return new ArrayList<>();
}
Queue<File> queue = new LinkedList<>();
queue.add(directory);
List<Map<String, String>> maps = new ArrayList<>();
Set<String> materialSet = new HashSet<>(materials.toList(String.class));
// 如果 materialSet 为空,直接返回空的 maps 列表
if (materialSet.isEmpty()) {
return maps;
}
while (!queue.isEmpty()) {
File currentDir = queue.poll();
File[] files = currentDir.listFiles();
if (files == null) {
continue;
}
for (File file : files) {
if (file.isDirectory()) {
queue.add(file);
} else if (file.isFile() && file.getName().endsWith(".pdf")) {
String material = file.getName().substring(0, file.getName().lastIndexOf('.'));
if (materialSet.contains(material)) {
pdf2png(file.getPath(), vehiclePath, material);
InetAddress localhost = InetAddress.getLocalHost();
String url = "http://" + localhost.getHostAddress() + ":" + vehicleFilePost + "/" + material + ".jpg";
HashMap<String, String> map = new HashMap<>();
map.put("name", material);
map.put("value", url);
maps.add(map);
materialSet.remove(material);
if (materialSet.isEmpty()) {
return maps;
}
}
}
}
}
return maps;
}
/**
* 使用pdfbox将整个pdf转换成图片
*
* @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
* @param type 图片类型 png 和jpg
*/
public byte[] pdf2png(String fileAddress, String type) {
File file = new File(fileAddress);
try (PDDocument doc = PDDocument.load(file)) {
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
// dpi为144越高越清晰转换越慢
BufferedImage image = renderer.renderImageWithDPI(i, 144);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
ImageIO.write(image, type, bos);
return bos.toByteArray();
}
}
} catch (IOException e) {
return null;
}
return null;
}
public void pdf2png(String fileAddress, String lastAddress, String materialName) {
File file = new File(fileAddress);
try (PDDocument doc = PDDocument.load(file)) {
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144);
String lastPath = lastAddress + "\\" + materialName + ".jpg";
File lastFile = new File(lastPath);
if(!lastFile.exists()){
lastFile.createNewFile();
}
ImageIO.write(image, "jpg", lastFile);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void imageToByteAndWriteToFile() throws IOException {
String imageUrl = "http://10.192.37.10:3000/uploads/sortedpallets/N329650_P26.png";
String outputFilePath = "D:\\software\\nginx-1.24.0\\N329650_P26.png";
URL url = new URL(imageUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
try (InputStream inputStream = httpURLConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
} finally {
httpURLConnection.disconnect();
}
}
}

View File

@@ -0,0 +1,17 @@
package com.nuoli.PDF.system.service.secutiry;
import cn.hutool.json.JSONObject;
/**
* 登录实现类
* @author LENOVO
*/
public interface AuthorizationService {
/**
* 用户登录
* @param json
* @return
*/
JSONObject login(JSONObject json);
}

View File

@@ -0,0 +1,26 @@
package com.nuoli.PDF.system.service.secutiry.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.nuoli.PDF.system.service.secutiry.AuthorizationService;
import org.springframework.stereotype.Service;
/**
* 登录实现类
*
* @author LENOVO
*/
@Service
public class AuthorizationServiceImpl implements AuthorizationService {
private static final String errString = "登录失败";
@Override
public JSONObject login(JSONObject json) {
boolean allNotEmpty = ObjectUtil.isAllNotEmpty(json.getStr("username"), json.getStr("password"));
if (allNotEmpty) throw new RuntimeException(errString);
return null;
}
}

View File

@@ -0,0 +1,9 @@
server:
port: 8083
material:
path: \\cnsha08447\SSLSDATA\07_Technology\Formal_Drawing\PDF
vehicle:
path: D:\software\images
post: 8088

View File

@@ -0,0 +1,13 @@
package com.nuoli.PDF;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class YanFirstApplicationTests {
@Test
void contextLoads() {
}
}