Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ftp Access from java

Posted on 2004-04-06
7
Medium Priority
?
397 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 800 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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