【问题标题】:Java: Accessing a File from an FTP ServerJava:从 FTP 服务器访问文件
【发布时间】:2023-03-25 23:13:01
【问题描述】:

所以我有这个 FTP 服务器,里面有一堆文件夹和文件。

我的程序需要访问这个服务器,读取所有文件,并显示它们的数据。

出于开发目的,我一直在处理我硬盘上的文件,就在“src”文件夹中。

但是现在服务器已经启动并运行,我需要将软件连接到它。

基本上我想要做的是获取服务器上特定文件夹中的文件列表。

这是我目前所拥有的:

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();
    }
}));

但我收到错误消息“URI 方案不是‘文件’。”

我明白这是因为我的网址以“ftp://”而不是“file:”开头

但是我似乎无法弄清楚我应该怎么做!

也许有更好的方法来解决这个问题?

【问题讨论】:

  • 而且这个问题已经被问过很多次了;也许其他答案之一会对您有所帮助:stackoverflow.com/…
  • 你想乐于助人还是愤世嫉俗?我希望。问题是我需要一个文件列表。我不知道有什么其他方法可以解决这个问题。
  • 不确定是否可以使用 URL。您可能想查看专用的 FTP 库,例如 Apache Net Commons 或 edtFTP4j
  • 我尝试使用 edtFTPj/Free,但是当我到达 FTPFile [] files = ftp.directoryList(); 时,它冻结了一分钟左右,并说“读取超时”。我想我可能应该开始一个线程,而不是。
  • @RichYoung 你必须小心,因为一些 ftp 服务器不允许目录列表。这取决于平台(IBM、Sun、Windows)及其配置方式。由于没有真正的标准方式来输出目录列表,ftp 客户端依赖于解析器,即使这样,它们也会对它们正在与哪个平台进行有根据的猜测。我主要使用 Net Commons(commons net?),因为它很容易启动和运行,但主要是因为它们支持 FTPS。

标签: java file url ftp


【解决方案1】:

如果你使用 URI 和文件,你可以使用你的代码但是,但是当你想使用 ftp 时你需要这种代码; code 列出你的 ftp 服务器下的文件名

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();
    }
}

已编辑 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();
        }
    }
}

【讨论】:

  • 服务器文件夹现在只有 test.txt。当我这样做时,我得到以下输出: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 那么如何使用它来读取文件...?
  • 这些代码列出了你目录下的文件名,实际上它列出了.当前目录、.. 父目录和您的 test.txt 如果您想了解有关这些文件的更多详细信息,您可以查看已编辑链接。
【解决方案2】:

File 对象无法处理FTP 连接,您需要使用URLConnection

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

考虑作为FTPClient 的替代Apache Commons Net,它支持许多协议。这是FTP list files example

【讨论】:

  • 关于 URLConnection 的事情是我只能从中获取 InputStream。如果我可以使用该 InputStream 来获取文件列表,当然...
  • 查看示例,在第 13 行显示client.connect("ftp.server.com");,但当我尝试此操作时出现错误,它说此方法未定义用于 FTPClient。
  • 那个方法是definitely there。你有正确的导入吗?
  • 我的错,我输入错误。我讨厌使用第三方代码。但现在我正在尝试 listFiles() ,它需要大约五分钟才能加载,但什么也没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2021-08-16
相关资源
最近更新 更多