Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use jave to do remote ftp, download, upload stuff ?

 
Steve Yu
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Apache's Jakarta Commons Net
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Annie Zhang
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this api, its free:

http://www.enterprisedt.com/products/edtftpj/overview.html

I am using this for my enterprise apps.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic