Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

writng Java FTP program -- how to

Thread view: 
cartercc@gmail.com - 25 Aug 2006 20:32 GMT
I'm writing a class that will connect to and download an ASCII file via
FTP. I currently do this manually, but am automating the process. I've
reached a sticking point, and have not been able to progress for
several days, and would very much appreciate any (and all) suggestions.

The local machine is a Fedora machine and the remote machine is an AIX
machine. I have no control over the remote machine except to connect to
it and download a file.

FIRST ---------------------------
when I ftp the file manually, this is what the session looks like.

[root@serctamfedora java-bin]# ftp 9.9.9.9
Connected to 9.9.9.9
220 datatel FTP server (Version 4.1 Tue Jul 6 21:20:07 CDT 2004) ready.
502 authentication type cannot be set to GSSAPI
502 authentication type cannot be set to KERBEROS_V4
KERBEROS_V4 rejected as an authentication type
Name (9.9.9.9:root): username
331 Password required for username.
Password:
230-Last unsuccessful login: Mon Jul 31 08:59:55 CDT 2006 on ftp from
::ffff:9.9.9.8
230-Last login: Fri Aug 25 09:20:13 CDT 2006 on ftp from ::ffff:9.9.9.7
230 User username logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /cdata/collive/_HOLD_
250 CWD command successful.
ftp> get CCC.SER.PROGRAMS
local: CCC.SER.PROGRAMS remote: CCC.SER.PROGRAMS
227 Entering Passive Mode (9,9,9,9,139,242)
150 Opening data connection for CCC.SER.PROGRAMS (241643 bytes).
226 Transfer complete.
241643 bytes received in 3 seconds (78 Kbytes/s)
ftp> bye
[root@serctamfedora java-bin]#

SECOND ---------------------------
I've written a perl script that automates this process. Here is the
perl script. This works!!

1 #!/usr/bin/perl
2
3 use strict;
4 use Net::FTP;
5
6 my ($username, $password, $holdfile, $newname);
7
8 # Step 1 -- obtain ftp arguments: username, password, holdfile,
newname
9 print "Enter Datatel username: ";
10 $username = <STDIN>;
11 chomp $username;
12
13 print "Enter password: ";
14 $password = <STDIN>;
15 chomp $password;
16
17 print "Enter name of hold file: ";
18 $holdfile = <STDIN>;
19 chomp $holdfile;
20
21 ($newname = $holdfile) =~ s/\./_/g;
22 $newname .= '.txt';
23
24 # Step 2 -- FTP to server and download the holdfile
25 my $ftp = Net::FTP->new("9.9.9.9", Debug => 0) or die "Cannot
connect to Datatel, $@";
26 $ftp->login($username, $password) or die "Cannot login to Datatel ",
$ftp-> message;
27 $ftp->cwd("/cdata/collive/_HOLD_") or die "Cannot change working
directory to /cdata/collive/_HOLD_ ", $ftp->message;
28 $ftp->ascii;
29 $ftp->get($holdfile) or die "Cannot get $holdfile ", $ftp->message;
30 $ftp->quit;
31
32 # Step 3 -- rename the local file replacing dots with underscores
and adding '.txt' extension
33 rename $holdfile, $newname;
34
35 # Step 4 -- Miller time!
36 exit();

THIRD----------------------------------
here is the Java class file.

[root@serctamfedora java-bin]# vi TestFtp1.java
     1 import java.io.*;
     2 import java.lang.*;
     3 import java.net.*;
     4 import org.apache.commons.net.ftp.*;
     5
     6
     7 public class TestFtp1
     8 {
     9     public static void main(String[] args) throws IOException
    10     {
    11         FTPClient ftp = new FTPClient();
    12         String reply;
    13         boolean success;
    14         OutputStream out = new
FileOutputStream("downloadfile.txt");
    15
    16         try
    17         {
    18             ftp.connect("9.9.9.9");
    19             reply = ftp.getReplyString();
    20             System.out.println(reply);
    21
    22             ftp.login("username","password");
    23             reply = ftp.getReplyString();
    24             System.out.println(reply);
    25
    26             //ftp.setFileType(FTP.ASCII_FILE_TYPE);
    27             //reply = ftp.getReplyString();
    28             //System.out.println(reply);
    29
    30             ftp.enterRemotePassiveMode();
    31             reply = ftp.getReplyString();
    32             System.out.println(reply);
    33
    34             ftp.changeWorkingDirectory("/dir/subdir/_HOLD_");
    35             reply = ftp.getReplyString();
    36             System.out.println(reply);
    37
    38             reply = ftp.printWorkingDirectory();
    39             System.out.println(reply);
    40
    41             success = ftp.retrieveFile("CCC.SER.PROGRAMS",out);
    42             reply = ftp.getReplyString();
    43             System.out.println(reply);
"TestFtp1.java" 62L, 1465C

FOURTH---------------------------------
here is the output when I run the Java application.

[root@serctamfedora java-bin]# java TestFtp1
220 datatel FTP server (Version 4.1 Tue Jul 6 21:20:07 CDT 2004) ready.

230-Last unsuccessful login: Thu Aug 24 09:52:47 CDT 2006 on
/dev/pts/63 from 9.9.9.7
230-Last login: Fri Aug 25 10:11:55 CDT 2006 on ftp from ::ffff:9.9.9.6
230 User username logged in.

227 Entering Passive Mode (9,9,9,9,139,234)

250 CWD command successful.

/cdata/collive/_HOLD_
org.apache.commons.net.io.ToNetASCIIOutputStream@a62fc3
257 "/cdata/collive/_HOLD_" is current directory.

The value of success is false
Ftp client is not connected.

QUESTION---------------------------
Does anyone have an idea as to what I am doing wrong or what can make
this work? I'm probably doing something real stupid, but for the life
of me I can't figure out what it is.

If you have an FTP class that I can use and that work, I would
appreciate it very much if you would tell me where I can get it.

CC
Andrey Kuznetsov - 25 Aug 2006 20:55 GMT
> I'm writing a class that will connect to and download an ASCII file via
> FTP. I currently do this manually, but am automating the process. I've
> reached a sticking point, and have not been able to progress for
> several days, and would very much appreciate any (and all) suggestions.

...

> QUESTION---------------------------
> Does anyone have an idea as to what I am doing wrong or what can make
[quoted text clipped - 3 lines]
> If you have an FTP class that I can use and that work, I would
> appreciate it very much if you would tell me where I can get it.

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

more about java ftp here (pretty old):
http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html

Andrey

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Mark Space - 25 Aug 2006 21:38 GMT
> QUESTION---------------------------
> Does anyone have an idea as to what I am doing wrong or what can make
> this work? I'm probably doing something real stupid, but for the life
> of me I can't figure out what it is.

Why bother with any of this stuff at all?  Is plain old ftp with a
command file not good enough?

ftp < command_file

Then automate it completely with cron:
http://www.unixgeeks.org/security/newbie/unix/cron-1.html

But maybe you've already looked into this...
as4109@wayne.edu - 25 Aug 2006 22:28 GMT
cartercc@gmail.com ha escrito:

> [...]
>
> If you have an FTP class that I can use and that work, I would
> appreciate it very much if you would tell me where I can get it.

Have you tried "java.net.URL", like this? :

URL url=new URL("ftp://9.9.9.9/path/to/file");
InputStream uis = URL.getContent();
OutputSteam fos = new FileOutputStream("./local.file");
int n;
byte[] buf = new byte[4096];
while ((n=uis.read(buf,0,buf.length))>0)
  fos.write(buf,0,n);
fos.close();
hiwa - 26 Aug 2006 00:29 GMT
as4109@wayne.edu のメッセージ:

> cartercc@gmail.com ha escrito:
>
[quoted text clipped - 13 lines]
>    fos.write(buf,0,n);
> fos.close();
Did you try this?
I'm afraind it won't work.
Arne Vajhøj - 26 Aug 2006 01:01 GMT
> as4109@wayne.edu のメッセージ:
>
[quoted text clipped - 16 lines]
> Did you try this?
> I'm afraind it won't work.

With:

InputStream uis = url.openConnection().getInputStream();

it works here.

Arne
artbaeyr123@yahoo.com - 26 Aug 2006 16:57 GMT
May be I have such FTP class. I have alot of FTP classes. What kind of class
you need?
FTP use two connection.
One is control connection and another one is transfer connection.
If you want to download file you need create both. I do not thing 1 class it
is enought for this task.
You may use FtpClient from Sun.
you can look my FTP client on
http://www.ftpapplet.uni.cc , if you like it I can help you with yours.

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.