瀏覽代碼

修改为SFTP访问方式

wangjun 22 小時之前
父節點
當前提交
647729c939

+ 42 - 43
code/jyyy/nckd-jimin-jyyy-fi/src/main/java/nckd/jimin/jyyy/fi/webapi/AttachmentFileUtil.java

@@ -12,10 +12,12 @@ import kd.bos.logging.LogFactory;
 import kd.bos.servicehelper.AttachmentServiceHelper;
 import kd.bos.servicehelper.MetadataServiceHelper;
 import kd.bos.util.FileNameUtils;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+import java.util.Properties;
 import nckd.jimin.jyyy.fi.common.CommonHelperUtils;
-import org.apache.commons.net.ftp.FTP;
-import org.apache.commons.net.ftp.FTPClient;
-import org.apache.commons.net.ftp.FTPReply;
+
 
 import java.io.*;
 import java.net.HttpURLConnection;
@@ -51,37 +53,37 @@ public class AttachmentFileUtil {
         attachItem.put("uid", uid1);
         InputStream inputStream = null;
         ByteArrayOutputStream outStream = null;
-        HttpURLConnection conn = null;
         String tempUrl = null;
         int size = 0;
-        FTPClient ftpClient = null;
+        ChannelSftp ftpClient = null;
+        Session sshSession = null;
         try {
-            ftpClient = new FTPClient();
+            JSch jsch = new JSch();
             // 连接到FTP服务器
             Map<String, String> conf = CommonHelperUtils.getCommonParams("FTP");
-            ftpClient.connect(conf.get("IP"), Integer.parseInt(conf.get("PORT")));
-            ftpClient.login(conf.get("USER"), conf.get("PASSWD"));
-            logger.info("FTP:"+conf.get("USER")+conf.get("PASSWD")+conf.get("IP")+conf.get("PORT"));
-            if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
-                ftpClient.disconnect();
-                return null;
-            }
-
-            // 设置文件类型为二进制
-            ftpClient.type(FTP.BINARY_FILE_TYPE);
-            ftpClient.enterLocalPassiveMode();  // 切换到被动模式
-            fileUrl = encode(fileUrl,"UTF-8");
-            logger.info("FTP fileUrl:" + fileUrl);
-            String dir = ftpClient.printWorkingDirectory();
-            //System.out.println(dir);
-            //ftpClient.changeWorkingDirectory("Files/CMB");
-            //System.out.println(ftpClient.printWorkingDirectory());
-            logger.info("FTP当前目录:"+ftpClient.printWorkingDirectory());
-            //fileUrl = dir + fileUrl;
-
-            inputStream = ftpClient.retrieveFileStream(fileUrl);
+            String loginName = conf.get("USER");
+            String loginPassword = conf.get("PASSWD");
+            String server = conf.get("IP");
+            Integer port = Integer.parseInt(conf.get("PORT"));
+            logger.info("FTP:"+loginName+server+conf.get("PORT"));
+            ChannelSftp.LsEntry isEntity = null;
+            // 获取sshSession
+            sshSession = jsch.getSession(loginName, server, port);
+            // 添加s密码
+            sshSession.setPassword(loginPassword);
+            Properties sshConfig = new Properties();
+            sshConfig.put("StrictHostKeyChecking", "no");
+            sshSession.setConfig(sshConfig);
+            // 开启sshSession链接
+            sshSession.connect();
+            // 获取sftp通道
+            ftpClient = (ChannelSftp) sshSession.openChannel("sftp");
+            // 开启
+            ftpClient.connect();
+
+            inputStream = ftpClient.get(fileUrl);
             logger.info("FTP inputStream -- size:"+inputStream.available());
-            //System.out.println("inputStream:"+inputStream);
+            System.out.println("inputStream:"+inputStream);
 
             outStream = new ByteArrayOutputStream();
             byte[] buffer = new byte[1024];
@@ -107,9 +109,9 @@ public class AttachmentFileUtil {
             logger.info("FTP tempUrl -- inputStream -- size:" + CacheFactory.getCommonCacheFactory().getTempFileCache().getInputStream(tempUrl).available());
             System.out.println(":tempUrl"+tempUrl);
         } catch (Exception e) {
-            logger.info("FTP 附件上传失败,堆栈信息:",e);
+            logger.info("FTP 附件上传失败,堆栈信息:",e.getMessage());
             result.put("isSuccess", "false");
-            result.put("msg", "转文件流异常"+e);
+            result.put("msg", "转文件流异常"+e.getMessage());
             return result;
         } finally {
             try {
@@ -119,12 +121,11 @@ public class AttachmentFileUtil {
                 if(outStream !=null){
                     outStream.close();
                 }
-                if (ftpClient.isConnected()) {
-                    ftpClient.logout();
-                    ftpClient.disconnect();
+                if(ftpClient.getSession().isConnected()){
+                    ftpClient.getSession().disconnect();
                 }
             } catch (Exception e) {
-                logger.info("FTP 附件上传失败,堆栈信息01:",e.getMessage());
+                logger.info("附件上传失败,堆栈信息01:",e.getMessage());
                 result.put("isSuccess", "false");
                 result.put("msg", "文件流关闭异常:"+e.getMessage());
                 return result;
@@ -137,7 +138,7 @@ public class AttachmentFileUtil {
 
             //上传到临时文件服务器
             String actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity, pk, (String)attachItem.get("name"));
-            logger.info("FTP actUrl:"+actUrl);
+            logger.info("actUrl:"+actUrl);
             System.out.println("actUrl:"+actUrl);
             TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
             InputStream in = cache.getInputStream(tempUrl);
@@ -146,7 +147,7 @@ public class AttachmentFileUtil {
             String uuid = UUID.randomUUID().toString().replace("-", "");
             String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(),
                     requestContext.getAccountId(), uuid, fileName);
-            System.out.println("pathParam"+pathParam);
+            System.out.println("FTP pathParam"+pathParam);
 
             result.put("pathParam", pathParam);
             FileItem fileItem = new FileItem(fileName, pathParam, in);
@@ -162,12 +163,10 @@ public class AttachmentFileUtil {
             AttachmentServiceHelper.upload(entity, pk, attachKey, attachments);
 
         }catch(Exception e1){
-            logger.info("FTP 附件上传失败,堆栈信息e1:",e1);
+            logger.info("FTP 附件上传失败,堆栈信息e1:",e1.getMessage());
             result.put("isSuccess", "false");
-            result.put("msg", "上传服务器异常:"+e1);
+            result.put("msg", "上传服务器异常:"+e1.getMessage());
             return result;
-        }finally {
-
         }
         result.put("isSuccess", "true");
         result.put("msg", "附件上传成功");
@@ -184,7 +183,7 @@ public class AttachmentFileUtil {
             //上传到临时文件服务器
             System.out.println(tempUrl);
             String actUrl = AttachmentServiceHelper.saveTempToFileService(tempUrl,appId,entity, pk,fileName);
-            System.out.println("FTP actUrl:"+actUrl);
+            System.out.println("actUrl:"+actUrl);
             TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
             InputStream in = cache.getInputStream(tempUrl);
             FileService service = FileServiceFactory.getAttachmentFileService();
@@ -192,7 +191,7 @@ public class AttachmentFileUtil {
             String uuid = UUID.randomUUID().toString().replace("-", "");
             String pathParam = FileNameUtils.getAttachmentFileName(requestContext.getTenantId(),
                     requestContext.getAccountId(), uuid, fileName);
-            System.out.println("FTP pathParam:"+pathParam);
+            System.out.println("pathParam:"+pathParam);
             FileItem fileItem = new FileItem(fileName, pathParam, in);
 
             //从临时上传至 文件服务器
@@ -352,4 +351,4 @@ public class AttachmentFileUtil {
         connection.disconnect();
     }
 
-}
+}