Browse Source

数据中心JAR包升级

zhouy 1 year ago
parent
commit
340fcb7a85

+ 7 - 1
pom.xml

@@ -126,10 +126,16 @@
 			<version>1.0.26</version>
 		</dependency>
 
-		<dependency>
+	<!--	<dependency>
 			<groupId>org.samba.jcifs</groupId>
 			<artifactId>jcifs</artifactId>
 			<version>1.3.17</version>
+		</dependency>-->
+
+		<dependency>
+			<groupId>org.codelibs</groupId>
+			<artifactId>jcifs</artifactId>
+			<version>2.1.34</version>
 		</dependency>
 
 		<dependency>

+ 10 - 11
src/main/java/com/uas/eis/serviceImpl/EDCBakServiceImpl.java

@@ -9,7 +9,6 @@ import com.uas.eis.utils.DateUtil;
 import com.uas.eis.utils.SmbUtil;
 import jcifs.smb.SmbFile;
 import jcifs.smb.SmbFileInputStream;
-import jcifs.smb.SmbSession;
 import org.apache.commons.lang.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -22,7 +21,6 @@ import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import java.io.BufferedInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -77,6 +75,7 @@ public class EDCBakServiceImpl implements EDCBakService {
                         /**相关路径匹配*/
                         if(smbFile !=null) {
                             SmbFile dataFile = SmbUtil.getCurrentFile(IP,dataRootPath);
+                            logger.info(" matchIP:"+IP+","+testStep+" :dataPath:"+ dataFile.getPath());
                             List <SmbFile> files = SmbUtil.getChildFiles(dataFile, testStep, "xls");
                             List <SmbFile> files1 = SmbUtil.getChildFiles(dataFile, testStep, "jdf");
                             List <SmbFile> files2 = SmbUtil.getChildFiles(dataFile, testStep, "njdf");
@@ -85,19 +84,18 @@ public class EDCBakServiceImpl implements EDCBakService {
                                 counterpath = String.valueOf(map.get("COUNTERPATH_"));
                                 final String filename = counterpath.substring(counterpath.lastIndexOf("\\")+1);
                                 smbFile = SmbUtil.getCurrentFile(IP,counterbakRootPath+filename);
-
                                 map.put("COUNTERPATH_", smbFile.getPath());
-                                Optional<SmbFile> optional = files.stream().filter(file -> mappingSuccess(filename,file.getDate())).findFirst();
+                                Optional<SmbFile> optional = files.stream().filter(file -> mappingSuccess(filename,file)).findFirst();
                                 //遍历到了对应的Data文件存在
                                 if (optional.isPresent()){
                                     map.put("DATAPATH_", optional.get().getPath());
                                 }
-                                optional = files1.stream().filter(file -> mappingSuccess(filename,file.getDate())).findFirst();
+                                optional = files1.stream().filter(file -> mappingSuccess(filename,file)).findFirst();
                                 //遍历到了对应的Data文件存在
                                 if (optional.isPresent()){
                                     map.put("JDFPATH_", optional.get().getPath());
                                 }
-                                optional = files2.stream().filter(file -> mappingSuccess(filename,file.getDate())).findFirst();
+                                optional = files2.stream().filter(file -> mappingSuccess(filename,file)).findFirst();
                                 if (optional.isPresent()){
                                     map.put("JDFPATH_", optional.get().getPath());
                                 }
@@ -126,7 +124,7 @@ public class EDCBakServiceImpl implements EDCBakService {
                     }else {
                         String chipcode=map.get("CHIPCODE_").toString();
                         String ID=map.get("OBJECT_RRN").toString();
-                        baseDao.execute("insert into EDCsyscfaillog(el_id,el_ip,el_devcode,el_teststep,el_synctime,el_chipcode,el_reason) values(EDCsyscfaillog_seq.nextval,'"+IP+"','"+devcode+"','"+testStep+"',sysdate,'"+chipcode+"','未匹配到对应JDF文件,无法归档')");
+                        baseDao.execute("insert into EDCsyscfaillog(el_id,el_ip,el_devcode,el_teststep,el_synctime,el_chipcode,el_reason) values(EDCsyscfaillog_seq.nextval,'"+IP+"','"+devcode+"','"+testStep+"',sysdate,'"+chipcode+"','未匹配到对应JDF文件,无法归档"+map.get("COUNTERPATH_")+"')");
                         baseDao.execute("update datacenter$mestemp set fail=-1 where OBJECT_RRN="+ID);
                     }
                 }
@@ -199,7 +197,7 @@ public class EDCBakServiceImpl implements EDCBakService {
                             String filename= counterBaKFile.getName();
                             String name = filename.substring(0,filename.indexOf("."));
                             String newFilename = name+"("+Count+")";
-                            counterBaKFile=SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/FP/"+newFilename+".xls");
+                            counterBaKFile= SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/FP/"+newFilename+".xls");
                         }
                         path1=counterCenterFile.getPath();
                         path3=counterBaKFile.getPath();
@@ -223,7 +221,7 @@ public class EDCBakServiceImpl implements EDCBakService {
                                 String filename= dataBakFile.getName();
                                 String name = filename.substring(0,filename.indexOf("."));
                                 String newFilename = name+"("+Count+")";
-                                dataBakFile=SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/SJ/"+newFilename+".xls");
+                                dataBakFile= SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/SJ/"+newFilename+".xls");
                             }
                             path2=dataCenterFile.getPath();
                             path4=dataBakFile.getPath();
@@ -254,7 +252,7 @@ public class EDCBakServiceImpl implements EDCBakService {
                                 String name = filename.substring(0,filename.indexOf("."));
                                 String suffix = filename.substring(filename.lastIndexOf("."));
                                 String newFilename = name+"("+Count+")";
-                                jdfBakFile=SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/SJ/"+newFilename+filesuffix);
+                                jdfBakFile= SmbUtil.getCurrentFile(IP,currentUploadPath+currentKind+"/SJ/"+newFilename+filesuffix);
                             }
                             path5=jdfCenterFile.getPath();
                             path6=jdfBakFile.getPath();
@@ -297,8 +295,9 @@ public class EDCBakServiceImpl implements EDCBakService {
         }
         logger.info(" matchIP:"+IP+" end;");
     }
-    private boolean mappingSuccess(String fileName , Long time) {
+    private boolean mappingSuccess(String fileName , SmbFile file) {
         boolean match=false;
+        Long time = file.getDate();
         //时间在同一分钟改的可以匹配
         if(fileName.substring(2).startsWith(DateUtil.format(new Date(time), Constant.YMDHM))){
             match=true;

+ 36 - 13
src/main/java/com/uas/eis/utils/SmbUtil.java

@@ -1,12 +1,18 @@
 package com.uas.eis.utils;
 
-import jcifs.smb.NtlmPasswordAuthentication;
+import jcifs.CIFSContext;
+import jcifs.CIFSException;
+import jcifs.config.PropertyConfiguration;
+import jcifs.context.BaseContext;
 import jcifs.smb.SmbFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 
 /**
  * @Author: zhouy
@@ -17,8 +23,22 @@ public class SmbUtil {
     private static String pwd = "0DlbAdmin0"; //密码
     public static String centerIP = "172.16.0.15";
     public static String centerPath = "D$/";
-
-
+    private static Logger logger = LoggerFactory.getLogger("SmbUtil");
+    public static PropertyConfiguration getConfig(){
+        Properties ps = new Properties();
+        ps.setProperty("jcifs.smb.client.domain", "");
+        ps.setProperty("jcifs.smb.client.username", user);
+        ps.setProperty("jcifs.smb.client.password", pwd);
+        // 这个 disable 属性默认为 false , 不设置为 true 的话读取共享文件会比较慢
+        ps.setProperty("jcifs.smb.client.dfs.disabled", "true");
+        try {
+            return new PropertyConfiguration(ps);
+        } catch (CIFSException e) {
+            logger.error("getConfiguration:", e);
+            e.printStackTrace();
+            return  null;
+        }
+    }
     /**
      * 获取远程访问地址
      */
@@ -26,16 +46,17 @@ public class SmbUtil {
         return  String.format("smb://@%s%s%s",serverID,"/",rootPath);
     }
 
-    public static NtlmPasswordAuthentication getAuth(){
-        return new NtlmPasswordAuthentication(null,user,pwd);
+    public static CIFSContext getContext(){
+       return new BaseContext(getConfig());
     }
 
     public static SmbFile getRootFile(String serverID, String rootPath){
         SmbFile smbFile = null;
         try {
-            smbFile = new SmbFile(getSmbRootUrl(serverID,rootPath),getAuth());
+            smbFile = new SmbFile(getSmbRootUrl(serverID,rootPath),getContext());
         } catch (MalformedURLException e) {
             e.printStackTrace();
+            logger.info("getRootFile:{},{}",serverID, rootPath);
         }
         return smbFile;
 
@@ -43,10 +64,11 @@ public class SmbUtil {
 
     public static SmbFile getCurrentFile(String serverID, String path){
         SmbFile smbFile = null;
-        try {
-            smbFile = new SmbFile(getSmbRootUrl(serverID,path),getAuth());
+       try {
+            smbFile = new SmbFile(getSmbRootUrl(serverID,path),getContext());
         } catch (MalformedURLException e) {
             e.printStackTrace();
+            logger.info("getCurrentFile:{},{}",serverID, path);
         }
         return smbFile;
     }
@@ -54,9 +76,10 @@ public class SmbUtil {
     public static SmbFile getCurrentFile(String path){
         SmbFile smbFile = null;
         try {
-            smbFile = new SmbFile(path,getAuth());
+            smbFile = new SmbFile(path,getContext());
         } catch (MalformedURLException e) {
             e.printStackTrace();
+            logger.info("getCurrentFile:{}",path);
         }
         return smbFile;
 
@@ -66,7 +89,7 @@ public class SmbUtil {
         List<String> paths = new ArrayList<>();
         SmbFile file = null;
         try {
-            smbFile.connect();
+          //  smbFile.connect();
             if(smbFile.isDirectory()){
                 SmbFile[] files = smbFile.listFiles();
                 for (int i = 0; i<files.length; i++){
@@ -89,7 +112,7 @@ public class SmbUtil {
         SmbFile childFile = null;
         String fileName = null;
         try {
-            smbFile.connect();
+           // smbFile.connect();
             if(smbFile.isDirectory()){
                 SmbFile [] childs = smbFile.listFiles();
                 for (int i = 0; i<childs.length; i++){
@@ -102,8 +125,8 @@ public class SmbUtil {
                     }
                 }
             }
-        } catch (IOException e) {
-            e.printStackTrace();
+        } catch (Exception e) {
+            logger.error("getChildFiles-Error", e);
         }
         return files;
     }

+ 117 - 0
src/main/java/com/uas/eis/utils/SmbUtilSmb1.java

@@ -0,0 +1,117 @@
+package com.uas.eis.utils;
+
+import jcifs.smb.NtlmPasswordAuthentication;
+import jcifs.smb.SmbFile;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Author: zhouy
+ * @Date: 2020/3/30 14:58
+ */
+public class SmbUtilSmb1 {
+    private static String user = "administrator"; //用户名
+    private static String pwd = "0DlbAdmin0"; //密码
+    public static String centerIP = "172.16.0.15";
+    public static String centerPath = "D$/";
+    private static Logger logger = LoggerFactory.getLogger("SmbUtil");
+    /**
+     * 获取远程访问地址
+     */
+    public static String getSmbRootUrl(String serverID, String rootPath){
+        return  String.format("smb://@%s%s%s",serverID,"/",rootPath);
+    }
+
+    public static NtlmPasswordAuthentication getAuth(){
+       // return new NtlmPasswordAuthentication(null,user,pwd);
+        return  null;
+    }
+
+    public static SmbFile getRootFile(String serverID, String rootPath){
+        SmbFile smbFile = null;
+       /* try {
+            smbFile = new SmbFile(getSmbRootUrl(serverID,rootPath),getAuth());
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+            logger.info("getRootFile:{},{}",serverID, rootPath);
+        }*/
+        return smbFile;
+
+    }
+
+    public static SmbFile getCurrentFile(String serverID, String path){
+        SmbFile smbFile = null;
+     /*   try {
+            smbFile = new SmbFile(getSmbRootUrl(serverID,path),getAuth());
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+            logger.info("getCurrentFile:{},{}",serverID, path);
+        }*/
+        return smbFile;
+    }
+
+    public static SmbFile getCurrentFile(String path){
+        SmbFile smbFile = null;
+       /* try {
+            smbFile = new SmbFile(path,getAuth());
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+            logger.info("getCurrentFile:{}",path);
+        }*/
+        return smbFile;
+
+    }
+
+    public static List<String> getChildFiles(SmbFile smbFile,String startChar){
+        List<String> paths = new ArrayList<>();
+        SmbFile file = null;
+        try {
+            smbFile.connect();
+            if(smbFile.isDirectory()){
+                SmbFile[] files = smbFile.listFiles();
+                for (int i = 0; i<files.length; i++){
+                    file = files[i];
+                    if(file.isFile()){
+                        if(file.getName().startsWith(startChar)){
+                            paths.add(files[i].getPath());
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return paths;
+    }
+
+    public static List<SmbFile> getChildFiles(SmbFile smbFile,String startChar, String docType){
+        List<SmbFile> files = new ArrayList<>();
+        SmbFile childFile = null;
+        String fileName = null;
+        try {
+            smbFile.connect();
+            if(smbFile.isDirectory()){
+                SmbFile [] childs = smbFile.listFiles();
+                for (int i = 0; i<childs.length; i++){
+                    childFile = childs[i];
+                    if(childFile.isFile()){
+                        fileName = childFile.getName();
+                        if(fileName.toUpperCase().startsWith(startChar) && fileName.endsWith(docType)) {
+                            files.add(childFile);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            logger.error("getChildFiles-Error", e);
+        }
+        return files;
+    }
+}
+

+ 69 - 13
src/test/java/com/uas/eis/UasEisApplicationTests.java

@@ -1,31 +1,30 @@
 package com.uas.eis;
 
-import com.uas.eis.dao.BaseDao;
-import com.uas.eis.entity.DataChip;
-import com.uas.eis.entity.EquipConfig;
-import com.uas.eis.service.DataDealService;
-import com.uas.eis.service.EDCBakService;
-import com.uas.eis.utils.CollectionUtil;
+
+import com.uas.eis.utils.SmbUtil;
+import com.uas.eis.utils.SmbUtilSmb1;
+import jcifs.CIFSContext;
+import jcifs.CIFSException;
+import jcifs.config.PropertyConfiguration;
+import jcifs.context.BaseContext;
+import jcifs.smb.SmbFile;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.util.Date;
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.List;
-import java.util.Map;
-import java.util.concurrent.CountDownLatch;
-import java.util.stream.Collectors;
+import java.util.Properties;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = {UasEisApplication.class})
 public class UasEisApplicationTests {
 	private final Logger logger = LoggerFactory.getLogger(this.getClass());
-
+/*
 	@Autowired
 	private DataDealService dataDealService;
 	@Autowired
@@ -75,7 +74,64 @@ public class UasEisApplicationTests {
 	}
 	@Test
 	public void test(){
+		String ip = "172.16.3.17";
+		SmbFile sb = SmbUtil.getRootFile(ip,"c$");
+		try {
+			System.out.println(ip+"测试连接");
+			sb.exists();
+			System.out.println(ip+"测试ok连接");
+		} catch (SmbException e) {
+			System.out.println(ip+"连接失败");
+			e.printStackTrace();
+		}
+
+	}*/
+	/*@Test
+	public void test2(){
+		NtlmPasswordAuthentication npa =  new NtlmPasswordAuthentication(null,"administrator","0DlbAdmin0");
 
+
+		try {
+			SmbFile sf = new SmbFile(SmbUtil.getSmbRootUrl("172.16.3.22","c$"),npa);
+			sf.connect();
+		} catch (MalformedURLException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}*/
+	//@Test
+	public void test3 (){
+		Properties ps = new Properties();
+		ps.setProperty("jcifs.smb.client.domain", "");
+		ps.setProperty("jcifs.smb.client.username", "administrator");
+		ps.setProperty("jcifs.smb.client.password", "0DlbAdmin0");
+		// 这个 disable 属性默认为 false , 不设置为 true 的话读取共享文件会比较慢
+		ps.setProperty("jcifs.smb.client.dfs.disabled", "true");
+		CIFSContext cifs = null;
+		try {
+			cifs = new BaseContext(new PropertyConfiguration(ps));
+		} catch (CIFSException e) {
+			e.printStackTrace();
+		}
+		try {
+			SmbFile dir = new SmbFile(SmbUtilSmb1.getSmbRootUrl("172.16.3.15","c$"),  cifs );
+			dir.connect();
+		} catch (MalformedURLException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	@Test
+	public void test4(){
+		SmbFile dataFile = SmbUtil.getCurrentFile("172.16.3.18","c$/DTS/data/");
+		try {
+			dataFile.connect();
+			List<SmbFile> files = SmbUtil.getChildFiles(dataFile, "B", "xls");
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 	}
 
 }