Merge remote-tracking branch 'origin/1.0' into 1.0
This commit is contained in:
commit
23ca847b93
14
pom.xml
14
pom.xml
|
@ -19,7 +19,7 @@
|
|||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<hutool.version>5.8.25</hutool.version>
|
||||
<hutool.version>5.8.36</hutool.version>
|
||||
<jna.version>5.14.0</jna.version>
|
||||
<pdfbox.version>3.0.2</pdfbox.version>
|
||||
</properties>
|
||||
|
@ -54,6 +54,12 @@
|
|||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 调用DLL -->
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
|
@ -119,12 +125,6 @@
|
|||
<version>3.1.28</version> <!-- 可以根据实际情况调整版本 -->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.15</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 数据校验-->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
|
|
|
@ -8,11 +8,13 @@ import com.dpkj.common.utils.ThirdService;
|
|||
import com.dpkj.modules.print.enums.*;
|
||||
import com.dpkj.modules.print.request.MS439Request;
|
||||
import com.dpkj.modules.print.service.MS439PrintService;
|
||||
import com.dpkj.modules.print.utils.PDFUtils;
|
||||
import com.dpkj.modules.print.vo.PrinterStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
@ -31,6 +33,12 @@ public class MS439PrintServiceImpl implements MS439PrintService {
|
|||
|
||||
@Override
|
||||
public LexMarkResultDTO<?> printImage(MS439Request request) {
|
||||
// 校验是本地地址还是http开头的
|
||||
PDFUtils.AddressType addressType = PDFUtils.checkAddressType(request.getFileDir());
|
||||
if (addressType.equals(PDFUtils.AddressType.HTTP)) {
|
||||
request.setFileDir(PDFUtils.downloadPdf(request.getFileDir()));
|
||||
}
|
||||
|
||||
LexMarkResultDTO<PrinterStatus> status = this.getStatus(request.getPagesource());
|
||||
PrinterStatus ms439 = status.getData();
|
||||
|
||||
|
@ -78,7 +86,19 @@ public class MS439PrintServiceImpl implements MS439PrintService {
|
|||
|
||||
lexMarkDTO.setParam(param.toString());
|
||||
LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class);
|
||||
// thirdService.close("HtmPrinter");
|
||||
|
||||
File file = new File(request.getFileDir());
|
||||
// 检查文件是否存在
|
||||
if (file.exists()) {
|
||||
// 尝试删除文件
|
||||
if (file.delete()) {
|
||||
log.info("文件删除成功: " + request.getFileDir());
|
||||
} else {
|
||||
log.info("文件删除失败: " + request.getFileDir());
|
||||
}
|
||||
} else {
|
||||
log.info("文件不存在: " + request.getFileDir());
|
||||
}
|
||||
return paramLexMarkResultDTO;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
package com.dpkj.modules.print.utils;
|
||||
|
||||
import com.dpkj.common.exception.RRException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class PDFUtils {
|
||||
|
||||
private static final String defaultPath = "D:/images";
|
||||
|
||||
public enum AddressType {
|
||||
HTTP,
|
||||
LOCAL,
|
||||
OTHER,
|
||||
UNKNOWN
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验地址类型
|
||||
* @param address 地址
|
||||
*/
|
||||
public static AddressType checkAddressType(String address) {
|
||||
if (address == null || address.isEmpty()) {
|
||||
return AddressType.UNKNOWN;
|
||||
}
|
||||
|
||||
// 检查是否为 HTTP 或 HTTPS 地址
|
||||
if (address.startsWith("http://") || address.startsWith("https://")) {
|
||||
return AddressType.HTTP;
|
||||
}
|
||||
|
||||
// 简单判断是否为本地地址
|
||||
// 本地地址可能以文件协议 file:// 开头,或者是一个相对路径或绝对路径
|
||||
if (address.startsWith("file://") ||
|
||||
(address.contains(":") && address.contains("\\")) ||
|
||||
(address.startsWith("/"))) {
|
||||
return AddressType.LOCAL;
|
||||
}
|
||||
|
||||
return AddressType.OTHER;
|
||||
}
|
||||
|
||||
public static String downloadPdf(String pdfUrl) {
|
||||
String savePath = defaultPath + "/genera_image_" + System.currentTimeMillis() + ".pdf";
|
||||
try {
|
||||
// 创建 URL 对象
|
||||
URL url = new URL(pdfUrl);
|
||||
// 打开 HTTP 连接
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
// 设置请求方法为 GET
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
// 获取响应码
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
// 获取输入流
|
||||
try (InputStream inputStream = connection.getInputStream();
|
||||
// 创建文件输出流
|
||||
FileOutputStream outputStream = new FileOutputStream(savePath)) {
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
// 循环读取数据并写入文件
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
log.info("PDF 文件下载成功,保存路径: " + savePath);
|
||||
}
|
||||
} else {
|
||||
log.error("下载失败,响应码: " + responseCode);
|
||||
}
|
||||
// 断开连接
|
||||
connection.disconnect();
|
||||
}catch (Exception e) {
|
||||
throw new RRException("PDF文件下载失败");
|
||||
}
|
||||
|
||||
return savePath;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue