Solved

Ftp Access from java

Posted on 2004-04-06
7
390 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
factorial example 4 39
java jdbc batch example 8 32
MySqlDump not dumping triggers 1 31
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 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