import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;/**
* ftp Client
*
* @author cuiyh
* @version $Revision: 1.1 $ $Date: 2006/06/02 02:50:54 $
*/public class FtpClient {
private String server; private String username; private String password; private FTPClient ftp; private boolean binaryTransfer = false; private final static Logger log = Logger.getLogger(FtpClient.class); /**
* @param server ftp服务器地址
* @param username ftp服务器登陆用户
* @param password ftp用户密码
*/
public FtpClient(String server, String username, String password) { this.server = server;
this.username = username;
this.password = password;
ftp = new FTPClient();
/*
* if(Configuration.PrintFTPCommandLog){ //打印FTP命令
* ftp.addProtocolCommandListener(new PrintCommandListener()); }
*/
} /**
* 根据配置文件构建一个FtpClient
*/
public FtpClient() {
this(Configuration.FtpServer, Configuration.FtpUser, Configuration.FtpPassword);
} public boolean connect() {
try {
int reply;
ftp.connect(server); // 连接后检测返回码来校验连接是否成功
reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) {
if (ftp.login(username, password)) {
// 设置为passive模式
ftp.enterLocalPassiveMode();
return true;
}
} else {
ftp.disconnect();
log.error("FTP server refused connection.");
}
} catch (IOException e) {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
}
}
log.error("Could not connect to server.", e);
}
return false;
} /**
* 下载一个文件到默认的本地路径中
*
* @param fileName 文件名称(不含路径)
* @param delFile 成功后是否删除该文件
* @return
*/
public boolean get(String fileName, boolean delFile) {
String remote = Configuration.RemoteDownPath + fileName;
String local = Configuration.LocalDownPath + fileName;
return get(remote, local, delFile);
} /**
* 上传一个文件到默认的远程路径中
*
* @param fileName 文件名称(不含路径)
* @param delFile 成功后是否删除该文件
* @return
*/
public boolean put(String fileName, boolean delFile) {
String remote = Configuration.RemoteUpPath + fileName;
String local = Configuration.LocalUpPath + fileName;
return put(remote, local, delFile);
} /**
* 上传多个文件到默认的远程路径中
*
* @param fileNames 文件名数组
* @param delFile 成功后是否删除文件
* @return
*/
public boolean[] put(String[] fileNames, boolean delFile) {
boolean[] result = new boolean[fileNames.length];
for (int j = 0; j < result.length; j++) {
result[j] = false;
}
String remoteFile;
String localFile;
for (int i = 0; i < fileNames.length; i++) {
localFile = fileNames[i];
result[i] = put(localFile, delFile);
}
return result;
} /**
* 上传一个本地文件到远程指定文件
*
* @param remoteAbsoluteFile 远程文件名(包括完整路径)
* @param localAbsoluteFile 本地文件名(包括完整路径)
* @return 成功时,返回true,失败返回false
*/
private boolean put(String remoteAbsoluteFile, String localAbsoluteFile,
boolean delFile) {
InputStream input = null;
try {
// //设置文件传输类型
if (binaryTransfer) {
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
// 处理传输
input = new FileInputStream(localAbsoluteFile);
ftp.storeFile(remoteAbsoluteFile, input);
log.debug("put " + localAbsoluteFile);
input.close();
if (delFile) {
(new File(localAbsoluteFile)).delete();
}
log.debug("delete " + localAbsoluteFile);
return true;
} catch (FileNotFoundException e) {
log.error("local file not found.", e);
} catch (IOException e1) {
log.error("Could put file to server.", e1);
} finally {
try {
if (input != null) {
input.close();
}
} catch (Exception e2) {
}
} return false;
} /**
* 下载一个远程文件到本地的指定文件
*
* @param remoteAbsoluteFile 远程文件名(包括完整路径)
* @param localAbsoluteFile 本地文件名(包括完整路径)
* @return 成功时,返回true,失败返回false
*/
public boolean get(String remoteAbsoluteFile, String localAbsoluteFile,
boolean delFile) {
OutputStream output = null;
try {
// 设置文件传输类型
if (binaryTransfer) {
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
// 处理传输
output = new FileOutputStream(localAbsoluteFile);
ftp.retrieveFile(remoteAbsoluteFile, output);
output.close();
if (delFile) { // 删除远程文件
ftp.deleteFile(remoteAbsoluteFile);
}
return true;
} catch (FileNotFoundException e) {
log.error("local file not found.", e);
} catch (IOException e1) {
log.error("Could get file from server.", e1);
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException e2) {
}
}
return false;
} /**
* 列出远程目录下所有的文件
*
* @param remotePath 远程目录名
* @return 远程目录下所有文件名的列表,目录不存在或者目录下没有文件时返回0长度的数组
*/
public String[] listNames(String remotePath) {
String[] fileNames = null;
try {
FTPFile[] remotefiles = ftp.listFiles(remotePath);
fileNames = new String[remotefiles.length];
for (int i = 0; i < remotefiles.length; i++) {
fileNames[i] = remotefiles[i].getName();
} } catch (IOException e) {
log.error("Could not list file from server.", e);
}
return fileNames;
} /**
* 断开ftp连接
*/
public void disconnect() {
try {
ftp.logout();
if (ftp.isConnected()) {
ftp.disconnect();
}
} catch (IOException e) {
log.error("Could not disconnect from server.", e);
}
} /**
* @return Returns the binaryTransfer.
*/
public boolean isBinaryTransfer() {
return binaryTransfer;
} /**
* @param binaryTransfer The binaryTransfer to set.
*/
public void setBinaryTransfer(boolean binaryTransfer) {
this.binaryTransfer = binaryTransfer;
} public static void main(String[] args) {
FtpClient ftp = new FtpClient("130.85.51.131", "cbs", "cbs");
ftp.connect();
String[] temp = ftp.listNames("/tuxlog/cbs");
System.out.println("connect sucess");
System.out.println(temp.length);
// ftp.put("test.txt", true);
// System.out.println("upload sucess"); /*boolean re = ftp.get("/expert/acc/bill/src/test.txt", "c://test02.txt",
false); if (re) {
System.out.println("down sucess");
} else {
System.out.println("down sucess");
}*/
/*
* if(ftp.get("test.txt",false)){ System.out.println("down sucess");
* }else{ System.out.println("down faile"); }
*/
ftp.disconnect();
}
}
为什么会显示长度为零呢?此路径下有文件的。用windows的ftp命令显示是成功的。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* 使用commons的net包进行ftp链接.
* 相关包:commons-net-1.4.1.jar ; commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件
* 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名
* @author
*
*
* @create 2010-12-06
*
*/
public class ListFtpFile {
private static final Log logger = LogFactory.getLog(ListFtpFile.class);
private String hostName; private int serverPort; private String serverPath; private String userName; private String password;
private boolean binaryMode = true; private boolean passiveMode = false;
public String getHostName() {
return hostName;
} public void setHostName(String hostName) {
this.hostName = hostName;
} public int getServerPort() {
return serverPort;
} public void setServerPort(int serverPort) {
this.serverPort = serverPort;
} public String getServerPath() {
return serverPath;
} public void setServerPath(String serverPath) {
this.serverPath = serverPath;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
public static Log getLogger() {
return logger;
} public boolean putFile( String remoteFile, String localFile, boolean delFile){
boolean flag = false;
FTPClient ftp = new FTPClient();
InputStream input = null;
try {
input = new FileInputStream(new File(localFile));
int reply;
ftp.connect(this.hostName, this.serverPort);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(this.userName, this.password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
logger.error("failed to connect to the FTP Server:");
return flag;
}
else{
if (binaryMode) {
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
}
if (passiveMode) {
ftp.enterLocalPassiveMode();
} else {
ftp.enterRemotePassiveMode();
}
}
ftp.changeWorkingDirectory(serverPath);
ftp.storeFile(remoteFile, input);
input.close();
ftp.logout();
if (delFile) { //删除本地文件
(new File(localFile)).delete();
logger.debug("delete " + localFile);
}
flag = true;
} catch (IOException e) {
logger.debug(e.getMessage());
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.debug("Could not connect to server. " + ioe);
}
}
}
return flag;
}
public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File("D:/app/weblogic/file/KEGUAN/Hello.java"));
boolean flag = uploadFile("192.168.60.163", 21, "nbftp", "nbftp", "/kgfj/download/", "Hello.java", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}
怎么报错!~~??