没有写摘要的习惯,就直接在这里写了。
首先搭建一个文件服务器,参考http://my.oschina.net/simpleton/blog/530081
然后就是客户端工具类代码(2015-11-16 11:00:16更新,下午才测试这段代码,暂时贴上来)
这里说明一下,客户端代码是写的一个核心代码,然后根据自己的需要,对其进行不同的包装。
为了让核心代码更高效和更具扩展、易用,核心代码可能会经常重构,到时候我会贴上最新的代码并保留原来的。
/**
*
*/
package com.common.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
/**
* FTP客户端工具类
* @author luolin
*
* @version $id:FTPClientUtils.java,v 0.1 2015年11月13日 下午4:18:07 luolin Exp $
*/
public class FTPClientUtil {
private static final Logger LOGGER = Logger.getLogger(FTPClientUtil.class);
/**
* 连接文件服务器
* @param addr 文件服务器地址
* @param port 端口
* @param username 用户名
* @param password 密码
* @throws Exception
*/
public static FTPClient connect(String addr, int port, String username, String password) {
LOGGER.info("【连接文件服务器】addr = " + addr + " , port : " + port + " , username = " + username + " , password = "
+ password);
FTPClient ftpClient = new FTPClient();
try {
// 连接
ftpClient.connect(addr, port);
// 登录
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (Exception e) {
LOGGER.error("【连接文件服务器失败】", e);
throw new RuntimeException("连接文件服务器失败");
}
// 判断文件服务器是否可用??
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
closeConnection(ftpClient);
}
return ftpClient;
}
/**
* 连接文件服务器
* @param addr 文件服务器地址
* @param port 端口
* @param username 用户名
* @param password 密码
* @param workingDirectory 目标连接工作目录
* @throws Exception
*/
public static FTPClient connect(String addr, int port, String username, String password, String workingDirectory)
throws Exception {
FTPClient ftpClient = connect(workingDirectory, port, workingDirectory, workingDirectory);
changeWorkingDirectory(workingDirectory, ftpClient);
return ftpClient;
}
/**
* 关闭连接,使用完连接之后,一定要关闭连接,否则服务器会抛出 Connection reset by peer的错误
* @throws IOException
*/
public static void closeConnection(FTPClient ftpClient) {
LOGGER.info("【关闭文件服务器连接】");
if (ftpClient == null) {
return;
}
try {
ftpClient.disconnect();
} catch (IOException e) {
LOGGER.error("【关闭连接失败】", e);
throw new RuntimeException("关闭连接失败");
}
}
/**
* 切换工作目录
* @param directory 目标工作目录
* @param ftpClient
* @throws IOException
*/
public static void changeWorkingDirectory(String directory, FTPClient ftpClient) {
LOGGER.info("【切换工作目录】directory : " + directory);
// 切换到目标工作目录
try {
if (!ftpClient.changeWorkingDirectory(directory)) {
ftpClient.makeDirectory(directory);
ftpClient.changeWorkingDirectory(directory);
}
} catch (IOException e) {
LOGGER.error("【切换工作目录失败】", e);
throw new RuntimeException("切换工作目录失败");
}
}
/**
* 上传文件/文件夹
* @param file 上传的文件或文件夹
* @return 文件存放的路径以及文件名
* @throws Exception
*/
public static void upload(File file, FTPClient ftpClient) throws Exception {
if (file == null) {
LOGGER.warn("【存储的文件为空】");
throw new RuntimeException("上传文件为空");
}
LOGGER.info("【上传文件/文件夹】file : " + file.getName());
// 是文件,直接上传
if (!file.isDirectory()) {
storeFile(new File(file.getPath()), ftpClient);
return;
}
changeWorkingDirectory(file.getName(), ftpClient);
// 文件夹,递归上传所有文件
for (File item : file.listFiles()) {
if (!item.isDirectory()) {
storeFile(item, ftpClient);
continue;
}
upload(item, ftpClient);
ftpClient.changeToParentDirectory();
}
}
/**
* 删除文件
* @param fileName 要删除的文件地址
* @return true/false
* @throws IOException
*/
public static boolean delete(String fileName, FTPClient ftpClient) throws IOException {
LOGGER.info("【删除文件】fileName : " + fileName);
return ftpClient.deleteFile(fileName);
}
/**
* 存储文件
* @param file {@link File}
* @throws Exception
*/
public static void storeFile(File file, FTPClient ftpClient) throws Exception {
if (file == null) {
LOGGER.warn("【存储的文件为空】");
throw new RuntimeException("存储的文件为空");
}
LOGGER.info("【存储文件】file : " + file.getName());
FileInputStream input = new FileInputStream(file);
ftpClient.storeFile(file.getName(), input);
input.close();
}
/**
* 下载文件到指定目录
* @param ftpFile 文件服务器上的文件地址
* @param dstFile 输出文件的路径和名称
* @throws Exception
*/
public static void downLoad(String ftpFile, String dstFile, FTPClient ftpClient) throws Exception {
LOGGER.info("【下载文件到指定目录】ftpFile = " + ftpFile + " , dstFile = " + dstFile);
if (StringUtils.isBlank(ftpFile)) {
LOGGER.warn("【参数ftpFile为空】");
throw new RuntimeException("【参数ftpFile为空】");
}
if (StringUtils.isBlank(dstFile)) {
LOGGER.warn("【参数dstFile为空】");
throw new RuntimeException("【参数dstFile为