Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:33 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Article
 Article subject: Using FTP in java
PostPosted: Thu Aug 25, 2011 8:04 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

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.
Code:
URL url = new URL("ftp://username:password@ftp.javaxforums.com/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.
http://www.sauronsoftware.it/projects/f ... 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.
http://www.zehon.com/
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 :
Code:
    KeyStore ks KeyStore.getInstance("JCEKS");
        
ks.load( new FileInputStream( new File("javaxforums.jks") ),"keyPassword".toCharArray());
         
KeyManagerFactory km KeyManagerFactory.getInstance"IbmX509","IBMJSSE2"); 
         
km.init(ks,"storePassword".toCharArray());
          
         
         
KeyManager[] kma km.getKeyManagers();   
         
TrustManagerFactory tm TrustManagerFactory.getInstance("IbmX509","IBMJSSE2");
         
tm.initks );
         
TrustManager[] tma tm.getTrustManagers(); 
        
sslContext SSLContext.getInstance("SSL");
        
sslContext.init(kma tma, new SecureRandom());  


Code:
java.security.UnrecoverableKeyException: Given final block not properly padded
   at com.ibm.crypto.provider.y.a(Unknown Source)
   at com.ibm.crypto.provider.JceKeyStore.engineGetKey(Unknown Source)
   at java.security.KeyStore.getKey(KeyStore.java:196)
   at com.ibm.jsse2.wc.<init>(wc.java:80)
   at com.ibm.jsse2.ic$a_.engineInit(ic$a_.java:22)
   at javax.net.ssl.KeyManagerFactory.init(KeyManagerFactory.java:9)
   at FTPJClient.FTPTestClient.<init>(FTPTestClient.java:79)
   at FTPJClient.FTPTestClient.main(FTPTestClient.java:195)
Exception in thread "main" java.lang.NullPointerException
   at FTPJClient.FTPTestClient.<init>(FTPTestClient.java:123)
   at FTPJClient.FTPTestClient.main(FTPTestClient.java:195)


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:
javax.net.ssl.SSLHandshakeException: com.ibm.jsse2.util.g: No trusted certificate found
   at com.ibm.jsse2.n.a(n.java:26)
   at com.ibm.jsse2.sc.a(sc.java:483)
   at com.ibm.jsse2.gb.a(gb.java:19)
   at com.ibm.jsse2.gb.a(gb.java:83)
   at com.ibm.jsse2.hb.a(hb.java:295)
   at com.ibm.jsse2.hb.a(hb.java:92)
   at com.ibm.jsse2.gb.n(gb.java:165)
   at com.ibm.jsse2.gb.a(gb.java:26)
   at com.ibm.jsse2.sc.a(sc.java:389)
   at com.ibm.jsse2.sc.g(sc.java:316)
   at com.ibm.jsse2.sc.a(sc.java:528)


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 :
Code:
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

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 .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
 
Post new topic Reply to topic  [ 1 post ] 

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Article | Next Article 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team