Java: accessing a file from an FTP server

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

My program should access this server, read all the files and display their data.

For development purposes, I worked with files on my hard drive directly in the "src" folder.

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

Basically, I want to get a list of files in a specific folder on the server.

This is what I have so far:

URL url = null; File folder = null; try { url = new URL ("ftp://username: password@www.superland.example /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 "The URI scheme is not a file."

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

However, I cannot understand what I should do with this!

Maybe there is a better way to do this?

+6
source share
2 answers

File objects can not handle the connection FTP , you need to use URLConnection :

 URL url = new URL ("ftp://username: password@www.superland.example /server"); URLConnection urlc = url.openConnection(); InputStream is = urlc.getInputStream(); ... 

Consider an alternative to FTPClient from Apache Commons Net , which supports many protocols. The following is an example FTP list file .

+8
source

If you use a URI with a file, you can use your code, but, but if you want to use ftp, then you need this code; code list file name 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: password@www.superland.example /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(); } } } 
+3
source

All Articles