Solved

Ftp Access from java

Posted on 2004-04-06
7
389 Views
Last Modified: 2008-02-01
Experts

I want to know if anyone has a simple library that:

Retrieve a file from ftp.
write a file to ftp.
get a directory listing.

Thanks.
0
Comment
Question by:Havalchy
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765153
Here's some upload code:

import sun.net.ftp.*;

      class Uploader  {
            FtpClient client;

            private int imageNumber;


            /**
             *  Log in to server
             */
            private void login() {
                  try {
                        client = new FtpClient(FTP_URL);
                        client.login("username", "password");
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }

            }

            private void upload(BufferedImage image) {
                  try {
                        TelnetOutputStream out = client.put(IMAGE_URL);
                        ImageIO.write(image, "JPG", out);
                        out.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10765162
...and some complete listing code:



import sun.net.ftp.*;
import java.io.*;
import java.util.*;
import sun.net.*;


public class FtpList {
  String username = "anonymous";
  String password = "ident";


  public static void main(String[] args){
    if(args.length < 2){
     System.out.println("Usage: java FtpList <host> <remote directory>");
     System.exit(-1);
    }
    FtpClient client = null;
    TelnetInputStream tis = null;
    BufferedReader in = null;
    String fileName = null;
    try {
      client = new FtpClient(args[0]);
      client.login(USERNAME,PASSWORD);
      client.cd(args[1]);
      tis = client.list();
      in = new BufferedReader(new InputStreamReader(tis));
      ArrayList fileList = new ArrayList();
      while ((fileName = in.readLine()) != null){
        System.out.println(fileName);
        fileList.add(fileName);
      }
    }
    catch (IOException e){
      e.printStackTrace();
    }
    finally {
      try { in.close(); } catch(IOException e) { e.printStackTrace(); }
    }
 }

}
0
 
LVL 1

Author Comment

by:Havalchy
ID: 10765486
CEHJ

do you have the function that retrieves a file from the ftp, also do you know of any well documented standard libraries, I could not find the documentation for the  Sun.net.ftp
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 10765545
It would be basically the opposite of the upload one

put->get
TelnetOutputStream->TelnetInputStream


final int BUF_SIZE = 4096;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
     out.write(buffer, 0, bytesRead); // out is a BufferedOutputStream on a file
}
// close both streams
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 50 total points
ID: 10765693
You can also use the finj library :
  http://finj.sourceforge.net/test.php
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10766468
8-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10768549
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 102
firstswap challenge 20 68
JDeveloper 12c for 32 bit 4 71
ArrayIndexOutOfBoundException 9 68
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now