Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to download directory from Ftp machine to local machine?  RSS feed

 
Ishrayansh Nath
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is sample program used to connect remote machine using FTP.
to get the directory and download the directoy in local machine.
before execute this program , local machine i create folder F:\folder1 in F drive.

how to over come this error

but i got error like this

Hello World!
Ftp is connected
Ftp is connected thro URL
java.io.FileNotFoundException: F:\folder1 (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at newftp.main(newftp.java:31)


import org.apache.commons.net.ftp.FTPClient;
import java.net.*;
import java.io.*;
import java.lang.reflect.Array;

class newftp
{
public static void main(String[] args) throws java.net.MalformedURLException,java.net.UnknownHostException,java.io.IOException,java.io.FileNotFoundException
{
System.out.println("Hello World!");

FTPClient ftpClient = new FTPClient();
String host ="192.168.5.70";
String userid="ftptest";
String pwd="ftptest";
ftpClient.connect(host);
ftpClient.login(userid,pwd);
System.out.println("Ftp is connected ");


URL hp = new URL("ftp://192.168.5.70//folder1");

InputStream ip = hp.openStream();
System.out.println("Ftp is connected thro URL ");


// Make destination file
try
{
FileOutputStream dest = new java.io.FileOutputStream("F:\\folder1");

byte[] buf =(byte[])Array.newInstance(java.lang.Byte.TYPE,8192);
System.out.println(buf);

int byteCount;

while ( (byteCount = ip.read(buf)) > 0 )
{
dest.write(buf,0,byteCount);
}
dest.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}
}

how to over come this error ,any one give me solution.

Thanks
Shiryans Nath
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an F drive that is mounted and permission to write to it? You'll get a file not found exception if the drive is unavailable.
Just a nitpick but why are you creating an array using reflection:

instead of the easy way:
 
Ishrayansh Nath
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an F drive that is mounted and permission to write it?
suppose not availble means how i can check to F drive mounted ?
 
Ishrayansh Nath
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for
URL hp = new URL("ftp://192.168.5.70//folder1"); // windows
can i replace it
userid="root"
pwd="ddd"
host:"192.152.45.8"
voice1 is folder name.
String path="ftp:/"+userid +":"+pwd+"@"+host+":"+"21"+"/"+"voice1";
// solaris
System.out.println("path :"+path);
URL hp = new URL(path);

connect is refused ,

how to connect solaris machine using URL Class

how to give the proper path value
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiryans Nath:
I have an F drive that is mounted and permission to write it?
suppose not availble means how i can check to F drive mounted ?

Create a File object for the root and call exists()
[ March 24, 2005: Message edited by: Joe Ess ]
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiryans Nath:
for
String path="ftp:/"+userid +":"+pwd+"@"+host+":"+"21"+"/"+"voice1";

You are missing the double-slash after the protocol. Since you print out the path variable it would be helpful to us if you would share the output.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!