java操作ftp下載文件示例
/**
*
* JAVA操作 FTP 下載
* 文件下載。
*
*/
private void ftpDownload()
{
FTPClient ftpClient = null;
InputStream input = null;
boolean loginFlag = false;
List<String> list = new ArrayList<String>();
try
{
int defaultPort = CommonsMessage.FTP_DEFAULT_PORT; // 21
int timeout = M400Constant.NUM_SIXTY_THOUSAND * M400Constant.NUM_TWO; // 120000
ftpClient = new FTPClient();
ftpClient.setDefaultPort(defaultPort);
ftpClient.setConnectTimeout(timeout);
// ftpClient.setSoTimeout(timeout);
ftpClient.setDefaultTimeout(timeout);
ftpClient.setDataTimeout(timeout);
ftpClient.setControlEncoding("UTF-8");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("en");
conf.setDefaultDateFormatStr("MMM dd HH:mm");
ftpClient.configure(conf);
ftpClient.connect(SystemConfig.getKey("scpFtpIp"));
loginFlag = ftpClient.login(SystemConfig.getKey("scpFtpUserName").trim(),
SystemConfig.getKey("scpFtpPassword").trim());
String ftpDir = SystemConfig.getKey("ftpHangupDir").trim();
if (!loginFlag)
{
DEBUGGER.debug("login scp ftp is :" + loginFlag);
}
// String ftpDir = SystemConfig.getKey("ftpHangupDir").trim();
if (ftpClient.changeWorkingDirectory(ftpDir))
{
FTPFile file[] = ftpClient.listFiles(ftpDir);
if (file != null && file.length > 0)
{
DEBUGGER.debug("hungup file size is: " + file.length);
for (int i = 0; i < file.length; i++)
{
String fileName = file[i].getName();
ftpClient.setBufferSize(CommonsMessage.ONE_K_BUFFER_SIZE);
// 設(shè)置文件類型(二進(jìn)制)
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
input = ftpClient.retrieveFileStream(fileName);
processInput(ftpClient, input, list, fileName);
// if (downFlag) {
// ftpClient.deleteFile(fileName);
// }
}
}
else
{
try
{
Thread.sleep(M400Constant.NUM_FIVE_THOUSAND);
}
catch (InterruptedException e)
{
/*
* if (DEBUGGER.isDebugEnable()) {
* DEBUGGER.debug("Failed to queryScpToQuery", e); }
*/
error("Failed to queryScpToQuery", e);
}
}
}
else
{
DEBUGGER.debug("scp dir is error ");
}
}
catch (Exception e)
{
/*
* DEBUGGER.debug("dowonload file is faild ", e);
*/
error("dowonload file is faild ", e);
}
finally
{
close(input, "close inputStream is faild ");
if (ftpClient != null)
{
try
{
deleteFile(ftpClient, list);
boolean logoutFlag = false;
try
{
logoutFlag = ftpClient.logout();
// DEBUGGER.debug("logout : " + logoutFlag);
}
catch (Exception e)
{
/*
* DEBUGGER.debug("logout : " + logoutFlag);
*/
error("logout : " + logoutFlag, e);
}
ftpClient = null;
}
catch (Exception e)
{
/*
* if (DEBUGGER.isDebugEnable()) {
* DEBUGGER.error("Failed to ftpDownload", e); }
*/
error("Failed to ftpDownload", e);
}
// try {
// ftpClient.disconnect();
// } catch (IOException e) {
// if (DEBUGGER.isDebugEnable()) {
// DEBUGGER.debug("Failed to ftpDownload", e);
// }
// }
}
}
}
/**
* 處理ftp對應(yīng)文件輸入流。
*
* @param ftpClient
* ftp客戶端對象
* @param input
* ftp文件輸入流
* @param list
* 處理完畢的文件列表,用于執(zhí)行文件刪除使用
* @param fileName
* 當(dāng)前處理的文件名稱
*/
private void processInput(FTPClient ftpClient, InputStream input, List<String> list, String fileName)
{
if (input == null)
{
return;
}
// boolean downFlag = callingService(input);
callingService(input);
try
{
input.close();
ftpClient.completePendingCommand();
list.add(fileName);
}
catch (Exception e)
{
/*
* DEBUGGER.debug("Failed to ftpDownload", e);
*/
error("Failed to ftpDownload", e);
}
}
}
相關(guān)文章
springboot項目配置context path失效的問題解決
本文主要介紹了springboot項目配置context path失效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解
這篇文章主要介紹了Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解,跨域,指的是瀏覽器不能執(zhí)行其他網(wǎng)站的腳本,它是由瀏覽器的同源策略造成的,是瀏覽器施加的安全限制,所謂同源是指,域名,協(xié)議,端口均相同,需要的朋友可以參考下2023-12-12Eclipse、MyEclipse 導(dǎo)入svn項目具體步驟
這篇文章主要介紹了Eclipse、MyEclipse 導(dǎo)入svn項目具體步驟的相關(guān)資料,需要的朋友可以參考下2016-10-10Java+opencv3.2.0實現(xiàn)hough圓檢測功能
這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實現(xiàn)hough圓檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02Java并發(fā)編程volatile關(guān)鍵字的作用
這篇文章主要介紹了Java并發(fā)編程volatile關(guān)鍵字的作用,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07Java請求流量合并和拆分提高系統(tǒng)的并發(fā)量示例
這篇文章主要為大家介紹了Java請求流量合并和拆分提高系統(tǒng)的并發(fā)量示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04SpringBoot項目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡化 Java 程序開發(fā),而 SpringBoot 的出現(xiàn)是為了簡化 Spring 程序開發(fā),這篇文章主要介紹了SpringBoot項目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02