上传
/**
* FTP上传单个文件测试
*/
public static void uploadFtpFile(String hostname,String username,
String password,String uploadFilePath,String fileName,String ftpWorkPath)
throws RuntimeException{
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient.connect(hostname);
ftpClient.login(username, password);
File srcFile = new File(uploadFilePath+fileName);
fis = new FileInputStream(srcFile);
//设置上传目录
ftpClient.changeWorkingDirectory(“/”+ftpWorkPath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding(“GBK”);
//设置为被动模式
ftpClient.enterLocalPassiveMode();
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(“FTP客户端出错!”, e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(“关闭FTP连接发生异常!”, e);
}
logger.info(“已上传至FTP服务器路径!”);
}
}
下载
/**
* 获取FTPClient对象
*
* @param ftpHost
* FTP主机服务器
* @param ftpPassword
* FTP 登录密码
* @param ftpUserName
* FTP登录用户名
* @param ftpPort
* FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) throws RuntimeException{
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.info(“未连接到FTP,用户名或密码错误。”);
ftpClient.disconnect();
} else {
logger.info(“FTP连接成功。”);
}
} catch (SocketException e) {
e.printStackTrace();
logger.info(“FTP的IP地址可能错误,请正确配置。”);
throw new RuntimeException(“FTP的IP地址可能错误,请正确配置!”, e);
} catch (IOException e) {
e.printStackTrace();
logger.info(“FTP的端口错误,请正确配置。”);
throw new RuntimeException(“FTP的端口错误,请正确配置!”, e);
}
return ftpClient;
}
/**
* 从FTP服务器下载文件
* @param ftpHost FTP IP地址
* @param ftpUserName FTP 用户名
* @param ftpPassword FTP用户名密码
* @param ftpPort FTP端口
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param localPath 下载到本地的位置 格式:H:/download
* @param fileName 文件名称
*/
public static boolean downloadFtpFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) throws RuntimeException{
boolean flag = false;
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftpClient.setControlEncoding(“UTF-8”); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
flag = true;
} catch (FileNotFoundException e) {
logger.error(“没有找到” + ftpPath + “文件”);
e.printStackTrace();
throw new RuntimeException(“没有找到” + ftpPath + “文件:”, e);
} catch (SocketException e) {
logger.error(“连接FTP失败.”);
e.printStackTrace();
throw new RuntimeException(“连接FTP失败:”, e);
} catch (IOException e) {
e.printStackTrace();
logger.error(“文件读取错误。”);
e.printStackTrace();
throw new RuntimeException(“文件读取错误:”, e);
}
return flag;
}
测试代码
public static void main(String[] args) {
//下载
// FavFTPUtil.downloadFtpFile(“192.168.2.133″,”adks”,”adconcepts2017″,
// “fileConllect/test”,”E:\\xcc”, “53840afe-0682-4960-84ef-3f3b972a0f12.zip”);
FavFTPUtil.uploadFtpFile(“192.168.2.133″,”test”,”123″,
“E:\\360\\pic\\”, “a.txt”,
“fengWu/”);
}