最近在做一个简单的ftp客户端,基本上已经搞定了,现在的要求是给定一个远程服务器上目录,然后要我把这个目录的树形结构反应到xml文件里,注:ftp客户端是用 apache commons-net 的ftp 包,xml是用Jdom操作的。
代码如下
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class DirTreeXML {
FTPClient fClient = ConnectServer.getftpClient();
/**
* 生成xml文件
* @param dirPath 指定的目录的路径
*/
public void createXML(String dirPath) {
Element root = new Element("root");
root.setAttribute("path", dirPath);
Document doc = new Document(root);
XMLOutputter XMLOut = new XMLOutputter();
//设置所创建的XML文档的格式
Format format = Format.getPrettyFormat();
XMLOut.setFormat(format);
this.createTreeElement(dirPath,root);
try {
XMLOut.output(doc,new FileOutputStream("dir_list.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建整个目录的树形结构
* @param dirPath 目录路径
* @param fatherElement 父节点
*/
public void createTreeElement(String dirPath,Element fatherElement) {
try {
FTPFile[] s = fClient.listFiles(SetCode.utf_iso(dirPath).replace("//","/"));
for (FTPFile f : s) {
Element currentElement = fatherElement; //当前的目录节点
String newRemote = (dirPath + f.getName() + "/").replace("//", "/");
if (f.isDirectory()) {
//Element dirElement = new Element(f.getName());
Element dirElement = new Element("dir");//目录节点
dirElement.setAttribute("name",f.getName());
fatherElement.addContent(dirElement);
currentElement = dirElement;
fClient.changeWorkingDirectory(SetCode.utf_iso(newRemote));
createTreeElement(newRemote,dirElement);
} else {
Element fileElement = new Element("file").setText(f.getName());//文件节点
currentElement.addContent(fileElement);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//测试函数
public static void main(String[] args) {
DirTreeXML test= new DirTreeXML();
test.createXML("/新建文件夹/");
ConnectServer.disconnect();
}
}
生成的xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<root path="/新建文件夹/">
<dir name="新建文件夹">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
<dir name="新建文件夹">
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 WPS文字 文档.wps</file>
</dir>
</dir>
<dir name="新建文件夹 (2)">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
</dir>
<dir name="新建文件夹 (3)">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
<dir name="新建文件夹" />
</dir>
<dir name="新建文件夹 (4)">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
<dir name="新建文件夹" />
<dir name="新建文件夹 (2)" />
</dir>
<dir name="新建文件夹 (5)">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
<dir name="新建文件夹51">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
</dir>
<dir name="新建文件夹52">
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 Microsoft Word 文档.doc</file>
<file>新建 文本文档.txt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Excel 工作表.xls</file>
</dir>
</dir>
<file>新建 文本文档.txt</file>
<file>新建 Microsoft Excel 工作表.xls</file>
<file>新建 WinRAR 压缩文件.rar</file>
<file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
<file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
<file>新建 WPS文字 文档.wps</file>
<file>新建 Microsoft Word 文档.doc</file>
</root>