package com.vingsoft.util;
/**
* @author 做者:dujj
* @version 建立時間:2020年1月13日 下午5:53:39
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;java
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;apache
public class FtpApche {
private static FTPClient ftpClient = new FTPClient();
private static String encoding = System.getProperty("file.encoding");
/**
* Description: 從FTP服務器下載文件
*
* @Version1.0
* @param url
* FTP服務器hostname
* @param port
* FTP服務器端口
* @param username
* FTP登陸帳號
* @param password
* FTP登陸密碼
* @param remotePath
* FTP服務器上的相對路徑
* @param fileName
* 要下載的文件名
* @param localPath
* 下載後保存到本地的路徑
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean result = false;
try {
int reply;
ftpClient.setControlEncoding(encoding);
/*
* 爲了上傳和下載中文文件,有些地方建議使用如下兩句代替
* new String(remotePath.getBytes(encoding),"iso-8859-1")轉碼。
* 通過測試,通不過。
*/
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
ftpClient.connect(url, port);
// 若是採用默認端口,能夠使用ftp.connect(url)的方式直接鏈接FTP服務器
ftpClient.login(username, password);// 登陸
// 設置文件傳輸類型爲二進制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 獲取ftp登陸應答代碼
reply = ftpClient.getReplyCode();
// 驗證是否登錄成功
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
return result;
}
// 轉移到FTP服務器目錄至指定的目錄下
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
// 獲取文件列表
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), is);
is.close();
}
}
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* 將FTP服務器上文件下載到本地
* * 10.150.41.94
* * 21
* * dx
* *
*/
public void testDownFile(String resourceFolder,String fileName,String targetFolder) {
try {
String propertiesName="/resources/datasource.properties";
String dataServerIp=PropertiesUtil.getPropertiesValue(propertiesName, "wz.ftp.ip");
String dataServerUsername=PropertiesUtil.getPropertiesValue(propertiesName, "wz.ftp.user");
String dataServerPassword=PropertiesUtil.getPropertiesValue(propertiesName, "wz.ftp.pwd");
boolean flag = downFile(dataServerIp, 21, dataServerUsername,
dataServerPassword, resourceFolder, fileName, targetFolder);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FtpApche fa = new FtpApche();服務器
//FTP服務器的文件夾的路徑,文件名稱,要存放的服務器的路徑
fa.testDownFile("/task_attr_up","E_620700000000_0c24e4abba7144a3ae739dc78fd80b50.doc","E:/");
}
}測試