/**
* 创建于:2015年11月18日 下午3:48:31
* 所属项目:
* 文件名称:FtpUtil.java
* 作者:test
* 版权信息:
*/
package ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
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工具类(未测试)
*
* @history 2015年11月18日 下午5:37:30
* @author test
* @version 0.1.0
*/
public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
/**
* 连接到ftp服务器
* @param ip
* @param port
* @param userName
* @param userPwd
* @param path
*/
public void connectServer(String ip, int port, String userName, String userPwd, String path) {
FTPClient ftpClient = new FTPClient();
try {
// 连接
ftpClient.connect(ip, port);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
// 登录
ftpClient.login(userName, userPwd);
if (path != null && path.length() > 0) {
// 跳转到指定目录
ftpClient.changeWorkingDirectory(path);
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭ftp连接
* @param ftpClient
*/
public void disconnect(FTPClient ftpClient) {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取指定目录下的文件名
* @param path
* @param ftpClient
* @return
*/
public FTPFile[] listFiles(String path, FTPClient ftpClient) {
try {
return ftpClient.listFiles(path);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 下载文件
* @param directory 服务器文件路径
* @param downloadFile 服务器文件名称
* @param saveFile 本地保存的文件名称
* @param ftpClient FTPClient
*/
public void download(String directory, String downloadFile, String saveFile, FTPClient ftpClient){
try {
if(directory != null && !"".equals(directory)){
ftpClient.changeWorkingDirectory(directory);
}
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files = ftpClient.listFiles(downloadFile);
if(files.length != 1){
logger.error(downloadFile + "远程文件不唯一");
return;
}
File file = new File(saveFile);
long lRemoteSize = files[0].getSize();
if(file.exists()){
OutputStream out = new FileOutputStream(file);
logger.info("本地" + saveFile + "文件大小为:" + file.length());
if(file.length() >= lRemoteSize){
logger.info("本地文件大小大于远程文件大小,下载中止");
out.close();
return;
}
ftpClient.setRestartOffset(file.length());
ftpClient.retrieveFile(downloadFile, out);
out.close();
}else {
OutputStream out = new FileOutputStream(file);
ftpClient.retrieveFile(downloadFile, out);
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件
* @param uploadFile 本地上传文件名
* @param remote 服务器文件名
* @param ftpClient FTPClient
* @throws IOException
*/
public void upload(String uploadFile, String remote, FTPClient ftpClient) throws IOException{
//设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
//设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//对远程目录的处理
String remoteFileName = remote;
if(remote.contains("/")){
remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
if(!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(directory)){
//如果远程目录不存在,则递归创建远程服务器目录
mkDirectory(directory, ftpClient);
}
}
//检查远程是否存在文件
FTPFile[] files = ftpClient.listFiles(remoteFileName);
if(files.length == 1){
long remoteSize = files[0].getSize();
File f = new File(uploadFile);
long localSize = f.length();
if(remoteSize == localSize){
logger.info(uploadFile + "文件已存在");
return;
}else if(remoteSize > localSize){
logger.info(uploadFile + "远程文件大于本地文件大小,上传终止");
return;
}
InputStream is = null;
try{
//尝试移动文件内读取指针,实现断点续传
is = new FileInputStream(f);
if(is.skip(remoteSize) == remoteSize){
ftpClient.setRestartOffset(remoteSize);
if(ftpClient.storeFile(remote, is)){
logger.info(uploadFile + "文件上传成功");
return;
}
}
//如果断点续传没有成功,则删除服务器上文件,重新上传
if(!ftpClient.deleteFile(remoteFileName)){
logger.info(remoteFileName + "远程文件删除失败");
return;
}
if(ftpClient.storeFile(remote, is)){
logger.info(remoteFileName + "文件上传成功");
}else{
logger.info(remoteFileName + "文件上传失败");
}
}finally{
if(is != null){
is.close();
}
}
}else {
// 远程文件不存在,直接上传
InputStream is = new FileInputStream(uploadFile);
if(ftpClient.storeFile(remoteFileName, is)){
logger.info(remoteFileName + "文件上传成功");
}else{
logger.info(remoteFileName + "文件上传失败");
}
is.close();
}
}
/**
* 创建目录
* @param directory 路径名
* @param ftpClient
* @throws IOException
*/
private void mkDirectory(String directory, FTPClient ftpClient) throws IOException{
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/", start);
while(true){
String subDirectory = directory.substring(start,end);
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
logger.info("创建" + subDirectory + "目录失败");
return;
}
}
start = end + 1;
end = directory.indexOf("/", start);
//检查所有目录是否创建完毕
if(end <= start){
break;
}
}
}
}
java ftp工具类
最新推荐文章于 2024-04-28 16:14:09 发布