27 changed files with 692 additions and 104 deletions
@ -0,0 +1,33 @@ |
|||||
|
HELP.md |
||||
|
target/ |
||||
|
!.mvn/wrapper/maven-wrapper.jar |
||||
|
!**/src/main/**/target/ |
||||
|
!**/src/test/**/target/ |
||||
|
|
||||
|
### STS ### |
||||
|
.apt_generated |
||||
|
.classpath |
||||
|
.factorypath |
||||
|
.project |
||||
|
.settings |
||||
|
.springBeans |
||||
|
.sts4-cache |
||||
|
|
||||
|
### IntelliJ IDEA ### |
||||
|
.idea |
||||
|
*.iws |
||||
|
*.iml |
||||
|
*.ipr |
||||
|
|
||||
|
### NetBeans ### |
||||
|
/nbproject/private/ |
||||
|
/nbbuild/ |
||||
|
/dist/ |
||||
|
/nbdist/ |
||||
|
/.nb-gradle/ |
||||
|
build/ |
||||
|
!**/src/main/**/build/ |
||||
|
!**/src/test/**/build/ |
||||
|
|
||||
|
### VS Code ### |
||||
|
.vscode/ |
@ -0,0 +1,19 @@ |
|||||
|
# Licensed to the Apache Software Foundation (ASF) under one |
||||
|
# or more contributor license agreements. See the NOTICE file |
||||
|
# distributed with this work for additional information |
||||
|
# regarding copyright ownership. The ASF licenses this file |
||||
|
# to you under the Apache License, Version 2.0 (the |
||||
|
# "License"); you may not use this file except in compliance |
||||
|
# with the License. You may obtain a copy of the License at |
||||
|
# |
||||
|
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
# |
||||
|
# Unless required by applicable law or agreed to in writing, |
||||
|
# software distributed under the License is distributed on an |
||||
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
||||
|
# KIND, either express or implied. See the License for the |
||||
|
# specific language governing permissions and limitations |
||||
|
# under the License. |
||||
|
wrapperVersion=3.3.2 |
||||
|
distributionType=only-script |
||||
|
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip |
@ -0,0 +1,66 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>2.2.10.RELEASE</version> |
||||
|
</parent> |
||||
|
<groupId>com.nuoli</groupId> |
||||
|
<artifactId>PDF</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
<name>PDF</name> |
||||
|
|
||||
|
|
||||
|
<properties> |
||||
|
<java.version>1.8</java.version> |
||||
|
<hutool.version>5.7.14</hutool.version> |
||||
|
<maven.compiler.source>1.8</maven.compiler.source> |
||||
|
<maven.compiler.target>1.8</maven.compiler.target> |
||||
|
</properties> |
||||
|
|
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
<!--工具包--> |
||||
|
<dependency> |
||||
|
<groupId>cn.hutool</groupId> |
||||
|
<artifactId>hutool-all</artifactId> |
||||
|
<version>${hutool.version}</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.pdfbox</groupId> |
||||
|
<artifactId>fontbox</artifactId> |
||||
|
<version>2.0.9</version> |
||||
|
</dependency> |
||||
|
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox --> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.pdfbox</groupId> |
||||
|
<artifactId>pdfbox</artifactId> |
||||
|
<version>2.0.9</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -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); |
||||
|
} |
||||
|
|
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
*/ |
@ -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(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -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); |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
server: |
||||
|
port: 8083 |
||||
|
|
||||
|
material: |
||||
|
path: \\cnsha08447\SSLSDATA\07_Technology\Formal_Drawing\PDF |
||||
|
|
||||
|
vehicle: |
||||
|
path: D:\software\images |
||||
|
post: 8088 |
@ -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() { |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue