Solved

Ftp Access from java

Posted on 2004-04-06
7
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

632 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