import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
public class FtpTest {
public static Logger log = Logger.getLogger(FtpTest.class.getName());
public static String ftpIp = "169.254.127.232";
public static String PORT = "21";
public static String USER = "chenxin";
public static String PWD = "111";
public static String localPath = "d:\\_test\\";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FTPClient ftpClient = null;
FTPClientConfig ftpConfig = null;
List downList = new ArrayList();
try {
ftpClient = new FTPClient();
// 配置UNIX FTP Server
ftpConfig = new FTPClientConfig("WINDOWS");
ftpConfig.setServerLanguageCode("EN");
ftpClient.configure(ftpConfig);
ftpClient.connect(ftpIp, Integer.parseInt(PORT));
ftpClient.setControlEncoding("ISO-8859-1");
ftpClient.enterLocalPassiveMode();// 设置被动模式
// // LOGIN FTP SERVER
// if (ftpClient.login(USER, PWD) == false) {
// return;
// }
ftpClient.login(null, null);
// 用户FTP HOME
String rootPath = ftpClient.printWorkingDirectory();
// 退到根路径
ftpClient.changeWorkingDirectory(File.separator);
log.info("退到根目录:" + ftpClient.printWorkingDirectory());
// 进入指定目录,自己建立一个名字为ftp的目录
ftpClient.changeWorkingDirectory("ftp");
log.info("从远程目录:" + ftpClient.printWorkingDirectory()
+ ",获取文件进行中.....");
// 改变上传模式使用BIN形式
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
try {
// 文件名列表
String[] names = ftpClient.listNames("*");
if (names == null || names.length == 0) {
return;
}
log.info("names.length:" + names.length);
for (int i = 0; i < names.length; i++) {
if(names[i].equals(".") || names[i].equals(".."))
continue;
try {
File file = new File(localPath + names[i]);
FileOutputStream fos = new FileOutputStream(file);
if (ftpClient.retrieveFile(names[i], fos))
log.info("下载" + names[i] + "到" + localPath
+ "路径下成功!");
fos.close();
} catch (Exception e) {
log.error(e);
continue;
}
}
} catch (Exception e) {
log.error(e);
return;
}
} catch (Exception e) {
}
}
}
写null不管用啊,匿名如何登陆啊.......还有就是中文是乱码 怎么办啊..