用Apache VFS实现FTP

来源:互联网 发布:换屏上门服务软件 编辑:程序博客网 时间:2024/06/05 19:25

提到假想文件系统(Virtual File System),大家并不陌生。

但是,在Java的文件操作中实现VFS功能我却从来都没有弄过。

Apache Common推出了Java版的VFS,目前最新版本是1.1。索性尝试了一下用Common VFS连接FTP服务器。

我在代码中添加了分析多国语言的处理,由于时间关系,以后再尝试一下SFTP。

代码如下:

 

import org.apache.commons.vfs.FileObject;import org.apache.commons.vfs.FileSystemException;import org.apache.commons.vfs.FileSystemManager;import org.apache.commons.vfs.VFS;/** * @author hcl * */public class TestFtpVFS {/** * @param args */public static void main(String[] args) {FileSystemManager manager;try {manager = VFS.getManager();FileObject ftpFile = manager.resolveFile("ftp://hcl:hcl@localhost:21/loveapple");FileObject[] children = ftpFile.getChildren();System.out.println( "Children of " + ftpFile.getName().getURI() );for (FileObject child : children) {String baseName = child.getName().getBaseName();System.out.println("ファイル名(日本語)-文件名(中文):" + baseName + "  --  " + new String(baseName.getBytes("iso-8859-1"),"UTF-8"));}} catch (FileSystemException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}}}

能够灵活运用VFS的好处这里不做介绍,有兴趣尝试一下VFS的朋友可以到官方下载相关数据包,地址如下http://commons.apache.org/vfs/

<script type="text/javascript"><!--google_ad_client = "pub-2097865745512830";/* 728x90, 作成済み 08/12/22 */google_ad_slot = "5461334436";google_ad_width = 728;google_ad_height = 90;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
转贴请注明出处:http://blog.csdn.net/froole