From 419511e82ce67ae72c948bc5b41dbd03d6ce95fe Mon Sep 17 00:00:00 2001 From: fangjf <2905107328@qq.com> Date: Wed, 7 Jan 2026 11:24:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=88=B6=E6=B3=A8=E9=94=80=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=A2=84=E8=A7=88=E4=B8=8A=E4=BC=A0=E6=9D=90=E6=96=99?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AttachmentController.java | 25 ++- .../abnormal/service/TSAttachmentService.java | 3 + .../service/impl/TSAttachmentServiceImpl.java | 36 ++++ .../chinaweal/aiccs/common/util/FtpUtil.java | 163 ++++++++++++++++++ 4 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/chinaweal/aiccs/common/util/FtpUtil.java diff --git a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/controller/AttachmentController.java b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/controller/AttachmentController.java index f4fae59..7288390 100644 --- a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/controller/AttachmentController.java +++ b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/controller/AttachmentController.java @@ -501,10 +501,14 @@ public class AttachmentController extends BaseController { @PostMapping("uploadFileByBizseq") public RestResult uploadFileByBizseq(MultipartFile file, TSAttachment tsAttachment, HttpServletRequest request) { // 获得上传目录 -// String bizseq = tsAttachment.getBizseq(); - String bizseq = request.getParameter("bizSeqNo"); - String attachtype = request.getParameter("attachType"); -// String attachtype = tsAttachment.getAttachtype(); + String bizseq = tsAttachment.getBizseq(); + String attachtype = tsAttachment.getAttachtype(); + if (StringUtils.isBlank(bizseq)) { + bizseq = request.getParameter("bizSeqNo"); + } + if (StringUtils.isBlank(attachtype)) { + attachtype = request.getParameter("attachType"); + } AICUser curUser = getLoginUser(request); String realPath = CommonConfig.getUploadPathByDate(); // 判断对应模块加上文件夹区分 @@ -639,4 +643,17 @@ public class AttachmentController extends BaseController { return RestResult.ok(data); } + @GetMapping("/getMaterial") + @ApiOperation(value = "预览上传材料", produces = "application/octet-stream") + public void getMaterial(HttpServletResponse response, String id, String bizSeqNo, String attachType) { + if ((StringUtils.isBlank(bizSeqNo) || StringUtils.isBlank(attachType)) && StringUtils.isBlank(id)) { + throw new RuntimeException("参数异常"); + } + try { + attachmentService.getMaterial(response, id, bizSeqNo, attachType); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } diff --git a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/TSAttachmentService.java b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/TSAttachmentService.java index be48bb9..a20d992 100644 --- a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/TSAttachmentService.java +++ b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/TSAttachmentService.java @@ -12,6 +12,7 @@ import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.util.List; /** @@ -77,4 +78,6 @@ public interface TSAttachmentService extends BaseService { boolean saveYMLCClueFile(List files,Toincludeabn toincludeabn); IPage selectWritsListzx(Page page, AttachmentWritDto dto); + + void getMaterial(HttpServletResponse response, String id, String bizSeqNo, String attachType) throws IOException; } diff --git a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/impl/TSAttachmentServiceImpl.java b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/impl/TSAttachmentServiceImpl.java index a19830d..79e983a 100644 --- a/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/impl/TSAttachmentServiceImpl.java +++ b/src/main/java/com/chinaweal/aiccs/aiccs/abnormal/service/impl/TSAttachmentServiceImpl.java @@ -18,6 +18,7 @@ import com.chinaweal.aiccs.aiccs.abnormal.utils.Base64Utils; import com.chinaweal.aiccs.aiccs.revoke.Vo.DisableListVo; import com.chinaweal.aiccs.aiccs.revoke.entity.dto.AttachmentWritDto; import com.chinaweal.aiccs.common.constant.Constant; +import com.chinaweal.aiccs.common.util.FtpUtil; import com.chinaweal.aiccs.common.util.StringUtils; import com.chinaweal.aiccs.common.util.filestorage.FileStorageService; import com.chinaweal.aiccs.config.CommonConfig; @@ -39,6 +40,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -70,6 +72,8 @@ public class TSAttachmentServiceImpl extends BaseServiceImpl selectwritByPage(TSAttachment tsWritModel) { @@ -338,4 +342,36 @@ public class TSAttachmentServiceImpl extends BaseServiceImpl selectWritsListzx(Page page, AttachmentWritDto attachmentWritDto) { return baseMapper.selectWritsListzx(page, attachmentWritDto); } + + @Override + public void getMaterial(HttpServletResponse response, String id, String bizSeqNo, String attachType) throws IOException { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + if(StringUtils.isNotBlank(id)){ + queryWrapper.eq(TSAttachment::getAttachmentid, id); + } else { + queryWrapper.eq(TSAttachment::getBizseq, bizSeqNo) + .eq(TSAttachment::getAttachtype, attachType) + .orderBy(true,false,TSAttachment::getLauptime) + .last("limit 1"); + } + TSAttachment xrAttachment = this.getOne(queryWrapper); + if (xrAttachment != null) { + response.setHeader("Content-Disposition", + "attachment; filename=\"" + new String(xrAttachment.getFilename().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + "\""); + if (StringUtils.equals(xrAttachment.getCreatetype(), "2")){ +// ftpClientFile.writeToStream(xrAttachment.getFileUrl(), response.getOutputStream()); + ftpUtil.getFtpFile(response,xrAttachment.getFileurl()); + } else{ + fileStorageService.writeToStream(xrAttachment.getFileurl(), response.getOutputStream()); + //改用SFTP +// try { +// sftpUtil.download(xrAttachment.getFileurl(), response.getOutputStream()); +// } catch (SftpException e) { +// throw new RuntimeException(e); +// } + } + } else { + throw new RuntimeException("无此上传材料"); + } + } } diff --git a/src/main/java/com/chinaweal/aiccs/common/util/FtpUtil.java b/src/main/java/com/chinaweal/aiccs/common/util/FtpUtil.java new file mode 100644 index 0000000..b3581e8 --- /dev/null +++ b/src/main/java/com/chinaweal/aiccs/common/util/FtpUtil.java @@ -0,0 +1,163 @@ +package com.chinaweal.aiccs.common.util; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPReply; +import org.apache.commons.text.StringSubstitutor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.HashMap; + +@Configuration +@Slf4j +public class FtpUtil { + + @Value("${storage.ftp.ip}") + private String hostName; //= "127.0.0.1" ;FTP IP + @Value("${storage.ftp..port}") + private int port;//FTP 端口号 + @Value("${storage.ftp..username}") + private String username;//FTP 登录账号 + @Value("${storage.ftp..password}") + private String password; //FTP 登录密码 + + public static byte[] downloadfileByte(String hostname,int port,String username,String password,String pathname,String fileName) { + FTPClient ftpClient = new FTPClient(); + String LOCAL_CHARSET; + byte[] in2b = new byte[12]; + try{ + //连接FTP服务器 + ftpClient.connect(hostname, port); + //登录FTP服务器 + ftpClient.login(username, password); + if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK). + LOCAL_CHARSET = "UTF-8"; + } + ftpClient.enterLocalPassiveMode();// 设置被动模式 + ftpClient.setFileType(FTP.BINARY_FILE_TYPE);// 设置传输的模式 + ftpClient.changeWorkingDirectory(pathname); + + InputStream retrieveFileStream = null; + byte[] input2byte = null; + retrieveFileStream = ftpClient.retrieveFileStream(new String(fileName.getBytes(),"ISO-8859-1")); + input2byte = input2byte(retrieveFileStream); + + + // 使用流进行操作,例如读取内容或保存到本地 + BufferedReader reader = new BufferedReader(new InputStreamReader(retrieveFileStream)); + reader.close(); + + return input2byte; + } catch (IOException e){ + e.printStackTrace(); + } finally{ + if(ftpClient.isConnected()){ + try { + ftpClient.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return in2b; + } + + public static byte[] input2byte(InputStream inStream) throws IOException { + ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); + byte[] buff = new byte[100]; + int rc = 0; + while ((rc = inStream.read(buff, 0, 100)) > 0) { + swapStream.write(buff, 0, rc); + } + byte[] in2b = swapStream.toByteArray(); + return in2b; + } + + public void getFtpFile(HttpServletResponse response,String path){ + HashMap valuesMap = new HashMap<>(); + valuesMap.put("username", username); + valuesMap.put("password", password); + valuesMap.put("hostName", hostName); + valuesMap.put("port", port); + valuesMap.put("path", path); + + String template = "ftp://${username}:${password}@${hostName}:${port}${path}"; //"ftp://epsuser:1qaz!QAZ@19.15.77.57:8888/NLIQ/upload/440003000049189+20210803.pdf"; + String file = StringSubstitutor.replace(template, valuesMap); + + if(file.indexOf("/")!=-1) + file = parseFile(file); + + String fileName = null; //中文解码 + try { + fileName = new String(file.getBytes("GB2312"), "ISO_8859_1"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + fileName = fileName.replace(' ','+'); + + HashMap map = new HashMap(); + map.put("mp3","audio/mpeg"); + map.put("gif","image/gif"); + map.put("jpg","image/jpeg"); + map.put("png","image/png"); + map.put("bmp","image/bmp"); + map.put("html","text/html"); + map.put("htm","text/html"); + map.put("txt","text/plain"); + map.put("pdf","application/pdf"); + map.put("doc","application/msword"); + map.put("ppt","application/powerpoint"); + map.put("xls","application/vnd.ms-excel"); + map.put("exe","application/x-msdownload"); + map.put("zip","application/zip"); + map.put("rar","application/rar"); + map.put("chm","application/mshelp"); + map.put("xml","text/xml"); + + String fileExt = fileName.substring(file.lastIndexOf(".")+1).toLowerCase(); + String attachMimeDesc = (String)map.get(fileExt); + if(StringUtils.isEmpty(attachMimeDesc)){ + attachMimeDesc = "text/plain"; + } + + try { + InputStream inStream = new ByteArrayInputStream(downloadfileByte(hostName,port,username,password,fileName,path)); + response.reset(); + response.setContentType(attachMimeDesc); + + String saveName = fileName.substring(fileName.lastIndexOf(File.separator)+1); + + // if (".gif.png.jpg.jpeg.bmp.swf".indexOf(fileExt)==-1) { //附件下载 不是图片时采用! + response.addHeader("Content-Disposition", "filename=\""+saveName + "\""); //用IE打开 + // 不是图片时,不管是本地或数据库上的文件都直接从网页上显示出来。 + byte[] b = new byte[2048]; + int len; + ServletOutputStream sos = response.getOutputStream(); + while ((len = inStream.read(b)) > 0) { + sos.write(b, 0, len); + } + sos.close(); + sos.flush(); + inStream.close(); + } catch (Exception e) { + e.printStackTrace(); + String msg = "文件“" + fileName + "”不存在或网络错误,异常信息:" + e.getMessage(); + } + } + + //修正路径 + String parseFile(String file){ + if(file.indexOf("/")!=-1){ + String[] tmp = file.split("/"); + if(tmp.length>0) + return StringUtils.join(tmp,"\\"); + } + return file; + } +}