Connect to ftps site via java

I am trying to connect to the ftps client server through my Java code. For this, I use the apache commons library. However, I cannot do this. Can anyone help me with this.

The client server uses the FTPS / Implicit SSL connection and uses passive mode for data connections.

My code is as follows:

public static void connectServer(String host, int port, String userName, String password) throws Exception { FTPSClient client = new FTPSClient("SSL", true); String remote = "Contact.xml"; File inFile = new File("C:/Documents/Applications/Contact.xml"); File outFile = new File("C:/Documents/Applications/Sample.xml"); InputStream input = new FileInputStream(inFile); InputStream out = new FileInputStream(outFile); try { if(client.isConnected()){ client.disconnect(); } client.connect(host,990); client.enterLocalPassiveMode(); client.enterRemotePassiveMode(); client.login(userName, password); client.setBufferSize((int)inFile.length()+100); client.completePendingCommand(); System.out.println(client.printWorkingDirectory()); System.out.println(inFile.getAbsolutePath()); client.storeFile(remote, input); out = client.retrieveFileStream("/folder/inputfeed.xml"); client.completePendingCommand(); client.logout(); } catch (Exception e) { e.printStackTrace(); System.err.println(client.getReplyString()); } finally { out.close(); input.close(); client.disconnect(); } } 

This code does not cause any exceptions, but I do not see that the file is being copied to the server, nor any data is being copied to my InputStream. Also, the sysout statement to get the working directory returns me the correct directory. I can also connect to the server through Filezilla and WinSCP.

Please help, I'm stuck with this.

thanks

+4
source share
5 answers

Why do you enter passive mode before logging in ()?

I suspect this may be a problem because the symptoms appear in an active mode when the connection cannot be established due to the FW DROP rule (not REJECT; if rejected, the exception is immediately discarded, but the DROP may hang forever).

PS In addition, it is unclear what the "remote" passive mode is; the only thing that makes a difference is "local."

0
source

enterRemotePassiveMode(); It is used only for connections from server to server, and not from client to server. Delete this line.

0
source

client.connect(host,990); Why is the hard code number used? I suggest you change this line to use the port number passed to you by function / method

For reasons that I didn’t have time to look at your code, this line also hangs

  client.completePendingCommand(); 

before the server shuts it down. I'm not sure if you are not getting an exception, or you just can't wait long enough for the code to throw an exception.

0
source

You need to check the status after each client(socket) method. Especially after client.connect() and client.login() .

Example:

 client.connect(host, 990); System.out.print(client.getReplyString()); ... client.login(username,password); System.out.print(client.getReplyString()); 

If the method did not work (error), getReplyString() usually very descriptive of what you did wrong.

0
source

The port number you use for FTP-21 and SFTP is 22

I have some same requirement to download a file from different servers, so several times I had to use 21 and some time 22.

I tried to approach the approach

 FTPClient ftpClient = new FTPClient(); int port = 21; try { try{ ftpClient.connect(serverName, port); }catch(Exception e){ ftpClient.connect(serverName, 22); } try { ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.changeWorkingDirectory(workingDir); String fileName = "fileToDownLoad"; File downloadFile = new File(fileName); OutputStream localOutputStream = new BufferedOutputStream( new FileOutputStream(downloadFile)); InputStream fptInputStream = ftpClient.retrieveFileStream(folder + "/" + file); byte[] bytesArray = new byte[4096]; int bytesRead = -1; while ((bytesRead = fptInputStream.read(bytesArray)) != -1) { localOutputStream.write(bytesArray, 0, bytesRead); } boolean success = ftpClient.completePendingCommand(); if (success) { } localOutputStream.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } 
0
source

All Articles