新增文件上传方法 修改ftppdf和ftpimage 方法增加类型

This commit is contained in:
lxd 2024-10-28 12:37:48 +08:00
parent c4e49c9265
commit 19df9ec6da
7 changed files with 212 additions and 106 deletions

View File

@ -8,8 +8,12 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.SneakyThrows;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;
/**
* 文件工具类
@ -81,4 +85,41 @@ public class FileUtils {
return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content));
}
/**
* 将Base64编码的字符串保存为文件
*
* @param base64String Base64编码的字符串
* @param folderPath 文件保存的文件夹路径
* @param fileName 文件名不包含扩展名
* @param extension 文件扩展名例如 "png", "jpg", "txt"
* @throws IOException 如果写入文件时发生错误
*/
public static void saveBase64ToFile(String base64String, String folderPath, String fileName, String extension) throws IOException {
// 检查文件名是否有效
if (fileName.contains("..") || fileName.contains("/") || fileName.contains("\\")) {
throw new IllegalArgumentException("Invalid file name.");
}
// 解码Base64字符串为字节数组
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
// 创建文件的路径
Path folder = Paths.get(folderPath);
if (!Files.exists(folder)) {
// 如果文件夹不存在则创建它
Files.createDirectories(folder);
}
// 构建完整的文件名包括扩展名
Path filePath = folder.resolve(fileName + "." + extension);
// 使用Files.newOutputStream创建OutputStream并设置为追加模式
try (java.io.OutputStream outputStream = Files.newOutputStream(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
outputStream.write(decodedBytes);
} catch (IOException e) {
throw e;
}
}
}

View File

@ -25,4 +25,5 @@ public class IoUtils {
return StrUtil.utf8Str(IoUtil.read(in, isClose));
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.tblist.controller.admin.patientexamlist;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.org.OrgUnitDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.org.OrgUnitService;
@ -396,63 +397,79 @@ public class PatientexamlistController {
@Operation(summary = "获取pdf上传ftp")
public void FtpPdf(@RequestBody inspdfscreenshotVO insimagescreenshotVO) {
String base64String = insimagescreenshotVO.getImagebase();
String ftpServer = "114.55.171.231";
int ftpPort = 21; // FTP端口默认为21
String ftpUser = "pacs";
String ftpPassword = "pacs";
String uploadPath = "/";
String fileName = System.currentTimeMillis() + ".pdf";
FTPClient ftpClient = new FTPClient();
ftpClient.enterLocalPassiveMode();
try {
// 连接FTP服务器
ftpClient.connect(ftpServer, ftpPort);
ftpClient.login(ftpUser, ftpPassword);
// 设置文件传输类型为二进制
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if(insimagescreenshotVO.getModel().equals("0"))
{
// 解码Base64字符串
String base64Image = base64String.split(",")[1]; // 去掉数据URL的头部
// 解码Base64字符串
byte[] decodedBytes = Base64.getDecoder().decode(base64Image);
// 创建输入流
InputStream inputStream = new ByteArrayInputStream(decodedBytes);
// 上传文件
boolean result = ftpClient.storeFile(uploadPath + "/" + fileName, inputStream);
if (result) {
//上传成功
//当前时间
LocalDateTime dateTime = LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
AdminUserDO user = userService.getUser(getLoginUserId());
PatientexamlistSaveReqVO updateReqVO = new PatientexamlistSaveReqVO();
updateReqVO.setId(insimagescreenshotVO.getID());
updateReqVO.setPdfurl("http://114.55.171.231:48082/" + fileName);
patientexamlistService.updatePatientexamlist(updateReqVO);
} else {
System.out.println("文件上传失败");
String base64Image = base64String.split(",")[1];
try
{
FileUtils.saveBase64ToFile(base64Image,insimagescreenshotVO.getFolderPath(),fileName.split("\\.")[0],"pdf");
}
// 断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (SocketException e) {
System.err.println("无法连接到FTP服务器: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO异常: " + e.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException ignored) {
catch (Exception ignored)
{
System.out.println("文件上传失败"+ignored.getMessage());
}
}
else
{
String ftpServer = "114.55.171.231";
int ftpPort = 21; // FTP端口默认为21
String ftpUser = "pacs";
String ftpPassword = "pacs";
String uploadPath = "/";
FTPClient ftpClient = new FTPClient();
ftpClient.enterLocalPassiveMode();
try {
// 连接FTP服务器
ftpClient.connect(ftpServer, ftpPort);
ftpClient.login(ftpUser, ftpPassword);
// 设置文件传输类型为二进制
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 解码Base64字符串
String base64Image = base64String.split(",")[1]; // 去掉数据URL的头部
// 解码Base64字符串
byte[] decodedBytes = Base64.getDecoder().decode(base64Image);
// 创建输入流
InputStream inputStream = new ByteArrayInputStream(decodedBytes);
// 上传文件
boolean result = ftpClient.storeFile(uploadPath + "/" + fileName, inputStream);
// 断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (SocketException e) {
System.err.println("无法连接到FTP服务器: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO异常: " + e.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException ignored) {
}
}
}
//上传成功
//当前时间
LocalDateTime dateTime = LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
AdminUserDO user = userService.getUser(getLoginUserId());
PatientexamlistSaveReqVO updateReqVO = new PatientexamlistSaveReqVO();
updateReqVO.setId(insimagescreenshotVO.getID());
updateReqVO.setPdfurl("https://pacs.gw12320.com/video/" +insimagescreenshotVO.getID()+"/"+ fileName);
patientexamlistService.updatePatientexamlist(updateReqVO);
}

View File

@ -9,4 +9,8 @@ public class inspdfscreenshotVO {
private String imagebase;
private String model;
private String folderPath;
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.medicalimg;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class SaveFileBase64 {
@Schema(description = "文件保存的文件夹路径")
private String folderPath;
@Schema(description = "Base64编码的字符串")
private String imagebase;
@Schema(description = "文件名(不包含扩展名)")
private String fileName;
@Schema(description = "文件扩展名(例如 \"png\", \"jpg\", \"txt\"")
private String extension;
private String orgId;
}

View File

@ -17,5 +17,8 @@ public class insimagescreenshotVO {
private String imgDescription;
private String orgId;
//
private String model;
private String folderPath;
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.dicomworklist.DicomImagesDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dicomworklist.DicomSeriesDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dicomworklist.DicompatientDO;
@ -14,6 +15,7 @@ import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.image.I
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.image.PatientInfoVO;
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.image.SeriesVO;
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.image.StudyVO;
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.medicalimg.SaveFileBase64;
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.medicalimg.insimagescreenshotVO;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ultrasoniccom.controller.admin.ultrasonic.medicalimg.upmedicalimgVO;
@ -35,15 +37,11 @@ import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -430,17 +428,32 @@ public class ultrasonicController {
@Operation(summary = "获取图片上传ftp")
public void FtpImage(@RequestBody insimagescreenshotVO insimagescreenshotVO) {
String base64String = insimagescreenshotVO.getImagebase();
String ftpServer = "192.168.0.110"; //114.55.171.231
int ftpPort = 21; // FTP端口默认为21
String ftpUser = "pacs";
String ftpPassword = "pacs123";
String uploadPath = "/";
String fileName = System.currentTimeMillis() + ".jpg";
if(insimagescreenshotVO.getModel().equals("0"))
{
// 解码Base64字符串
String base64Image = base64String.split(",")[1];
try
{
FileUtils.saveBase64ToFile(base64Image,insimagescreenshotVO.getFolderPath(),fileName.split("\\.")[0],"jpg");
}
catch (Exception ignored)
{
System.out.println("文件上传失败"+ignored.getMessage());
}
}
else
{
String ftpServer = "192.168.0.110"; //114.55.171.231
int ftpPort = 21; // FTP端口默认为21
String ftpUser = "pacs";
String ftpPassword = "pacs123";
String uploadPath = "/";
FTPClient ftpClient = new FTPClient();
ftpClient.enterLocalPassiveMode();
try {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.enterLocalPassiveMode();
// 连接FTP服务器
ftpClient.connect(ftpServer, ftpPort);
ftpClient.login(ftpUser, ftpPassword);
@ -457,51 +470,48 @@ public class ultrasonicController {
// 上传文件
boolean result = ftpClient.storeFile(uploadPath + "/" + fileName, inputStream);
if (result) {
//上传成功
//当前时间
LocalDateTime dateTime = LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
AdminUserDO user = userService.getUser(getLoginUserId());
// 生成随机 UUID
UUID randomUUID = UUID.randomUUID();
medicalimgDO medicalimgDO = new medicalimgDO();
medicalimgDO.setId(randomUUID.toString());
medicalimgDO.setImgUrl("http://192.168.0.110:48082/" + fileName);
medicalimgDO.setCreatePerson("");
medicalimgDO.setCreateDate(dateTime);
medicalimgDO.setRegId(insimagescreenshotVO.getID());
if (insimagescreenshotVO.getOrgId() != null && !insimagescreenshotVO.getOrgId().trim().equals(""))
medicalimgDO.setOrgId(insimagescreenshotVO.getOrgId().trim());
else if (user != null && user.getOrgId() != null)
medicalimgDO.setOrgId(user.getOrgId());
medicalimgDO.setSelected("0");
if (insimagescreenshotVO.getImgType() == null || insimagescreenshotVO.getImgType().trim().equals(""))
medicalimgDO.setImgType("1");
else
medicalimgDO.setImgType(insimagescreenshotVO.getImgType().trim());
int count = medicalimgService.insimage(medicalimgDO);
} else {
System.out.println("文件上传失败");
}
// 断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (SocketException e) {
System.err.println("无法连接到FTP服务器: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO异常: " + e.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
// 断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (SocketException e) {
System.err.println("无法连接到FTP服务器: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO异常: " + e.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException ignored) {
}
} catch (IOException ignored) {
}
}
//上传成功
//当前时间
LocalDateTime dateTime = LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
AdminUserDO user = userService.getUser(getLoginUserId());
// 生成随机 UUID
UUID randomUUID = UUID.randomUUID();
medicalimgDO medicalimgDO = new medicalimgDO();
medicalimgDO.setId(randomUUID.toString());
medicalimgDO.setImgUrl("/video/" +insimagescreenshotVO.getID()+"/"+ fileName);
medicalimgDO.setCreatePerson("");
medicalimgDO.setCreateDate(dateTime);
medicalimgDO.setRegId(insimagescreenshotVO.getID());
if (insimagescreenshotVO.getOrgId() != null && !insimagescreenshotVO.getOrgId().trim().equals(""))
medicalimgDO.setOrgId(insimagescreenshotVO.getOrgId().trim());
else if (user != null && user.getOrgId() != null)
medicalimgDO.setOrgId(user.getOrgId());
medicalimgDO.setSelected("0");
if (insimagescreenshotVO.getImgType() == null || insimagescreenshotVO.getImgType().trim().equals(""))
medicalimgDO.setImgType("1");
else
medicalimgDO.setImgType(insimagescreenshotVO.getImgType().trim());
int count = medicalimgService.insimage(medicalimgDO);
}
@ -622,5 +632,16 @@ public class ultrasonicController {
return success("插入成功");
}
@PostMapping("/SaveFileBase64")
@Operation(summary = "base64保存文件")
public String SaveFileBase64(@RequestBody SaveFileBase64 fileBase64){
try{
String base=fileBase64.getImagebase();
FileUtils.saveBase64ToFile(base,fileBase64.getFolderPath(),fileBase64.getFileName(),fileBase64.getExtension());
return "上传成功";
} catch (Exception e) {
return "上传失败" +e.getMessage();
}
}
}