|
@@ -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;
|
|
|
+ }
|
|
|
+}
|