Author
How to use jave to do remote ftp, download, upload stuff ?
Steve Yu
Ranch Hand
Joined: Mar 26, 2003
Posts: 60
posted Oct 02, 2004 11:39:00
0
How to use jave to do remote ftp, download, upload stuff ? Korn shell script is very good at doing that ? How to do it in java ? steve
Jean-Francois Briere
Ranch Hand
Joined: Mar 03, 2004
Posts: 101
Take a look at Apache's Jakarta Commons Net
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
Try this out .... This is working for me .. Srinivasa Raghavan import java.net.*; import java.io.*; class ftpClient { URLConnection connection; InputStream is; OutputStream os; String url;String user; String password; public ftpClient(String url,String user,String password) { this.url =url; this.user= user; this.password=password; } private void getConnection(String file) { try { StringBuffer location = new StringBuffer (); location.append("ftp://"); location.append(user + ":"); location.append(password + "@"); location.append(url + "/"); location.append(file +";type=i"); connection = (new URL(location.toString())).openConnection(); is = connection.getInputStream(); } catch(Exception e) { System.out.println(e.toString() + "Here" ); } } public void download( String inputFile,String outputFile ) throws Exception { try { getConnection( inputFile ); int data; InputStreamReader fileReader = new InputStreamReader (is); FileOutputStream writer = new FileOutputStream ( new File(outputFile) ); while ( (data = fileReader.read()) != -1 ) { writer.write((char)data); } } catch(Exception e) { System.out.println(e ); } } public static void main(String[] args) { String inputFile = "home/srinivas/test/reverse.java"; String outputFile = "C:\\reverse.java"; try { ftpClient client = new ftpClient("20.10.3.225","test","password"); client.download(inputFile,outputFile); System.out.println( inputFile + " downloaded successfully to " + outputFile); } catch(Exception e) { System.out.println(e); } } }
Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Annie Zhang
Ranch Hand
Joined: Sep 08, 2004
Posts: 31
Hi Srini, Do you have the upload() code? If you do, could you please post it? I tried the upload with JDK1.3.1, it complains: "java.net.UnknownServiceException: protocol doesn't support output at java.net.URLConnection.getOutputStream(URLConnection.java:655)" Seems like JDK1.3.1 doesn't support upload(put) file to ftp server. Is this true? Otherwise, did I get it wrong? Please clarify. The download(get) works fine. Thanks, Annie
Z Zia
Greenhorn
Joined: Oct 03, 2004
Posts: 24
Try this api, its free: http://www.enterprisedt.com/products/edtftpj/overview.html I am using this for my enterprise apps.
subject: How to use jave to do remote ftp, download, upload stuff ?