Browse Source

代码更新

longht 4 years ago
parent
commit
9e04d6bdeb

+ 6 - 0
META-INF/PubDevTool.upm

@@ -14,6 +14,12 @@
             <interface>nc.itf.pubdev.pubtool.logserver.IDevExecLogServer_RequiresNew</interface>
             <implementation>nc.impl.pubtool.logserver.DevExecLogServer_RequiresNewimpl</implementation>
         </component> 
+        
+          <!--BDGain-->
+        <component remote="true" singleton="true" tx="CMT">
+            <interface>nc.itf.pubdev.pubtool.BDTool.IBDDatasGainTool</interface>
+            <implementation>nc.impl.pubdev.pubtool.BDTool.BDDatasGainToolimpl</implementation>
+        </component> 
 	</public>
 	<private>
 	</private>

+ 18 - 0
src/private/nc/impl/pubdev/pubtool/BDTool/BDDatasGainToolimpl.java

@@ -0,0 +1,18 @@
+package nc.impl.pubdev.pubtool.BDTool;
+
+import java.util.List;
+
+import nc.impl.pubdev.pubtool.execmethod.GainDefdocUtil;
+import nc.itf.pubdev.pubtool.BDTool.IBDDatasGainTool;
+import nc.vo.bd.defdoc.DefdocVO;
+import nc.vo.pub.BusinessException;
+
+public class BDDatasGainToolimpl implements IBDDatasGainTool{
+
+	@Override
+	public List<DefdocVO> GainDefDocByListcode(String doclistname, String conditon) throws BusinessException {
+		// TODO Auto-generated method stub
+		return new GainDefdocUtil().gainDefdoc(doclistname,conditon);
+	}
+
+}

+ 1 - 1
src/private/nc/impl/pubdev/pubtool/ExcelUtil.java

@@ -1,4 +1,4 @@
-package nc.impl.pubdev.pubtool;
+package nc.impl.pubdev.pubtool.execmethod;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;

+ 1 - 1
src/private/nc/impl/pubdev/pubtool/FtpUtil.java

@@ -1,4 +1,4 @@
-package nc.impl.pubdev.pubtool;
+package nc.impl.pubdev.pubtool.execmethod;
 
 import java.io.BufferedReader;
 import java.io.IOException;

+ 230 - 0
src/private/nc/impl/pubdev/pubtool/execmethod/FtpsUtil.java

@@ -0,0 +1,230 @@
+package nc.impl.pubdev.pubtool.execmethod;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.SocketException;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.commons.net.ftp.FTPSClient;
+
+import nc.pub.Proptool.PropertiesUtil;
+import nc.vo.pubapp.pattern.exception.ExceptionUtils;
+
+public class FtpsUtil {
+	
+	private FTPSClient ftpClient;
+	private String strencoding = "ISO-8859-1";
+	private String ip = ""; // 服务器IP地址
+	private String userName = ""; // 用户名
+	private String userPwd = ""; // 密码
+	private int port = 21; // 端口号
+	private String basePath = ""; // 读取文件的存放目录
+
+    /**
+     * @author longht
+     * @param propfilename  ftp参数文件
+     * @param type 1对应读取excel文件类型
+     * @return 返回类型为Map<String,Object> key =文件名  value=对应的数据 
+     * 
+     */
+	public Map<String, Object>  doFtpReadFile(String propfilename,int type) {
+		Map<String, Object> result =null;
+		try {
+			ftpConnectSet(propfilename);
+			result = readFileList(null, type);
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} finally {
+			closeServer();
+		}
+		return result;
+	}
+
+
+	
+	//设置ftp连接参数
+	private void ftpConnectSet(String propfilename) {
+		
+		ip = PropertiesUtil.getProperty(propfilename, "ftp_ip");
+		userName = PropertiesUtil.getProperty(propfilename, "ftp_username");
+		userPwd = PropertiesUtil.getProperty(propfilename, "ftp_pwd");
+		port = Integer.parseInt(PropertiesUtil.getProperty(propfilename, "ftp_port"));
+		basePath = PropertiesUtil.getProperty(propfilename, "ftp_basePath");
+		// 以当前系统时间拼接文件名
+		this.connectServer(ip, port, userName, userPwd, basePath);
+	}
+
+	/**
+	 * 连接并登陆ftp
+	 * @param ip
+	 * @param port
+	 * @param userName
+	 * @param userPwd
+	 * @param path
+	 * @throws SocketException
+	 * @throws IOException     function:连接到服务器
+	 */
+	private  void connectServer(String ip, int port, String userName, String userPwd, String path) {
+		ftpClient = new FTPSClient("TLS", true);
+		try {
+			ftpClient.setDataTimeout(300 * 1000);
+			ftpClient.setDefaultTimeout(300 * 1000);
+			// 连接
+			ftpClient.connect(ip, port);
+			// 登录
+			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+			boolean flag = ftpClient.login(userName, userPwd);
+			ftpClient.execPROT("P");//加上这个才能正常读取
+			if (path != null && path.length() > 0) {
+				// 跳转到指定目录
+				ftpClient.changeWorkingDirectory(path);
+				strencoding = ftpClient.getControlEncoding();
+			}
+		} catch (SocketException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * 关闭连接
+	 * @throws IOException function:关闭连接
+	 */
+	private void closeServer() {
+		if (ftpClient.isConnected()) {
+			try {
+				ftpClient.logout();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}finally {
+				try {
+					ftpClient.disconnect();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				
+			}
+		}
+	}
+
+	/**
+	 * @param path
+	 * @return function:读取指定目录下的文件名
+	 * @throws IOException
+	 */
+	private Map<String, Object> readFileList(Map<String, String> mapdir, int type) throws IOException {
+		// 构造返回值
+		Map<String, Object> reDatas = new HashMap<String, Object>();
+		// 获得指定目录下所有文件名
+		FTPFile[] ftpFiles = null;
+		try {
+			ftpClient.enterLocalPassiveMode();
+			ftpFiles = ftpClient.listFiles();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
+			ftpClient.changeWorkingDirectory(this.basePath);
+			FTPFile file = ftpFiles[i];
+			if (file.isFile()) {
+				String filename = new String(file.getName().getBytes(strencoding), "GBK").toLowerCase();
+				if (1 == type) {
+
+					ArrayList<ArrayList<Object>> result = readExcelFile(file.getName());
+					reDatas.put(filename, result);
+				}
+			}
+		}
+		return reDatas;
+	}
+
+	/**
+	 * @param ftpfile
+	 * @return function:从服务器上读取指定的文件
+	 * @throws ParseException
+	 * @throws IOException
+	 */
+	private ArrayList<ArrayList<Object>> readExcelFile(String ftpfile) {
+		ArrayList<ArrayList<Object>> datas = null;
+		InputStream ins = null;
+		try {
+			ftpClient.enterLocalPassiveMode();
+			// 从服务器上读取指定的文件
+			ins = ftpClient.retrieveFileStream(ftpfile);
+			if (null == ins)
+				return null;
+			if (ftpfile.endsWith("xlsx")) {
+				// 处理ecxel2007
+				datas = new ExcelUtil().readExcel2007(ins);
+			} else if (ftpfile.endsWith("xls")) {
+				// 处理ecxel2003
+				datas = new ExcelUtil().readExcel2003(ins);
+			} else if (ftpfile.endsWith("csv")) {
+				datas = new ExcelUtil().readCsv(ins);
+			} else {
+				ExceptionUtils.wrappBusinessException("导入失败,系统只接收xls,xlsx,csv类型文件");
+				return null;
+			}
+
+		} catch (IOException e) {
+
+			e.printStackTrace();
+		} finally {
+			if (ins != null) {
+				try {
+					ins.close();
+					// 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
+					ftpClient.getReply();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+		}
+		return datas;
+	}
+
+	/**
+	 * @param fileName
+	 * @return function:从服务器上读取指定的文件
+	 * @throws ParseException
+	 * @throws IOException
+	 */
+	private List<String> readFile(String fileName, List<String> list) throws ParseException {
+		if (!(null != list && list.size() > 0))
+			list = new ArrayList<String>();
+		InputStream ins = null;
+		try {
+			ftpClient.enterLocalPassiveMode();
+			// 从服务器上读取指定的文件
+			ins = ftpClient.retrieveFileStream(fileName);
+			if (null == ins)
+				return list;
+			BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "GBK"));
+			String line;
+			while ((line = reader.readLine()) != null) {
+				line = "," + line;
+				line = line.replaceAll(";", ",");
+				list.add(line);
+			}
+			reader.close();
+			if (ins != null) {
+				ins.close();
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return list;
+	}
+}

+ 52 - 0
src/private/nc/impl/pubdev/pubtool/execmethod/GainDefdocUtil.java

@@ -0,0 +1,52 @@
+package nc.impl.pubdev.pubtool.execmethod;
+
+import java.util.List;
+
+import nc.bs.dao.BaseDAO;
+import nc.bs.dao.DAOException;
+import nc.bs.logging.Logger;
+import nc.jdbc.framework.processor.BeanListProcessor;
+import nc.vo.bd.defdoc.DefdocVO;
+import nc.vo.pub.BusinessException;
+import nc.vo.pubapp.pattern.pub.PubAppTool;
+
+public class GainDefdocUtil {
+
+	public List<DefdocVO> gainDefdoc(String doclistname, String conditon) throws BusinessException {
+		// TODO Auto-generated method stub
+		Logger.info("꿴璂菱땍屢도갭±"+doclistname+"×start");
+		String QDefdocSql = " SELECT doc.* FROM bd_defdoc doc\n" +
+						" inner join bd_defdoclist doclist\n" + 
+						" on doclist.pk_defdoclist = doc.pk_defdoclist\n" + 
+						" and doclist.code = 'TCORGBD001'\n" + 
+						" where doc.enablestate = '2'\n ";
+        if(!PubAppTool.isNull(conditon))
+        	QDefdocSql =QDefdocSql+conditon;
+		List<DefdocVO> DefdocList =null;
+		try {
+			DefdocList = (List<DefdocVO>) getBaseDAO().executeQuery(QDefdocSql,
+					new BeanListProcessor(DefdocVO.class));
+			Logger.info("꿴璂菱땍屢도갭±"+doclistname+"×end");
+		} catch (DAOException e1) {
+			// TODO Auto-generated catch block
+			Logger.error("꿴璂菱땍屢도갭±"+doclistname+"×놔댄:" + e1.getMessage());
+			throw new BusinessException("꿴璂菱땍屢도갭±"+doclistname+"×놔댄:" + e1.getMessage());
+		}
+		
+		return DefdocList;
+	}
+	
+	
+	
+	
+	private BaseDAO baseDAO;
+	
+	public BaseDAO getBaseDAO() {
+		if (baseDAO == null) {
+			baseDAO = new BaseDAO();
+		}
+		return baseDAO;
+	}
+
+
+}

+ 1 - 1
src/private/nc/impl/pubdev/pubtool/PubDevTool.java

@@ -1,4 +1,4 @@
-package nc.impl.pubdev.pubtool;
+package nc.impl.pubdev.pubtool.execmethod;
 
 import java.io.File;
 import java.util.ArrayList;

+ 137 - 0
src/private/nc/impl/pubdev/pubtool/execmethod/SFTPUtil.java

@@ -0,0 +1,137 @@
+package nc.impl.pubdev.pubtool.execmethod;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Properties;
+
+import com.jcraft.jsch.Channel;
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+ 
+/**
+ * SFTP工具类
+ */
+public class SFTPUtil {
+    static Session sshSession = null;
+ 
+    /**
+     * 获取ChannelSftp
+     */
+    public static ChannelSftp getConnectIP(String host, String sOnlineSftpPort, String username, String password) {
+        int port = 0;
+        if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {
+            port = Integer.parseInt(sOnlineSftpPort);
+        }
+        ChannelSftp sftp = null;
+        try {
+            JSch jsch = new JSch();
+            jsch.getSession(username, host, port);
+            sshSession = jsch.getSession(username, host, port);
+            sshSession.setPassword(password);
+            Properties sshConfig = new Properties();
+            sshConfig.put("StrictHostKeyChecking", "no");
+            sshSession.setConfig(sshConfig);
+            sshSession.setTimeout(60000);
+            sshSession.connect();
+            Channel channel = sshSession.openChannel("sftp");
+            channel.connect();
+            sftp = (ChannelSftp) channel;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return sftp;
+    }
+ 
+    /**
+     * 上传
+     */
+    public static void upload(String directory, String uploadFile, ChannelSftp sftp) {
+        try {
+            sftp.cd(directory);
+            File file = new File(uploadFile);
+            sftp.put(new FileInputStream(file), file.getName());
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (sftp.isConnected()) {
+                sshSession.disconnect();
+                sftp.disconnect();
+            }
+ 
+        }
+    }
+ 
+    /**
+     * 下载
+     */
+    public static void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
+        try {
+            sftp.cd(directory);
+            File file = new File(saveFile);
+            sftp.get(downloadFile, new FileOutputStream(file));
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (sftp.isConnected()) {
+                sshSession.disconnect();
+                sftp.disconnect();
+            }
+ 
+        }
+    }
+ 
+    /**
+     * 查看
+     */
+    public static List<String> check(String directory, ChannelSftp sftp) {
+        List<String> fileList = new ArrayList<>();
+        try {
+            sftp.cd(directory);
+            ListIterator a = sftp.ls(directory).listIterator();
+            while (a.hasNext()) {
+                fileList.add((String) a.next());
+            }
+ 
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (sftp.isConnected()) {
+                sshSession.disconnect();
+                sftp.disconnect();
+            }
+ 
+        }
+        return fileList;
+    }
+ 
+    /**
+     * 删除
+     */
+    public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
+        try {
+            sftp.cd(directory);
+            sftp.rm(deleteFile);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (sftp.isConnected()) {
+                sshSession.disconnect();
+                sftp.disconnect();
+            }
+ 
+        }
+    }
+ 
+ 
+    public static void main(String[] args) {
+        ChannelSftp ftp = getConnectIP("218.19.140.180", "990", "004-FSRQ", "esaB7p%dymTX");
+        check("", ftp);
+    }
+ 
+ 
+}

+ 9 - 2
src/private/nc/impl/pubdev/pubtool/filetool/DevFileGainToolimpl.java

@@ -4,8 +4,9 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.Map;
 
-import nc.impl.pubdev.pubtool.FtpUtil;
-import nc.impl.pubdev.pubtool.PubDevTool;
+import nc.impl.pubdev.pubtool.execmethod.FtpUtil;
+import nc.impl.pubdev.pubtool.execmethod.FtpsUtil;
+import nc.impl.pubdev.pubtool.execmethod.PubDevTool;
 import nc.itf.pubdev.pubtool.filetool.IDevFileGainTool;
 import nc.vo.pub.BusinessException;
 
@@ -23,5 +24,11 @@ public class DevFileGainToolimpl implements IDevFileGainTool{
 		return new FtpUtil().doFtpReadFile(propfilename, type);
 	}
 
+	@Override
+	public Map<String, Object> GainDatasByFtps(String propfilename, int type) throws BusinessException {
+		// TODO Auto-generated method stub
+		return new FtpsUtil().doFtpReadFile(propfilename, type);
+	}
+
 
 }

+ 22 - 0
src/public/nc/itf/pubdev/pubtool/BDTool/IBDDatasGainTool.java

@@ -0,0 +1,22 @@
+package nc.itf.pubdev.pubtool.BDTool;
+
+import java.util.List;
+
+import nc.vo.bd.defdoc.DefdocVO;
+import nc.vo.pub.BusinessException;
+
+public interface IBDDatasGainTool {
+	
+	
+	/**
+	 * 根据自定义档案编码查询自定义项档案内容
+	 * 返回的数据,enablestate = 2 的数据
+	 * 别名 表体doc 表头doclist
+	 * @param doclistname 自定义档案编码
+	 * @param conditon 自定义查询条件 格式:and xxxxxxxx
+	 * @return
+	 * @throws BusinessException
+	 */
+	public List<DefdocVO> GainDefDocByListcode(String doclistname,String conditon) throws BusinessException;
+
+}

+ 10 - 0
src/public/nc/itf/pubdev/pubtool/filetool/IDevFileGainTool.java

@@ -28,6 +28,16 @@ public interface IDevFileGainTool {
      */
 	public abstract Map<String,Object> GainDatasByFtp(String propfilename,int type) throws BusinessException;
 
+	/**
+	 * ftps方式,有加密的
+	 * @author longht
+	 * @param propfilename  ftp参数文件
+	 * @param type 1 = 获取excel文件类型
+	 * @return 返回类型为Map<String,Object> key =文件名  value=对应的数据 
+	 * 
+	 */
+	public abstract Map<String,Object> GainDatasByFtps(String propfilename,int type) throws BusinessException;
+	
 
 
 }

+ 10 - 10
src/public/nc/pub/Proptool/ftp_settings.properties

@@ -1,14 +1,14 @@
 # FSGAS-UNIONPAYFTP
-#ftp_ip=218.19.140.180
-#ftp_username=004-FSRQ
-#ftp_pwd=FsRq123!@#
-#ftp_basePath=/
-#ftp_port=990
+ftp_ip=218.19.140.180
+ftp_username=004-FSRQ
+ftp_pwd=esaB7p%dymTX
+ftp_basePath=/
+ftp_port=990
 #
 #
 # Localtest
-ftp_ip=10.10.21.191
-ftp_port=21
-ftp_username=draght
-ftp_pwd=dragon!18
-ftp_basePath=/
+#ftp_ip=10.10.21.191
+#ftp_port=21
+#ftp_username=draght
+#ftp_pwd=dragon!18
+#ftp_basePath=/