|
edtFTPj是一个非常强大的FTP组件,有Java版本、.NET版本、JavaScript版本。
Java版本的有收费的edtFTPj/PRO,还有免费开源的edtFTPj/Free。
这里使用edtFTPj/Free。
edtFTPj/Free提供了一套稳定、功能强大又易于使用的类库,这让使用FTP协议传输文件变得非常简单。
edtFTPj/Free有以下功能:
1)通过FTP协议快速稳定的文件传输
2)具有主动(Active)模式和被动(Passive)模式,支持二进制(Binary)传输和ASCII传输
3)支持中断后恢复传输
4)支持以FTP流的方式灵活地从FTP服务器读取和写入
edtFTPj/Free的下载地址 http://www.enterprisedt.com/products/edtftpj/download.html
下载解压后把edtftpj.jar添加到classpath路径下。
1、写一个FTP工具类,获取FTP客户端。
package cn.luxh.utils;
import java.io.IOException;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FileTransferClient;
/**
* FTP Util
* @author Luxh
*/
public class FtpUtil {
/**
*
* @param host FTP服务器地址
* @param userName FTP账号
* @param password FTP密码
* @return
* @throws FTPException
* @throws IOException
*/
public static FileTransferClient getFileTransferClient(String host,String userName,String password) throws FTPException, IOException {
//创建一个FTP client
//官方已不推荐使用FTPClient
FileTransferClient ftp = new FileTransferClient();
//设置FTP服务器
ftp.setRemoteHost(host);
//设置FTP用户名
ftp.setUserName(userName);
//设置FTP密码
ftp.setPassword(password);
//解决中文文件名乱码
ftp.getAdvancedSettings().setControlEncoding("GBK");
//被动模式,数据连接由客户端发起
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
//用HTML 和文本编写的文件必须用ASCII模式上传,用BINARY模式上传会破坏文件,导致文件执行出错.
//ftp.setContentType(FTPTransferType.ASCII);
//BINARY模式用来传送可执行文件,压缩文件,和图片文件.
ftp.setContentType(FTPTransferType.BINARY);
//连接到FTP服务器
ftp.connect();
return ftp;
}
/**
* 从FTP Server断开连接
* @param ftp
* @throws FTPException
* @throws IOException
*/
public static void disconnectFromFTPServer(FileTransferClient ftp) throws FTPException, IOException {
if(ftp != null && ftp.isConnected()) {
ftp.disconnect();
}
}
}
2、简单测试一下上传文件、下载文件、删除文件。
package cn.luxh.test;
import java.io.IOException;
import org.junit.Test;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FileTransferClient;
import cn.luxh.utils.FtpUtil;
public class FtpUtilTest {
/**FTP服务器*/
private static final String FTP_HOST = "127.0.0.1";
/**FTP账号*/
private static final String FTP_USERNAME = "root";
/**FTP密码*/
private static final String FTP_PASSWORD = "root";
/**
* 上传文件
* @throws IOException
* @throws FTPException
*/
@Test
public void testUploadFile() throws FTPException, IOException {
FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);
String localFileName = "d:/test/中文.txt";
String remoteFileName = "/test/中文.txt";
ftp.uploadFile(localFileName, remoteFileName);
FtpUtil.disconnectFromFTPServer(ftp);
}
/**
* 下载文件
* @throws FTPException
* @throws IOException
*/
@Test
public void testDownloadFile() throws FTPException, IOException {
FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);
ftp.downloadFile("D:/test/中文.txt", "/test/中文.txt");
FtpUtil.disconnectFromFTPServer(ftp);
}
/**
* 删除文件
* @throws FTPException
* @throws IOException
*/
@Test
public void testDeleteFile() throws FTPException, IOException {
FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);
ftp.deleteFile("/test/中文.txt");
FtpUtil.disconnectFromFTPServer(ftp);
}
}
|
|
|