|
@@ -0,0 +1,782 @@
|
|
|
+/*------------------------------------------------------------------------------
|
|
|
+ *******************************************************************************
|
|
|
+ * COPYRIGHT China Mobile (SuZhou) Software Technology Co.,Ltd. 2018
|
|
|
+ *******************************************************************************
|
|
|
+ *----------------------------------------------------------------------------*/
|
|
|
+package com.cmss.pretendProxy;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.BufferedWriter;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.io.PrintStream;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.net.ServerSocket;
|
|
|
+import java.net.Socket;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.SSLSocket;
|
|
|
+import javax.net.ssl.SSLSocketFactory;
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
+
|
|
|
+import org.apache.commons.net.ftp.FTPClient;
|
|
|
+import org.apache.commons.net.ftp.FTPFile;
|
|
|
+import org.apache.commons.net.ftp.FTPReply;
|
|
|
+import org.apache.commons.net.ftp.FTPSClient;
|
|
|
+import org.apache.commons.net.ftp.FTPSServerSocketFactory;
|
|
|
+import org.apache.commons.net.ftp.FTPSSocketFactory;
|
|
|
+import org.apache.commons.net.util.SSLContextUtils;
|
|
|
+import org.apache.commons.net.util.TrustManagerUtils;
|
|
|
+
|
|
|
+public class FtpsClientTest {
|
|
|
+
|
|
|
+ public static void main(final String[] args) throws Exception {
|
|
|
+
|
|
|
+ // testConnect();
|
|
|
+
|
|
|
+ // testList();
|
|
|
+
|
|
|
+ _testList();
|
|
|
+
|
|
|
+ // testUpload();
|
|
|
+
|
|
|
+ // testDownload();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void testConnect() throws Exception {
|
|
|
+
|
|
|
+ //ftpsClent
|
|
|
+ // final FTPSClient fc = new FTPSClient(false);
|
|
|
+ // fc.connect("10.139.5.112", 21);
|
|
|
+ // fc.login("eshub", "eshub");
|
|
|
+ // if (!FTPReply.isPositiveCompletion(fc.getReplyCode())) {
|
|
|
+ // fc.disconnect();
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ // System.out.println("登录成功");
|
|
|
+ // fc.logout();
|
|
|
+
|
|
|
+ //socket
|
|
|
+
|
|
|
+ Socket s = new Socket("10.139.5.112", 21);
|
|
|
+ System.out.println(s);
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
|
|
|
+
|
|
|
+ String readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("AUTH SSL\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //234 Proceed with negotiation
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ final TrustManager trustManager = TrustManagerUtils.getValidateServerCertificateTrustManager();
|
|
|
+ final SSLContext context = SSLContextUtils.createSSLContext("TLS", null, trustManager);
|
|
|
+ final SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
+ final SSLSocket socket = (SSLSocket) ssf.createSocket(s, s.getInetAddress().getHostAddress(), s.getPort(),
|
|
|
+ false);
|
|
|
+ socket.setEnableSessionCreation(true);
|
|
|
+ socket.setUseClientMode(true);
|
|
|
+ socket.startHandshake();
|
|
|
+
|
|
|
+ s = socket;
|
|
|
+ br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
|
|
|
+ bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1"));
|
|
|
+
|
|
|
+ bw.write("USER eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //331 Please specify the password.
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("PASS eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //230 Login successful.
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("QUIT\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //221 Goodbye.
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试list目录
|
|
|
+ */
|
|
|
+ public static void testList() throws Exception {
|
|
|
+
|
|
|
+ //ftpsClent
|
|
|
+ // final FTPSClient fc = new FTPSClient(false);
|
|
|
+ // fc.connect("10.139.5.112", 21);
|
|
|
+ // fc.login("eshub", "eshub");
|
|
|
+ // if (!FTPReply.isPositiveCompletion(fc.getReplyCode())) {
|
|
|
+ // fc.disconnect();
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ // System.out.println("登录成功");
|
|
|
+ //
|
|
|
+ // fc.setFileType(2);
|
|
|
+ // fc.execPROT("P");
|
|
|
+ // fc.enterLocalPassiveMode();
|
|
|
+ //
|
|
|
+ // final FTPFile[] fs = fc.listFiles();
|
|
|
+ // System.out.println("size:" + fs.length);
|
|
|
+ // for (int i = 0; i < fs.length; i++) {
|
|
|
+ // System.out.println(fs[i].getName());
|
|
|
+ // }
|
|
|
+ // fc.logout();
|
|
|
+
|
|
|
+ //socket
|
|
|
+
|
|
|
+ Socket cmdSocket = new Socket("10.139.5.112", 21);
|
|
|
+ System.out.println(cmdSocket);
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(cmdSocket.getInputStream()));
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cmdSocket.getOutputStream()));
|
|
|
+
|
|
|
+ String readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("AUTH SSL\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //234 Proceed with negotiation
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ final TrustManager trustManager = TrustManagerUtils.getValidateServerCertificateTrustManager();
|
|
|
+ final SSLContext context = SSLContextUtils.createSSLContext("TLS", null, trustManager);
|
|
|
+ final SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
+ final SSLSocket socket = (SSLSocket) ssf.createSocket(cmdSocket, cmdSocket.getInetAddress().getHostAddress(),
|
|
|
+ cmdSocket.getPort(), false);
|
|
|
+ socket.setEnableSessionCreation(true);
|
|
|
+ socket.setUseClientMode(true);
|
|
|
+ socket.startHandshake();
|
|
|
+
|
|
|
+ cmdSocket = socket;
|
|
|
+
|
|
|
+ br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
|
|
|
+ bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1"));
|
|
|
+
|
|
|
+ //登录
|
|
|
+ bw.write("USER eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //331 Please specify the password.
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("PASS eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //230 Login successful.
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ //设置模式
|
|
|
+ bw.write("TYPE I\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("PROT P\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ //开通数据连接
|
|
|
+ bw.write("PASV \r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ final int port = parsePort(readLine);
|
|
|
+
|
|
|
+ final SSLSocket dataSocket = (SSLSocket) new FTPSSocketFactory(context).createSocket();
|
|
|
+ dataSocket.connect(new InetSocketAddress(cmdSocket.getInetAddress(), port));
|
|
|
+
|
|
|
+ //发送命令
|
|
|
+ bw.write("LIST\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ dataSocket.setUseClientMode(true);
|
|
|
+ dataSocket.setEnableSessionCreation(true);
|
|
|
+ dataSocket.startHandshake();
|
|
|
+ System.out.println("已建立数据连接");
|
|
|
+ final BufferedReader brClient = new BufferedReader(new InputStreamReader(dataSocket.getInputStream()));
|
|
|
+ final PrintStream psClient = new PrintStream(dataSocket.getOutputStream(), true);
|
|
|
+ while ((readLine = brClient.readLine()) != null) {
|
|
|
+
|
|
|
+ System.out.println(readLine);
|
|
|
+ }
|
|
|
+ readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("QUIT\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //221 Goodbye.
|
|
|
+ System.out.println(readLine);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试list目录
|
|
|
+ */
|
|
|
+ public static void _testList() throws Exception {
|
|
|
+
|
|
|
+ //ftpsClent
|
|
|
+ final FTPSClient fc = new FTPSClient(false);
|
|
|
+ fc.connect("10.139.5.112", 21);
|
|
|
+ fc.login("eshub", "eshub");
|
|
|
+ if (!FTPReply.isPositiveCompletion(fc.getReplyCode())) {
|
|
|
+ fc.disconnect();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ System.out.println("登录成功");
|
|
|
+
|
|
|
+ fc.setFileType(2);
|
|
|
+ fc.execPROT("P");
|
|
|
+ fc.enterLocalPassiveMode();
|
|
|
+
|
|
|
+ final FTPFile[] fs = fc.listFiles();
|
|
|
+ System.out.println("size:" + fs.length);
|
|
|
+ for (int i = 0; i < fs.length; i++) {
|
|
|
+ System.out.println(fs[i].getName());
|
|
|
+ }
|
|
|
+ fc.logout();
|
|
|
+
|
|
|
+ //socket
|
|
|
+
|
|
|
+ Socket cmdSocket = new Socket("10.139.5.112", 21);
|
|
|
+ System.out.println(cmdSocket);
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(cmdSocket.getInputStream()));
|
|
|
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cmdSocket.getOutputStream()));
|
|
|
+
|
|
|
+ String readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("AUTH SSL\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //234 Proceed with negotiation
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ final TrustManager trustManager = TrustManagerUtils.getValidateServerCertificateTrustManager();
|
|
|
+ final SSLContext context = SSLContextUtils.createSSLContext("TLS", null, trustManager);
|
|
|
+ final SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
+ final SSLSocket socket = (SSLSocket) ssf.createSocket(cmdSocket, cmdSocket.getInetAddress().getHostAddress(),
|
|
|
+ cmdSocket.getPort(), false);
|
|
|
+ socket.setEnableSessionCreation(true);
|
|
|
+ socket.setUseClientMode(true);
|
|
|
+ socket.startHandshake();
|
|
|
+
|
|
|
+ cmdSocket = socket;
|
|
|
+
|
|
|
+ br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
|
|
|
+ bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1"));
|
|
|
+
|
|
|
+ //登录
|
|
|
+ bw.write("USER eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //331 Please specify the password.
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("PASS eshub\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //230 Login successful.
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ //设置模式
|
|
|
+ bw.write("TYPE I\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //200 Switching to Binary mode.
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("PROT P\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //200 PROT now Private.
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ //开通数据连接
|
|
|
+
|
|
|
+ final ServerSocket server = new FTPSServerSocketFactory(context).createServerSocket(0, 1,
|
|
|
+ cmdSocket.getLocalAddress());
|
|
|
+ System.out.println(server);
|
|
|
+ final String toServer = "PORT " + cmdSocket.getLocalAddress().getHostAddress().replace('.', ',') + ','
|
|
|
+ + server.getLocalPort() / 256 + ',' + (server.getLocalPort() % 256);
|
|
|
+ System.out.println(toServer);
|
|
|
+ bw.write(toServer + "\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ //发送命令
|
|
|
+ bw.write("LIST\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //
|
|
|
+ System.out.println(readLine);
|
|
|
+
|
|
|
+ final SSLSocket dataSocket = (SSLSocket) server.accept();
|
|
|
+ dataSocket.setUseClientMode(true);
|
|
|
+ dataSocket.setEnableSessionCreation(true);
|
|
|
+ dataSocket.startHandshake();
|
|
|
+ System.out.println("已建立数据连接");
|
|
|
+ final BufferedReader brClient = new BufferedReader(new InputStreamReader(dataSocket.getInputStream()));
|
|
|
+ final PrintStream psClient = new PrintStream(dataSocket.getOutputStream(), true);
|
|
|
+ while ((readLine = brClient.readLine()) != null) {
|
|
|
+
|
|
|
+ System.out.println(readLine);
|
|
|
+ }
|
|
|
+ readLine = br.readLine();
|
|
|
+ System.out.println(readLine);
|
|
|
+ bw.write("QUIT\r\n");
|
|
|
+ bw.flush();
|
|
|
+ readLine = br.readLine();
|
|
|
+ //221 Goodbye.
|
|
|
+ System.out.println(readLine);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试upload
|
|
|
+ */
|
|
|
+ public static void testUpload() {
|
|
|
+
|
|
|
+ final FTPClient ftpClient = new FTPClient();
|
|
|
+
|
|
|
+ try {
|
|
|
+ int reply;
|
|
|
+ // 代理服务器IP和端口
|
|
|
+ ftpClient.connect("172.20.44.229", 8089);
|
|
|
+
|
|
|
+ // 登录名:FTP服务器用户名@FTP服务器IP:端口,密码:FTP服务器用户名对应的密码
|
|
|
+ ftpClient.login("ftp1@10.139.10.176:21", "ftp123456");
|
|
|
+ reply = ftpClient.getReplyCode();
|
|
|
+
|
|
|
+ System.out.println("reply:" + reply);
|
|
|
+
|
|
|
+ if (!FTPReply.isPositiveCompletion(reply)) {
|
|
|
+ ftpClient.disconnect();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // ftpClient.changeWorkingDirectory("/");// 转移到FTP服务器目录
|
|
|
+
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
+
|
|
|
+ final FTPFile[] fs = ftpClient.listFiles();
|
|
|
+ System.out.println("上传前size:" + fs.length);
|
|
|
+ for (int i = 0; i < fs.length; i++) {
|
|
|
+ System.out.println(fs[i].getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ InputStream in = null;
|
|
|
+ // Upload.
|
|
|
+ try {
|
|
|
+ final File file = new File("D:/testProxy.txt");
|
|
|
+
|
|
|
+ // Use passive mode to pass firewalls.
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
+
|
|
|
+ final String ftpFileName = "testProxy1.txt";
|
|
|
+
|
|
|
+ in = new BufferedInputStream(new FileInputStream(file));
|
|
|
+ if (!ftpClient.storeFile(ftpFileName, in)) {
|
|
|
+ throw new IOException("Can't upload file '" + ftpFileName
|
|
|
+ + "' to FTP server. Check FTP permissions and path.");
|
|
|
+ }
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (final IOException ex) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ final FTPFile[] fs1 = ftpClient.listFiles();
|
|
|
+ System.out.println("上传后size:" + fs1.length);
|
|
|
+ for (int i = 0; i < fs1.length; i++) {
|
|
|
+ System.out.println(fs1[i].getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ ftpClient.logout();
|
|
|
+ } catch (final IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
+ try {
|
|
|
+ ftpClient.disconnect();
|
|
|
+ } catch (final IOException ioe) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试download
|
|
|
+ */
|
|
|
+ public static void testDownload() {
|
|
|
+
|
|
|
+ final FTPClient ftpClient = new FTPClient();
|
|
|
+
|
|
|
+ try {
|
|
|
+ int reply;
|
|
|
+ // 代理服务器IP和端口
|
|
|
+ ftpClient.connect("10.139.10.123", 8089);
|
|
|
+
|
|
|
+ // 登录名:FTP服务器用户名@FTP服务器IP:端口,密码:FTP服务器用户名对应的密码
|
|
|
+ ftpClient.login("ftp1@10.139.10.176:21", "ftp123456");
|
|
|
+ reply = ftpClient.getReplyCode();
|
|
|
+
|
|
|
+ System.out.println("reply:" + reply);
|
|
|
+
|
|
|
+ if (!FTPReply.isPositiveCompletion(reply)) {
|
|
|
+ ftpClient.disconnect();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Download.
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ // Use passive mode to pass firewalls.
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
+
|
|
|
+ final File localFile = new File("D:/BigFile.zip");
|
|
|
+ final String ftpFileName = "BigFile.zip";
|
|
|
+
|
|
|
+ // Download file.
|
|
|
+ out = new BufferedOutputStream(new FileOutputStream(localFile));
|
|
|
+ if (!ftpClient.retrieveFile(ftpFileName, out)) {
|
|
|
+ throw new IOException("Error loading file " + ftpFileName
|
|
|
+ + " from FTP server. Check FTP permissions and path.");
|
|
|
+ }
|
|
|
+
|
|
|
+ out.flush();
|
|
|
+ } finally {
|
|
|
+ if (out != null) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (final IOException ex) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ftpClient.logout();
|
|
|
+ } catch (final IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
+ try {
|
|
|
+ ftpClient.disconnect();
|
|
|
+ } catch (final IOException ioe) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Override
|
|
|
+ // public void connect(final String hostname, final String portStr, final String username, final String password) {
|
|
|
+ //
|
|
|
+ // if (isConnected()) {
|
|
|
+ // LOGGER.error("FTPS已连接!");
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // int port = 21;
|
|
|
+ //
|
|
|
+ // if (!("".equals(portStr)) && null != portStr) {
|
|
|
+ // port = Integer.parseInt(portStr);
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // this.hostname = hostname;
|
|
|
+ // this.port = port;
|
|
|
+ // this.username = username;
|
|
|
+ // this.password = password;
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ //
|
|
|
+ // client.setConnectTimeout(CONNECT_TIMEOUT);
|
|
|
+ // LOGGER.debug("FTPS连接中。。。");
|
|
|
+ // client.connect(hostname, port);
|
|
|
+ // LOGGER.debug("FTPS连接成功。。。");
|
|
|
+ //
|
|
|
+ // LOGGER.debug("FTPS登录中。。。");
|
|
|
+ // client.login(username, password);
|
|
|
+ // if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
|
|
|
+ // client.disconnect();
|
|
|
+ // LOGGER.error("FTPS登录失败。。。");
|
|
|
+ // }
|
|
|
+ // LOGGER.debug("FTPS登录成功。。。");
|
|
|
+ //
|
|
|
+ // // PBSZ = 0
|
|
|
+ // // client.execPBSZ(0);
|
|
|
+ //
|
|
|
+ // // 设置以二进制方式传输
|
|
|
+ // client.setFileType(FTPSClient.BINARY_FILE_TYPE);
|
|
|
+ // client.setControlEncoding(ENCODING);
|
|
|
+ // // Protection level set to P
|
|
|
+ // client.execPROT("P"); // 521 PROT P required
|
|
|
+ // // 被动模式
|
|
|
+ // if (PASSIVE_MODE) {
|
|
|
+ // client.enterLocalPassiveMode();
|
|
|
+ // }
|
|
|
+ // client.setSoTimeout(CLIENT_TIMEOUT);
|
|
|
+ // // client.setBufferSize(1024 * 1024);
|
|
|
+ // } catch (final Exception e) {
|
|
|
+ // LOGGER.error("打开FTPS服务器失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void disconnect() {
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ // if (client != null && client.isConnected()) {
|
|
|
+ // client.logout();
|
|
|
+ // client.disconnect();
|
|
|
+ // }
|
|
|
+ // } catch (final Exception e) {
|
|
|
+ // LOGGER.error("关闭FTPS服务器错误,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void deleteFile(final String filePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ // client.deleteFile(new String(filePath.getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("删除文件【" + filePath + "】失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void changeDir(final String remotePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ // client.changeWorkingDirectory(new String(remotePath.getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("切换目录【" + remotePath + "】失败,异常信息: ", e);
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public boolean isConnected() {
|
|
|
+ //
|
|
|
+ // return client != null && client.isConnected();
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void uploadOutputStream(final InputStream inputStream, final String remotePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ //
|
|
|
+ // new Thread(new Polling(client)).start();
|
|
|
+ //
|
|
|
+ // final boolean storeFile = client.storeFile(new String(remotePath.getBytes("utf-8"), "iso-8859-1"),
|
|
|
+ // inputStream);
|
|
|
+ // if (!storeFile) {
|
|
|
+ // LOGGER.error("目录未找到:" + remotePath);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_DIR_NOT_EXIST.getExceptionType());
|
|
|
+ // }
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("上传文件【" + remotePath + "】失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public InputStream getInputStream(final String remotePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ //
|
|
|
+ // new Thread(new Polling(client)).start();
|
|
|
+ //
|
|
|
+ // final InputStream inputStream = client.retrieveFileStream(new String(remotePath.getBytes("utf-8"),
|
|
|
+ // "iso-8859-1"));
|
|
|
+ // if (inputStream == null) {
|
|
|
+ // LOGGER.error("文件未找到:" + remotePath);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_FILE_NOT_EXIST.getExceptionType());
|
|
|
+ // }
|
|
|
+ // return inputStream;
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("获取下载流【" + remotePath + "】失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public List<String> lsFile(final String remotePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ //
|
|
|
+ // try {
|
|
|
+ // final FTPFile[] listFiles = client.listFiles(new String(remotePath.getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ // final List<String> lsFile = new ArrayList<String>();
|
|
|
+ // for (final FTPFile file : listFiles) {
|
|
|
+ // if (file.isFile()) {
|
|
|
+ // lsFile.add(file.getName());
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // return lsFile;
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("获取文件列表【" + remotePath + "】失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public long getFileSize(final String remotePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ // try {
|
|
|
+ // if (remotePath.lastIndexOf("/") != -1) {
|
|
|
+ // final String path = remotePath.substring(0, remotePath.lastIndexOf("/"));
|
|
|
+ // final String fileName = remotePath.substring(remotePath.lastIndexOf("/") + 1);
|
|
|
+ // final FTPFile[] listFiles = client.listFiles(new String(path.getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ // for (final FTPFile file : listFiles) {
|
|
|
+ // if (fileName.equals(file.getName())) {
|
|
|
+ // return file.getSize();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // final FTPFile[] listFiles = client.listFiles();
|
|
|
+ // for (final FTPFile file : listFiles) {
|
|
|
+ // if (remotePath.equals(file.getName())) {
|
|
|
+ // return file.getSize();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_FILE_NOT_EXIST.getExceptionType());
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("获取文件大小【" + remotePath + "】失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void moveFile(final String remotePath, final String movePath) {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ // try {
|
|
|
+ // final boolean result = client.rename(new String(remotePath.getBytes("utf-8"), "iso-8859-1"), new String(
|
|
|
+ // movePath.getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ // if (!result) {
|
|
|
+ // LOGGER.error("移动文件失败:" + remotePath + ">>>" + movePath);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_MOVE_FILE_NOT_EXIST.getExceptionType());
|
|
|
+ // }
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("移动文件" + remotePath + ">>>" + movePath + "失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // /**
|
|
|
+ // * 校验连接
|
|
|
+ // */
|
|
|
+ // private void checkConnected() {
|
|
|
+ //
|
|
|
+ // if (!isConnected()) {
|
|
|
+ // LOGGER.debug("尚未连接FTPS。。。 ");
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // class Polling implements Runnable {
|
|
|
+ //
|
|
|
+ // private FTPSClient client = null;
|
|
|
+ //
|
|
|
+ // Polling() {
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // Polling(final FTPSClient client) {
|
|
|
+ //
|
|
|
+ // this.client = client;
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public void run() {
|
|
|
+ //
|
|
|
+ // while (isConnected()) {
|
|
|
+ // LOGGER.info("FTPS正在轮询");
|
|
|
+ // try {
|
|
|
+ // client.noop();
|
|
|
+ // } catch (final Exception e) {
|
|
|
+ // LOGGER.error("FTPS轮询失败", e);
|
|
|
+ // }
|
|
|
+ // try {
|
|
|
+ // Thread.sleep(1000 * 60);
|
|
|
+ // } catch (final Exception e) {
|
|
|
+ // LOGGER.error("", e);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // @Override
|
|
|
+ // public String getRootDir() {
|
|
|
+ //
|
|
|
+ // checkConnected();
|
|
|
+ // try {
|
|
|
+ // final String workingDirectory = client.printWorkingDirectory();
|
|
|
+ // return workingDirectory;
|
|
|
+ // } catch (final IOException e) {
|
|
|
+ // LOGGER.error("获取根目录失败,异常信息: ", e);
|
|
|
+ // throw new BusinessException(FtpExceptionEnum.FTP_NET_EXCP.getExceptionType());
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // }
|
|
|
+
|
|
|
+ public static int parsePort(String s) throws IOException {
|
|
|
+
|
|
|
+ int port;
|
|
|
+ try {
|
|
|
+ int i = s.lastIndexOf('(');
|
|
|
+ int j = s.lastIndexOf(')');
|
|
|
+ if ((i != -1) && (j != -1) && (i < j)) {
|
|
|
+ s = s.substring(i + 1, j);
|
|
|
+ }
|
|
|
+
|
|
|
+ i = s.lastIndexOf(',');
|
|
|
+ j = s.lastIndexOf(',', i - 1);
|
|
|
+
|
|
|
+ port = Integer.parseInt(s.substring(i + 1));
|
|
|
+ port += 256 * Integer.parseInt(s.substring(j + 1, i));
|
|
|
+ } catch (final Exception e) {
|
|
|
+ throw new IOException();
|
|
|
+ }
|
|
|
+ return port;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|