Java: Accessing a File from an FTP Server

49,385

Solution 1

File objects cannot handle an FTP connection, you need to use a URLConnection:

URL url = new URL ("ftp://username:[email protected]/server");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();
...

Consider as an alternative FTPClient from Apache Commons Net which has support for many protocols. Here is an FTP list files example.

Solution 2

if you use URI with file you can use your code but , but when you want to use ftp so you need to this kind of code; code list the name of the files under your ftp server

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL url = new URL("ftp://username:[email protected]/server");
        URLConnection con = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

EDITED Demo Code Belongs to Codejava

package net.codejava.ftp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FtpUrlListing {

    public static void main(String[] args) {
        String ftpUrl = "ftp://%s:%s@%s/%s;type=d";
        String host = "www.myserver.com";
        String user = "tom";
        String pass = "secret";
        String dirPath = "/projects/java";

        ftpUrl = String.format(ftpUrl, user, pass, host, dirPath);
        System.out.println("URL: " + ftpUrl);

        try {
            URL url = new URL(ftpUrl);
            URLConnection conn = url.openConnection();
            InputStream inputStream = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line = null;
            System.out.println("--- START ---");
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("--- END ---");

            inputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Share:
49,385
Rich Young
Author by

Rich Young

Updated on December 12, 2020

Comments

  • Rich Young
    Rich Young over 3 years

    So I have this FTP server with a bunch of folders and files inside.

    My program needs to access this server, read all of the files, and display their data.

    For development purposes I've been working with the files on my hard drive, right in the "src" folder.

    But now that the server is up and running, I need to connect the software to it.

    Basically what I want to do is get a list of the Files in a particular folder on the server.

    This is what I have so far:

    URL url = null;
    File folder = null;
    try {
        url = new URL ("ftp://username:[email protected]/server");
        folder = new File (url.toURI());
    } catch (Exception e) {
        e.printStackTrace();
    }
    data = Arrays.asList(folder.listFiles(new FileFilter () {
        public boolean accept(File file) {
            return file.isDirectory();
        }
    }));
    

    But I get the error "URI scheme is not 'file'."

    I understand this is because my URL starts with "ftp://" and not "file:"

    However I can't seem to figure out what I'm supposed to do about it!

    Maybe there's a better way to go about this?

  • Rich Young
    Rich Young over 11 years
    The thing about a URLConnection is that I can only get an InputStream from it. If I could use that InputStream to then get a list of the files, sure...
  • Rich Young
    Rich Young over 11 years
    The server folder, right now, has only test.txt in it. When I do this I get the following output: drwx---rwx 2 trucker2 inetuser 4096 Jan 22 12:40 . drwx---r-x 10 trucker2 inetuser 4096 Jan 22 10:53 .. -rw----r-- 1 trucker2 inetuser 8 Jan 22 12:40 test.txt So how do I use this to read the files... ?
  • Rich Young
    Rich Young over 11 years
    Looking at the example, on line 13 it says client.connect("ftp.server.com"); but I get an error when I try this, it says this method is undefined for FTPClient.
  • Reimeus
    Reimeus over 11 years
    That method is definitely there. Do you have the correct import?
  • Rich Young
    Rich Young over 11 years
    My bad, I imported wrong. I hate using third-party code. But now I'm trying listFiles() and it takes like five minutes to load, and comes up with nothing.
  • erhun
    erhun over 6 years
    These code list the name of the files under your directory,actually it lists . current directory, .. parent directory and your test.txt if you want to get more details about these files you can check Edited link.