Total members 11774 |It is currently Thu Apr 25, 2019 7:09 am Login / Join Codemiles














R Scripts


FTP in Java :
Today, I needed to connect from my java code to FTP server. Here how things goes on.
Searching for the best FTP API exists will be the first step. You have to define the criteria’s that your choice we be based on. The Criteria’s are:
- Degree of Support (like: forums, mails, FAQ, phone support and documentation).
- License (Just Free, GPL or LGPL, Not Free (For Commercial)).
- First release or age. (Older package means more testing and bigger community).
- Open Source code (It is better to be open source code; it will be more comfortable to you as a developer).
- Monitoring transfer (So you can calculate number of transferred bytes, you can pause transfer and resume it another time).
- Connect throw proxies (support communication throw firewalls using SOCKS (v4, v5)).
- Support of FTP commands like (Listing directories, rename, deleted, move).
- Support threading and multi-able parallel transfer.
- Support for authentication and security protocol like (SFTP - SSH, FTPS).

For FTP servers I have done my learning phase using File-Zilla server.

About Java FTP API, you may ask, does JDK include FTP classes support? The Answer is yes .which is RFC959, it is implemented but it internal and no source provided. You can use it by the URL specification RFC1738.
URL url = new URL("ftp://username:[email protected]/file.txt;type=i");
URLConnection urlconnection = url.openConnection();
InputStream InputstreamObj = urlconnection.getInputStream(); // To download stream
OutputStream outputstreamObj = urlconnection.getOutputStream(); // To upload stream      

I didn’t use it, There many missing features by this why, it just open input and output data streams.
Well , I tested two libraries , the first one is FTP4J and the second one is Zehon .

1. FTP4J is good, it included the features I needed and mainly the FTPS. It also includes resuming upload and download. ... nual.php#5
2. Zehon is a good FTP package. One of its advantage that it includes that can upload and download folders which is pretty good way.
My big challenge was to run the FTPs. The First thing I did, I generated a security certification using FileZilla server tool .The generated certification is .crt and X502 ASCII contains public and private key. Well , my java code need keystore to run .Keystore is a general concept means a store of private keys , public keys pairs saved by passwords . To create this keystore I use keytool.exe which exists in bin folder.

keytool -genkey -alias javaxforums -keyalg RSA -sigalg MD5withRSA -keypass keyPassword -storepass storePassword -storetype JCEKS -keystore javaxforums.jks
Using the command above, I created a keystore of type JCEKS, remember this keystore contains only 1 private key.
Running the code of loading the keystore :
java code
    KeyStore ks = KeyStore.getInstance("JCEKS");
        ks.load( new FileInputStream( new File("javaxforums.jks") ),"keyPassword".toCharArray());
         KeyManagerFactory km = KeyManagerFactory.getInstance( "IbmX509","IBMJSSE2"); 
         KeyManager[] kma = km.getKeyManagers();   
         TrustManagerFactory tm = TrustManagerFactory.getInstance("IbmX509","IBMJSSE2");
         tm.init( ks );
         TrustManager[] tma = tm.getTrustManagers(); 
        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(kma , tma, new SecureRandom());  

java code Given final block not properly padded
at Source)
at Source)
at FTPJClient.FTPTestClient.<init>(
at FTPJClient.FTPTestClient.main(
Exception in thread "main" java.lang.NullPointerException
at FTPJClient.FTPTestClient.<init>(
at FTPJClient.FTPTestClient.main(

I was getting this error, I discovered that there must be only one private key in keystore and the passwords of private and keystore have to be the same.

After this I got a new error
Code: No trusted certificate found

my solution to this that I need to insert a public key to the keystore so it can be a trust certificate . I made this by editing the .crt filezilla server generate certification, which includes two parts :

I to the certificate part and I added it to a new file for example certification.crt and I imported it to the keystore using the following command
keytool -import -trustcacerts -alias root -file certification.crt -keystore javaxforums.jks
After this , it is working .
With zehon JCKES keystore type wasn’t working, you can try the default type or JKS while the creation of keystore.

FTPS is a little bit slower than the FTP .This is what I found by expert and this make sense. A note : if you tried to upload the public key to exported from keystore. You get error like : I can’t upload key file .

User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Using FTP in java
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  
 need help in java     -  
 What is Java API?!!!     -  
 java or .net     -  
 about Java IO-interface     -  
 java scjp     -  
 VB.NET to JAVA translation     -  
 Java help needed     -  

Topic Tags

Java Networking

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to 2007-2011
mileX v1.0 designed by codemiles team is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to