保单打印,临时处理:在没有ftp或者sftp配置时,将报文保存在服务器临时目录

master
szy 3 weeks ago
parent fba6d66b56
commit 8757c54bae
  1. 47
      micro-modules/micro-print/src/main/java/com/sinosoft/print/service/impl/FileUploadAndDownService.java

@ -55,6 +55,8 @@ import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -99,7 +101,6 @@ public class FileUploadAndDownService {
String printName = jsonObject.getString(PrintTempConstants.CONT_PRINT_NAME); String printName = jsonObject.getString(PrintTempConstants.CONT_PRINT_NAME);
String system = jsonObject.getString(PrintTempConstants.CONT_PRINT_FROM_SYSTEM); String system = jsonObject.getString(PrintTempConstants.CONT_PRINT_FROM_SYSTEM);
String XmlData =PrintTempConstants.XML_DECLARATION + JSONUtil.toXmlStr(JSONUtil.parse(Xml0bject)); String XmlData =PrintTempConstants.XML_DECLARATION + JSONUtil.toXmlStr(JSONUtil.parse(Xml0bject));
log.info("保单打印报文xml:" + XmlData);
if(StringUtils.isEmpty(XmlData)){ if(StringUtils.isEmpty(XmlData)){
return failedResponse("打印请求数据不能为空"); return failedResponse("打印请求数据不能为空");
//throw new RuntimeException("打印请求数据不能为空"); //throw new RuntimeException("打印请求数据不能为空");
@ -136,7 +137,16 @@ public class FileUploadAndDownService {
operate = "electronicUp"; operate = "electronicUp";
} }
PrintFtpConfig ftpConfig = getFtpConfig(operate); PrintFtpConfig ftpConfig = getFtpConfig(operate);
if(PrintTempConstants.FTP.equals(ftpConfig.getType())){ if(ftpConfig == null){
/*
在没有ftp或者sftp配置时将报文保存在服务器临时目录
Windows C:\Users\用户名\AppData\Local\Temp\demo.txt
Linux /tmp/demo.txt
*/
log.info(formattedXml);
System.out.println(formattedXml);
writeToTemp(fileName,formattedXml);
}else if(PrintTempConstants.FTP.equals(ftpConfig.getType())){
try { try {
log.info("开始使用FTP的方式上传xml文件"); log.info("开始使用FTP的方式上传xml文件");
FTPUtils.upload(ftpConfig.getIpAddress(),ftpConfig.getPort().intValue(),ftpConfig.getUsername(),ftpConfig.getPassword(),xmlInputStream,ftpConfig.getBasePath(),fileName); FTPUtils.upload(ftpConfig.getIpAddress(),ftpConfig.getPort().intValue(),ftpConfig.getUsername(),ftpConfig.getPassword(),xmlInputStream,ftpConfig.getBasePath(),fileName);
@ -177,6 +187,39 @@ public class FileUploadAndDownService {
return successResponse(""); return successResponse("");
} }
/**
* 将字符串写入服务器临时目录
*
* @param fileName 文件名 test.txt
* @param content 文件内容
* @return 写入后的文件对象
*/
public static Path writeToTemp(String fileName, String content) throws IOException {
// ✅ 使用系统临时目录(跨平台)
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
// ✅ 防止路径穿越攻击
Path safeFileName = Paths.get(fileName).getFileName();
if (safeFileName == null) {
throw new IllegalArgumentException("Invalid file name");
}
Path target = tempDir.resolve(safeFileName);
// ✅ 写入文件(UTF-8)
Files.write(
target,
content.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING
);
return target;
}
private ContPrintVo successResponse(String msg){ private ContPrintVo successResponse(String msg){
ContPrintVo contPrintVo = new ContPrintVo(); ContPrintVo contPrintVo = new ContPrintVo();
contPrintVo.setContPrintResult("Y"); contPrintVo.setContPrintResult("Y");

Loading…
Cancel
Save