日期:2014-05-19  浏览次数:20670 次

java做个FTP下载 从根目录开始把所有的下载都本地怎么做啊 本人新手
java做个FTP  下载从根目录开始把所有文件和文件夹下载都本地怎么做啊 请求高手  有源码最好 因为没得接触过ftp这个块
------解决方案--------------------
/**
 * 
 */
package ftp;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

// import org.apache.commons.logging.Log;
// import org.apache.commons.logging.LogFactory;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpUpfile {
private FtpClient ftpclient;

private String ipAddress;

private int ipPort;

private String userName;

private String PassWord;

public FtpUpfile(String ip, int port, String username, String password) throws Exception {
ipAddress = new String(ip);
ipPort = port;
ftpclient = new FtpClient(ipAddress, ipPort);
userName = new String(username);
PassWord = new String(password);
}

public FtpUpfile(String ip, String username, String password) throws Exception {
ipAddress = new String(ip);
ipPort = 21;
ftpclient = new FtpClient(ipAddress, ipPort);
userName = new String(username);
PassWord = new String(password);
}

public void login() throws Exception {
ftpclient.login(userName, PassWord);
}

public void logout() throws IOException {
// 用ftpclient.closeServer()断开FTP出错时用下更语句退出
// ftpclient.sendServer("QUIT\r\n");
ftpclient.closeServer();
// int reply = ftpclient.readServerResponse(); // 取得服务器的返回信息
}

public void buildList(String pathList) throws IOException {
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); // sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}

public ArrayList fileNames(String fullPath) throws IOException {
ftpclient.ascii(); // 注意,使用字符模式
TelnetInputStream list = ftpclient.nameList(fullPath);
byte[] names = new byte[2048];
int bufsize = 0;
bufsize = list.read(names, 0, names.length); // 从流中读取
list.close();
ArrayList namesList = new ArrayList();
i