瀏覽代碼

增加SFTP

koul 4 年之前
父節點
當前提交
6307aeacc6

+ 37 - 0
src/main/java/com/uas/service/Impl/DownloadFilePathServiceImpl.java

@@ -42,6 +42,42 @@ public class DownloadFilePathServiceImpl implements DownloadFilePathService {
     		Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
 			Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
 			String fileName = "";
+			if ("BSD_HK-ZSD".equals(depot)){
+				sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
+						ftpMap.get("ip").toString(),
+						Integer.parseInt(ftpMap.get("port").toString()));
+				if(sftp!=null){
+					sftp.login();
+					List<MultipartFile> files = sftp.downloadMultipartFile(ftpMap.get("file").toString());
+					if(files!=null&&files.size()>0){
+						connect = JdbcUtil.getConnectBySob(sob);
+						if(connect!=null){
+							for (MultipartFile file:files) {
+								try {
+									Statement statement = connect.createStatement();
+									fileName += "," + file.getOriginalFilename().replaceAll(",", ",");
+									boolean bol = FileUtil.fileUp(statement, file, sob);
+									if (bol) {
+                                        if ("BSD_HK-ZSD".equals(depot)) {
+                                            Boolean rename =
+                                                    sftp.rename("/" + ftpMap.get("file").toString() + "/" + file.getName(),
+                                                            "/bosidahkhub/signback_bak/" + file.getName());
+                                        }
+										if ("BSD_HK-ZSD".equals(depot)) {
+											Boolean rename =
+													sftp.rename("/" + ftpMap.get("file").toString() + "/" + file.getName(), "/bosidahkhub/error_signback/" + file.getName());
+										}
+									}
+								} catch (Exception e) {
+									e.printStackTrace();
+									BaseUtil.getLogger().error(e.toString());
+									continue;
+								}
+							}
+						}
+					}
+				}
+			}else {
 				client = FtpUtil.connect(ftpMap);
 				if(client!=null){
 					List<File> files = FtpUtil.downloadAllFileByDir(client,ftpMap.get("file").toString());
@@ -73,6 +109,7 @@ public class DownloadFilePathServiceImpl implements DownloadFilePathService {
 						}
 					}
 				}
+			}
 			if(!"".equals(fileName)){
 				BaseUtil.logDownload(fileName.substring(1),ftpMap.get("ip").toString(),ftpMap.get("file").toString());
 			}

+ 42 - 8
src/main/java/com/uas/service/Impl/DownloadServiceImpl.java

@@ -1,9 +1,11 @@
 package com.uas.service.Impl;
 
+import com.jcraft.jsch.JSchException;
 import com.uas.service.DownloadService;
 import com.uas.util.BaseUtil;
 import com.uas.util.FtpUtil;
 import com.uas.util.JdbcUtil;
+import com.uas.util.SFTPUtil;
 import org.apache.commons.net.ftp.FTPClient;
 import org.codehaus.jackson.map.ObjectMapper;
 import org.dom4j.Document;
@@ -30,7 +32,7 @@ public class DownloadServiceImpl implements DownloadService {
 
 	@Override
 	public void downloadRun(){
-		//联合创泰-准时达
+		//博思达-准时达
 		downloadByDepotAndSob("BSD_HK-ZSD","BSD_HK","Upstar Technology (HK) Limited");
 
 
@@ -42,15 +44,34 @@ public class DownloadServiceImpl implements DownloadService {
 	public void downloadByDepotAndSob(String depot,String sob,String enterprise){
 		BaseUtil.getLogger().info("download xml" + " from "+depot);
 		FTPClient client = null;
+		SFTPUtil sftp = null;
 		List<String> sqls = new ArrayList<String>();
 		boolean executeRes = false;
 		Connection connect = null;
+
 		try {
 			Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
 			Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
-			client = FtpUtil.connect(ftpMap);
-			if(client!=null) {
-				List<File> files = FtpUtil.downloadAllFileByType(client, "XML");
+			if ("BSD_HK-ZSD".equals(depot)){
+				sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
+						ftpMap.get("ip").toString(),
+						Integer.parseInt(ftpMap.get("port").toString()));
+				try {
+					sftp.login();
+				} catch (JSchException e) {
+					e.printStackTrace();
+				}
+			}else {
+				client = FtpUtil.connect(ftpMap);
+			}
+			if(client!=null||sftp!=null) {
+				List<File> files = null;
+				if ("BSD_HK-ZSD".equals(depot)) {
+					files = sftp.download(ftpMap.get("downloadpath").toString());
+
+				}else{
+					files = FtpUtil.downloadAllFileByType(client, "XML");
+				}
 				String fileName = "";
 				if (files != null && files.size() > 0) {
 					connect = JdbcUtil.getConnectBySob(sob);
@@ -64,18 +85,28 @@ public class DownloadServiceImpl implements DownloadService {
 								}
 								Statement statement = connect.createStatement();
 								sqls = parseXmlFileToSqls(file, statement);
+								String s = file.getName().substring(0, file.getName().indexOf(".")) + ".xml";
 								if (sqls == null) {
+									sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "goodsreceipt/backup/" + s);
 									continue;
 								}
 
 								executeRes = JdbcUtil.executeSqls(connect, sqls);
 
 								if (executeRes) {
-									//移动处理完的文件到bak文件夹下
-									client.rename(file.getName(), "/HKHub/UPSTAR/reply_bak/" + file.getName());
+									if ("BSD_HK-ZSD".equals(depot)) {
+										Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "bosidahkhub/reply_bak/" + s);
+									} else {
+										//移动处理完的文件到bak文件夹下
+										client.rename(file.getName(), "bak/" + file.getName());
+									}
 								}else{
-									//移动处理完的文件到failed文件夹下
-									client.rename(file.getName(), "/HKHub/UPSTAR/erro_reply/" + file.getName());
+									if ("BSD_HK-ZSD".equals(depot)) {
+										Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "bosidahkhub/erro_reply/" + s);
+									} else {
+										//移动处理完的文件到failed文件夹下
+										client.rename(file.getName(), "failed/" + file.getName());
+									}
 								}
 								statement.close();
 							} catch (Exception e) {
@@ -99,6 +130,9 @@ public class DownloadServiceImpl implements DownloadService {
 				FtpUtil.closeFtpClient(client);
 				client = null;
 			}
+			if (sftp!=null){
+				sftp.logout();
+			}
 			//把当前的连接关闭
 			try {
 				if(connect!=null){

+ 1 - 1
src/main/java/com/uas/service/Impl/UploadServiceImpl.java

@@ -71,7 +71,7 @@ public class UploadServiceImpl implements UploadService {
 									continue;
 								}
 								System.out.println(config);
-								if ("SF".equals(depot)||"ZX".equals(depot)){
+								if ("ZSD".equals(depot)){
 									sftp = new SFTPUtil(config.get("user").toString(),config.get("password").toString(),config.get("ip").toString(),
 											Integer.parseInt(config.get("port").toString()));
 									try {

+ 7 - 7
src/main/resources/properties/ftpconfig.properties

@@ -1,11 +1,11 @@
 {
     "BSD_HK-ZSD":{
-        "ip":"47.106.234.187",
-        "port":21,
-        "user":"lhct",
-        "password":"bE!8M3#5y0",
-        "downloadpath":"HKHub/UPSTAR/reply",
-        "in":"HKHub/UPSTAR/appointment_in",
-        "file":"HKHub/UPSTAR/signback"
+        "ip":"sftp.jusdaglobal.com",
+        "port":50035,
+        "user":"bosida",
+        "password":"cuIgJdg2gq6W",
+        "downloadpath":"bosidahkhub/reply",
+        "in":"bosidahkhub/appointment_in",
+        "file":"bosidahkhub/signback"
     }
 }