Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. package com.test;
  2. import java.io.*;
  3. import java.security.GeneralSecurityException;
  4. import java.security.KeyStore;
  5. import java.security.KeyStoreException;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.security.UnrecoverableKeyException;
  8. import java.security.cert.CertificateException;
  9. import javax.net.ssl.KeyManager;
  10. import javax.net.ssl.KeyManagerFactory;
  11. import javax.net.ssl.SSLContext;
  12. import javax.net.ssl.TrustManager;
  13. import javax.net.ssl.TrustManagerFactory;
  14. import org.apache.commons.net.PrintCommandListener;
  15. import org.apache.commons.net.ftp.FTP;
  16. import org.apache.commons.net.ftp.FTPCmd;
  17. import org.apache.commons.net.ftp.FTPConnectionClosedException;
  18. import org.apache.commons.net.ftp.FTPFile;
  19. import org.apache.commons.net.ftp.FTPReply;
  20. import org.apache.commons.net.ftp.FTPSClient;
  21. import org.apache.commons.net.util.KeyManagerUtils;
  22. public final class FTPSExample
  23. {
  24. public static final void main(String[] args) throws IOException, GeneralSecurityException
  25. {
  26. System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
  27. int base = 0;
  28. boolean storeFile = false, binaryTransfer = true, error = false;
  29. String server, username, password, remote, local;
  30. String protocol = "TLSv1.2"; // SSL/TLS
  31. FTPSClient ftps;
  32. server = "hostaddress";
  33. username = "user";
  34. password = "password";
  35. remote="/IN";
  36. local="E:\abc.txt";
  37.  
  38. ftps = new FTPSClient(protocol,true);
  39. InputStream is = new FileInputStream("abc.jks");
  40. String file1 = "abc.jks";
  41. KeyStore keyStore = KeyStore.getInstance("JKS");
  42. keyStore.load(new FileInputStream(file1), "changeit".toCharArray());
  43. TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  44. trustManagerFactory.init(keyStore);
  45. KeyManager keyManager = createKeyManagers(file1,"changeit")[0];
  46. TrustManager trustManager = createTrustManagers(file1,"changeit")[0];
  47. //ftps.setControlEncoding("UTF-8");
  48. ftps.setKeyManager(keyManager);
  49. ftps.setTrustManager(trustManager);
  50.  
  51. ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  52.  
  53. try
  54. {
  55. int reply;
  56.  
  57. ftps.connect(server,990);
  58. ftps.setBufferSize(324224434);
  59. System.out.println("Connected to " + server + ".");
  60. reply = ftps.getReplyCode();
  61. if (!FTPReply.isPositiveCompletion(reply))
  62. {
  63. ftps.disconnect();
  64. System.err.println("FTP server refused connection.");
  65. System.exit(1);
  66. }
  67. }
  68. catch (IOException e)
  69. {
  70. e.printStackTrace();
  71. if (ftps.isConnected())
  72. {
  73. try
  74. {
  75. ftps.disconnect();
  76. }
  77. catch (IOException f)
  78. {
  79. f.printStackTrace();
  80. }
  81. }
  82. System.err.println("Could not connect to server.");
  83. e.printStackTrace();
  84. System.exit(1);
  85. }
  86. __main:
  87. try
  88. {
  89. ftps.setBufferSize(1000);
  90.  
  91. if (!ftps.login(username, password))
  92. {
  93. ftps.logout();
  94. error = true;
  95. break __main;
  96. }
  97. if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE);
  98. ftps.enterLocalPassiveMode();
  99. ftps.execPROT("P");
  100. ftps.execPBSZ(0L);
  101. ftps.pwd();
  102.  
  103. if (storeFile)
  104. {
  105. System.out.println("Storing File");
  106. InputStream input;
  107.  
  108. input = new FileInputStream(local);
  109.  
  110.  
  111. ftps.storeFile(remote, input);
  112.  
  113. input.close();
  114. }
  115. else
  116. {
  117.  
  118. ftps.setKeepAlive(true);
  119. ftps.setRemoteVerificationEnabled(true);
  120. ftps.sendCommand(FTPCmd.PORT);
  121. System.out.println("Retreiving File");
  122. FTPFile[] files = ftps.listFiles("/IN");
  123. System.out.println("Count :"+files.length);
  124. OutputStream output;
  125.  
  126. output = new FileOutputStream(local);
  127. ftps.sendCommand(FTPCmd.PORT);
  128. ftps.retrieveFile(remote, output);
  129. System.out.println("File Retrieved");
  130.  
  131. output.close();
  132. }
  133.  
  134. ftps.logout();
  135. }
  136. catch (FTPConnectionClosedException e)
  137. {
  138. error = true;
  139. System.err.println("Server closed connection.");
  140. e.printStackTrace();
  141. }
  142. catch (IOException e)
  143. {
  144. error = true;
  145. e.printStackTrace();
  146. }
  147. finally
  148. {
  149. if (ftps.isConnected())
  150. {
  151. try
  152. {
  153. ftps.disconnect();
  154. }
  155. catch (IOException f)
  156. {
  157. f.printStackTrace();
  158. }
  159. }
  160. }
  161.  
  162. System.exit(error ? 1 : 0);
  163. }
  164. private static TrustManager[] createTrustManagers(String keyStorePath, String keyStorePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
  165. KeyStore keyStore = KeyStore.getInstance("JKS");
  166. keyStore.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());
  167. TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  168. trustManagerFactory.init(keyStore);
  169.  
  170. return trustManagerFactory.getTrustManagers();
  171. }
  172. private static KeyManager[] createKeyManagers(String keyStorePath, String keyStorePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
  173. KeyStore keyStore = KeyStore.getInstance("JKS");
  174. keyStore.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());
  175. KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  176. keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
  177.  
  178. return keyManagerFactory.getKeyManagers();
  179. }
  180. }
  181.  
  182. 220-FileZilla Server 0.9.59 beta
  183. 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
  184. 220 Please visit https://filezilla-project.org/
  185. Connected to host.
  186. USER ***
  187. 331 Password required for ****
  188. PASS ******
  189. 230 Logged on
  190. TYPE I
  191. 200 Type set to I
  192. PROT P
  193. 200 Protection level set to P
  194. PBSZ 0
  195. 200 PBSZ=0
  196. PWD
  197. 257 "/" is current directory.
  198. PORT
  199. 501 Syntax error
  200. Retreiving File
  201. SYST
  202. 215 UNIX emulated by FileZilla
  203. PASV
  204. 227 Entering Passive Mode ()
  205. [Replacing PASV mode reply address]
  206. LIST /IN
  207. 150 Opening data channel for directory listing of "/IN"
  208. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
  209. at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:980)
  210. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
  211. at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
  212. at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
  213. at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:646)
  214. at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:785)
  215. at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3409)
  216. at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3339)
  217. at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3016)
  218. at com.test.FTPSExample.main(FTPSExample.java:168)
  219. Caused by: java.io.EOFException: SSL peer shut down incorrectly
  220. at sun.security.ssl.InputRecord.read(InputRecord.java:505)
  221. at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:961)
  222. ... 9 more
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement