Solved

Ftp Access from java

Posted on 2004-04-06
7
388 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

747 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

13 Experts available now in Live!

Get 1:1 Help Now