使用FTPClient上传下载文件夹

阅读更多

程序自动递归下载指定目录下的所有文件及子文件夹,上传时,自动递归创建服务器目录

/**

 * @author: bruce.huang

 * @createTime:2014年8月4日

 */

package com.framework.ftp;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.HashMap;

 

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;

 

import com.framework.xml.ServersFileReader;

import com.framework.xml.xmlbean.ServersBean;

 

/**

 * @author black

 * 

 */

public class FtpClient {

HashMap map = null;

private static final Logger log = Logger.getLogger(FtpClient.class);

 

public HashMap downLoad(String serverName) {

// "ftpServer"

map = new HashMap();

ServersBean cfb = ServersFileReader.readServerProperty().get(serverName);

FTPClient ftp = new FTPClient();

FileOutputStream fos = null;

try {

ftp.connect(cfb.getServerIP());

ftp.login(cfb.getServerUser(), cfb.getServerPassword());

 

String remoteFileName = cfb.getServerHomeDir();

fos = new FileOutputStream(remoteFileName);

 

ftp.setBufferSize(1024);

// 设置文件类型(二进制)

ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

ftp.retrieveFile(remoteFileName, fos);

 

this.downloadFiles(ftp, remoteFileName, cfb.getLocalHomeDir());

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException("FTP客户端出错!", e);

} finally {

try {

if (fos != null)

fos.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

ftp.disconnect();

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException("关闭FTP连接发生异常!", e);

}

}

return map;

}

 

/************************************

* 下载远程目录

* */

private void downloadFiles(FTPClient ftp, String remoteFileName, String localPath) throws IOException {

// 转移到FTP服务器目录

ftp.changeWorkingDirectory(remoteFileName);

FTPFile[] fs = ftp.listFiles();

for (FTPFile ff : fs) {

if (ff.isDirectory()) {

downloadFiles(ftp, remoteFileName + "/" + ff.getName(), localPath + "/" + ff.getName());

} else {

File localFile = new File(localPath + "/" + ff.getName());

if (!localFile.getParentFile().exists()) {

localFile.getParentFile().mkdirs();

}

OutputStream is = new FileOutputStream(localFile);

ftp.retrieveFile(ff.getName(), is);

map.put(ff.getName(), ff.getSize() + ""); // 放数据

log.info("ftp下载远程文件:" + remoteFileName + "/" + ff.getName() + "(字节数:" + ff.getSize() + ")" + "到" + localPath + "/" + ff.getName());

is.close();

}

}

 

}

 

public static boolean upload(String serverName) {

ServersBean serverBean = ServersFileReader.readServerProperty().get(serverName);

boolean success = false;

FTPClient ftp = new FTPClient();

try {

int reply;

ftp.connect(serverBean.getServerIP(), serverBean.getServerPort());// 连接FTP服务器

ftp.login(serverBean.getServerUser(), serverBean.getServerPassword());// 登录

ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

return success;

}

ftp.changeWorkingDirectory(serverBean.getServerHomeDir());

uploadFiles(ftp, serverBean.getServerHomeDir(), new File(serverBean.getLocalHomeDir()));

ftp.logout();

success = true;

} catch (IOException e) {

e.printStackTrace();

} finally {

if (ftp.isConnected()) {

try {

ftp.disconnect();

} catch (IOException ioe) {

}

}

}

return success;

}

 

/*********************

* 上传文件或文件夹

* */

private static void uploadFiles(FTPClient ftp, String remotePath, File localFile) {

if (localFile.isDirectory()) {

for (File ff : localFile.listFiles()) {

if (ff.isDirectory()) {

try {

ftp.changeWorkingDirectory(remotePath);

if (!ftp.changeWorkingDirectory(remotePath + "/" + localFile.getName())) {

log.info("FTP到目录(" + remotePath + ")下创建文件夹:" + ff.getName());

ftp.makeDirectory(ff.getName());

}

uploadFiles(ftp, remotePath + "/" + ff.getName(), ff);// 递归调用,传文件夹

} catch (IOException e) {

log.error("FTP服务器创建远程目录(" + remotePath + "/" + ff.getName() + ")失败:" + e.getMessage());

}

} else {

uploadFile(ftp, remotePath, ff);

}

}

} else {

uploadFile(ftp, remotePath, localFile);

}

}

 

/*****************************************

* 上传单个文件

* */

private static void uploadFile(FTPClient ftp, String remotePath, File localFile) {

try {

log.info("开始上传文件到:" + remotePath + "/==" + localFile.getName());

ftp.changeWorkingDirectory(remotePath);

ftp.storeFile(localFile.getName(), new FileInputStream(localFile));

} catch (IOException e) {

log.error("FTP向服务器(" + ftp.getRemoteAddress().getHostAddress() + ")上传文件(" + localFile.getAbsolutePath() + ")失败:" + e.getMessage());

}

}

}

 

 

 

文件引用的服务对象

/**

 * @author: bruce.huang

 * @createTime:2014年7月27日

 */

package com.framework.xml.xmlbean;

 

/**

 * @author black

 * 

 */

public class ServersBean {

private String name;

private String description;

private String serverIP;

private int serverPort;

private String serverUser;

private String serverPassword;

private String serverHomeDir;

private String localHomeDir;

 

public String getLocalHomeDir() {

return localHomeDir;

}

public void setLocalHomeDir(String localHomeDir) {

this.localHomeDir = localHomeDir;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public String getServerIP() {

return serverIP;

}

public void setServerIP(String serverIP) {

this.serverIP = serverIP;

}

public int getServerPort() {

return serverPort;

}

public void setServerPort(int serverPort) {

this.serverPort = serverPort;

}

public String getServerUser() {

return serverUser;

}

public void setServerUser(String serverUser) {

this.serverUser = serverUser;

}

public String getServerPassword() {

return serverPassword;

}

public void setServerPassword(String serverPassword) {

this.serverPassword = serverPassword;

}

public String getServerHomeDir() {

return serverHomeDir;

}

public void setServerHomeDir(String serverHomeDir) {

this.serverHomeDir = serverHomeDir;

}

}

 

 

 

 

你可能感兴趣的:(JAVA,FTP)