java 访问 ftp,Java:从FTP服务器访问文件

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: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 "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?

解决方案

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

URL url = new URL ("ftp://username:password@www.superland.example/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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值