下面是测试程序的源码:
import java.io.IOException;
import java.util.Date;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class Test {
private static FTPClient ftp;
private static String host = "222.22.7.215";
private static String user = "anonymous";
private static String pass = "";
private static int recNum = 0;
public static void main(String[] args) {
ftp = new FTPClient();
boolean error = false;
try {
int reply;
ftp.connect(host);
System.out.println("Connected to " + host);
System.out.print(ftp.getReplyString());
// After connection attempt, you should check the reply code to
// verify
// success.
reply = ftp.getReplyCode();
//ftp.setControlEncoding("UTF-8");
ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
ftp.login(user, pass);
// transfer files
/*
* FTPFile[] files = ftp.listFiles(); for (int i = 0; i <
* files.length; i++) { System.out.println(files[i].getName());
*
* }
*/
Date startTime = new Date();
traverse(ftp, ftp.printWorkingDirectory());
Date endTime = new Date();
System.out.println("遍历耗时" + String.valueOf(endTime.getTime() - startTime.getTime() / 60000.0) + "分钟");
System.out.println("共有文件数:" + recNum + "个");
/*
* String[] names = ftp.listNames(); for (String string : names) {
* System.out.println(string); }
* System.out.println(ftp.printWorkingDirectory());
* System.out.println(ftp.getBufferSize());
*/
ftp.logout();
} catch (IOException e) {
error = true;
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
// do nothing
}
}
System.exit(error ? 1 : 0);
}
}
public static void traverse(FTPClient client, String path) {
String prefix = "";
try {
FTPFile[] files = client.listFiles(path);
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals(".") || files[i].getName().equals("..")) {
continue;
} else if (files[i].isDirectory()) {
String tempDir = client.printWorkingDirectory() + "/" + files[i].getName();
// tempDir = new String(tempDir.getBytes(), "UTF-8");
// System.out.println("Directory:" + tempDir);
//System.out.println(client.changeWorkingDirectory(tempDir))
// ;
client.changeWorkingDirectory(tempDir);
traverse(client, tempDir);
prefix += client.printWorkingDirectory();
client.changeToParentDirectory();
} else {
String temp = client.printWorkingDirectory();
if (temp.equals("/")) {
recNum++;
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out
.println("URL:" + temp + new String(files[i].getName().getBytes()));
System.out.println(files[i].getSize() / 1000000.0 + "MB");
System.out.println(files[i].getTimestamp().getTime());
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("\r\n\r\n");
} else {
recNum++;
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("URL:" + temp + "/"
+ new String(files[i].getName().getBytes()));
System.out.println(files[i].getSize() / 1000000.0 + "MB");
System.out.println(files[i].getTimestamp().getTime());
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("\r\n\r\n");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5 个解决方案
#1
up,mark,学习,先标记上。
#2
system.out
.println("URL:" + temp + new String(files[i].getName().getBytes()));
在取得文件名称后,要重新解码,
system.out
.println("URL:" + temp + new String(files[i].getName().getBytes("GBK"), "iso-8859-1")));
但是前提条件是,在遍历文件名的时候,使用iso-8859-1没有丢失中文信息,如果已经丢失,是没有办法取回来的。
楼主试试。
#3
谢谢,我已经找到解决的办法了,谢谢你!
#4
解决方法分享一下啊!
#5
解决方法就在主函数
public static void main(String[] args) {
ftp = new FTPClient();
ftp.setControlEncoding("GBK"); //添上这句设置编码就没中文乱码了
public static void main(String[] args) {
ftp = new FTPClient();
ftp.setControlEncoding("GBK"); //添上这句设置编码就没中文乱码了
#1
up,mark,学习,先标记上。
#2
system.out
.println("URL:" + temp + new String(files[i].getName().getBytes()));
在取得文件名称后,要重新解码,
system.out
.println("URL:" + temp + new String(files[i].getName().getBytes("GBK"), "iso-8859-1")));
但是前提条件是,在遍历文件名的时候,使用iso-8859-1没有丢失中文信息,如果已经丢失,是没有办法取回来的。
楼主试试。
#3
谢谢,我已经找到解决的办法了,谢谢你!
#4
解决方法分享一下啊!
#5
解决方法就在主函数
public static void main(String[] args) {
ftp = new FTPClient();
ftp.setControlEncoding("GBK"); //添上这句设置编码就没中文乱码了
public static void main(String[] args) {
ftp = new FTPClient();
ftp.setControlEncoding("GBK"); //添上这句设置编码就没中文乱码了