最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

sftp和ftp 根據(jù)配置遠程服務器地址下載文件到當前服務

 更新時間:2016年10月26日 14:26:09   作者:zhangqinfu  
這篇文章主要介紹了sftp和ftp 根據(jù)配置遠程服務器地址下載文件到當前服務的相關(guān)資料本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下

廢話不多說,關(guān)鍵代碼如下所示:

 

package com.eastrobot.remote; 
import java.util.List; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import com.eastrobot.util.PropertiesUtil; 
/** 
* full.zhang 
* 
* ftp/sftp抽象方法類 
* 
*/ 
public abstract class FileRemote { 
private static final String FTP_MODE = "ftp"; 
private static final String SFTP_MODE = "sftp"; 
public static String ftproot; 
public static String mode; 
public static String host; 
public static String username; 
public static String password; 
public static String port; 
private static FileRemote client = null; 
// 最大一次性下載50個文件 
public static int max = 50; 
private final static Log LOGGER = LogFactory.getLog(FileRemote.class); 
public static FileRemote getInstance() { 
if (client == null) { 
ftproot = PropertiesUtil.getString("transfer.root"); 
mode = PropertiesUtil.getString("transfer.mode"); 
host = PropertiesUtil.getString("transfer.host"); 
username = PropertiesUtil.getString("transfer.username"); 
password = PropertiesUtil.getString("transfer.password"); 
port = PropertiesUtil.getString("transfer.port"); 
if (mode.equals(FTP_MODE)) { 
client = new FileFtpRemote(); 
} else if (mode.equals(SFTP_MODE)) { 
client = new FileSftpRemote(); 
} 
} 
return client; 
} 
/** 
* 執(zhí)行定時任務 
*/ 
public void process() { 
LOGGER.debug("----------------------------------------進入定時下載遠程文件"); 
// 創(chuàng)建線程池 
ExecutorService exec = Executors.newSingleThreadExecutor(); 
exec.execute(new Runnable() { 
@Override 
public void run() { 
// 建立連接 
initFtpInfo(host, port, username, password); 
// 遠程服務所有源文件路徑集合 
List<String> listSourcePath = listRemoteFilePath(ftproot); 
if (listSourcePath.isEmpty()) { 
LOGGER.debug("____________________釋放連接"); 
client.closeConnection(); 
return; 
} 
if (listSourcePath.size() > max) { 
listSourcePath = listSourcePath.subList(0, max); 
} 
for (String path : listSourcePath) { 
downloadRemoteFile(path); 
} 
LOGGER.debug("____________________釋放連接"); 
client.closeConnection(); 
} 
}); 
exec.shutdown(); 
} 
/** 
* 初始化連接 
* 
* @param host 
* @param port 
* @param username 
* @param password 
* @throws Exception 
* @return 
*/ 
public abstract void initFtpInfo(String host, String port, String username, String password); 
/** 
* 下載遠程服務下文件到本地服務 
* 
* @param path 
* @return 
* @throws Exception 
*/ 
public abstract void downloadRemoteFile(String filePath); 
/** 
* 獲取遠程服務下指定目錄下的所有文件路徑集合(包含子目錄下文件) 
* 
* @param path 
* @return 
*/ 
public abstract List<String> listRemoteFilePath(String path); 
/** 
* 釋放連接 
*/ 
public abstract void closeConnection(); 
} 
[java] view plain copy
package com.eastrobot.remote; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringUtils; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.FTPReply; 
import com.eastrobot.command.Commander; 
public class FileFtpRemote extends FileRemote { 
protected FTPClient ftpClient; 
private String encoding = "UTF-8"; 
private boolean binaryTransfer = true; 
private final static Log LOGGER = LogFactory.getLog(FileFtpRemote.class); 
@Override 
public void initFtpInfo(String host, String port, String username, String password) { 
try { 
// 構(gòu)造一個FtpClient實例 
ftpClient = new FTPClient(); 
// 設置字符集 
ftpClient.setControlEncoding(encoding); 
// 連接FTP服務器 
ftpClient.connect(host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 21); 
// 連接后檢測返回碼來校驗連接是否成功 
int reply = ftpClient.getReplyCode(); 
if (FTPReply.isPositiveCompletion(reply)) { 
// 登陸到ftp服務器 
if (ftpClient.login(username, password)) { 
setFileType(); 
} 
ftpClient.login(username, password); 
} else { 
ftpClient.disconnect(); 
LOGGER.error("ftp服務拒絕連接!"); 
} 
} catch (Exception e) { 
if (ftpClient.isConnected()) { 
try { 
ftpClient.disconnect(); // 斷開連接 
} catch (IOException e1) { 
LOGGER.error("ftp服務連接斷開失敗!"); 
} 
} 
LOGGER.error("ftp服務連接失敗!"); 
} 
} 
/** 
* 設置文件傳輸類型 
*/ 
private void setFileType() { 
try { 
if (binaryTransfer) { 
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
} else { 
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE); 
} 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
@Override 
public void downloadRemoteFile(String filePath) { 
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) { 
filePath = filePath.substring(0, filePath.length() - 1); 
} 
File saveFile = new File(filePath); 
if (saveFile.exists()) { 
return; 
} 
// 文件所在目錄 
String path = filePath.substring(0, filePath.lastIndexOf("/")); 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
OutputStream output = null; 
try { 
// 創(chuàng)建目標文件路徑 
if (!saveFile.getParentFile().exists()) { 
saveFile.getParentFile().mkdirs(); 
} 
saveFile.createNewFile(); 
// 轉(zhuǎn)移到FTP服務器目錄 
ftpClient.changeWorkingDirectory(path); 
output = new FileOutputStream(saveFile); 
ftpClient.retrieveFile(filePath, output); 
} catch (IOException e) { 
LOGGER.debug("文件:" + filePath + "______________________下載失敗!"); 
e.printStackTrace(); 
} finally { 
LOGGER.debug("文件:" + filePath + "______________________下載成功!"); 
IOUtils.closeQuietly(output); 
} 
} 
@Override 
public List<String> listRemoteFilePath(String path) { 
List<String> list = new ArrayList<String>(); 
try { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
boolean changedir = ftpClient.changeWorkingDirectory(path); 
if (changedir) { 
ftpClient.setControlEncoding(encoding); 
FTPFile[] files = ftpClient.listFiles(); 
for (FTPFile file : files) { 
if (list.size() >= max) { 
break; 
} 
if (file.isDirectory()) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
list.addAll(this.listRemoteFilePath(path + file.getName())); 
} else if (changedir) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
File saveFile = new File(path + file.getName()); 
if (!saveFile.exists()) { 
list.add(path + file.getName()); 
} 
} 
} 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return list; 
} 
@Override 
public void closeConnection() { 
if (ftpClient != null) { 
try { 
ftpClient.logout(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
if (ftpClient.isConnected()) { 
try { 
ftpClient.disconnect(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 
} 
} 
} 
[java] view plain copy
package com.eastrobot.remote; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Properties; 
import java.util.Vector; 
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringUtils; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import com.eastrobot.command.Commander; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.SftpATTRS; 
import com.jcraft.jsch.SftpException; 
import com.jcraft.jsch.ChannelSftp.LsEntry; 
public class FileSftpRemote extends FileRemote { 
protected Session session = null; 
protected ChannelSftp channel = null; 
private final static Log LOGGER = LogFactory.getLog(FileSftpRemote.class); 
@Override 
public void initFtpInfo(String host, String port, String username, String password) { 
try { 
JSch jsch = new JSch(); // 創(chuàng)建JSch對象 
session = jsch.getSession(username, host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 22); 
session.setPassword(password); // 設置密碼 
Properties config = new Properties(); 
config.put("StrictHostKeyChecking", "no"); 
session.setConfig(config); // 為Session對象設置properties 
session.setTimeout(60000); // 設置timeout時間 
session.connect(); // 通過Session建立鏈接 
Channel chan = session.openChannel("sftp"); // 打開SFTP通道 
chan.connect(); // 建立SFTP通道的連接 
channel = (ChannelSftp) chan; 
} catch (Exception e) { 
LOGGER.error("sftp連接失敗"); 
e.printStackTrace(); 
} 
} 
@Override 
public void downloadRemoteFile(String filePath) { 
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) { 
filePath = filePath.substring(0, filePath.length() - 1); 
} 
File saveFile = new File(filePath); 
FileOutputStream output = null; 
try { 
if (saveFile.exists()) { 
return; 
} 
// 創(chuàng)建目標文件路徑 
if (!saveFile.getParentFile().exists()) { 
saveFile.getParentFile().mkdirs(); 
} 
saveFile.createNewFile(); 
// 文件所在目錄 
String path = filePath.substring(0, filePath.lastIndexOf("/")); 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
channel.cd(path); 
channel.get(filePath, new FileOutputStream(saveFile)); 
LOGGER.debug("文件:" + filePath + "____________________________________________下載成功!"); 
} catch (Exception e) { 
LOGGER.debug("文件:" + filePath + "____________________________________________下載失敗!"); 
e.printStackTrace(); 
} finally { 
IOUtils.closeQuietly(output); 
} 
} 
@SuppressWarnings("unchecked") 
@Override 
public List<String> listRemoteFilePath(String path) { 
List<String> list = new ArrayList<String>(); 
Vector<LsEntry> v = null; 
try { 
if (!StringUtils.endsWith(path, "/") && StringUtils.endsWith(path, File.separator)) { 
path = path + File.separator; 
} 
v = channel.ls(path); 
} catch (SftpException e) { 
e.printStackTrace(); 
} 
for (LsEntry lsEntry : v) { 
if (list.size() >= max) { 
break; 
} 
if (!".".equals(lsEntry.getFilename()) && !"..".equals(lsEntry.getFilename())) { 
SftpATTRS attrs = lsEntry.getAttrs(); 
if (attrs.isDir()) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
list.addAll(this.listRemoteFilePath(path + lsEntry.getFilename())); 
} else { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
File saveFile = new File(path + lsEntry.getFilename()); 
if (!saveFile.exists()) { 
list.add(path + lsEntry.getFilename()); 
} 
} 
} 
} 
return list; 
} 
@Override 
public void closeConnection() { 
try { 
if (channel != null) { 
channel.quit(); 
channel.disconnect(); 
} 
if (session != null) { 
session.disconnect(); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
public Session getSession() { 
return session; 
} 
public void setSession(Session session) { 
this.session = session; 
} 
public ChannelSftp getChannel() { 
return channel; 
} 
public void setChannel(ChannelSftp channel) { 
this.channel = channel; 
} 
}

以上所述是小編給大家介紹的sftp和ftp 根據(jù)配置遠程服務器地址下載文件到當前服務,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringBoot配置連接兩個或多個數(shù)據(jù)庫的實現(xiàn)

    SpringBoot配置連接兩個或多個數(shù)據(jù)庫的實現(xiàn)

    本文主要介紹了SpringBoot配置連接兩個或多個數(shù)據(jù)庫的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • Java基礎知識總結(jié)之繼承

    Java基礎知識總結(jié)之繼承

    這一篇我們來學習面向?qū)ο蟮牡诙€特征——繼承,文中有非常詳細的基礎知識總結(jié),對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • log4j中l(wèi)ogger標簽中additivity屬性的用法說明

    log4j中l(wèi)ogger標簽中additivity屬性的用法說明

    這篇文章主要介紹了log4j中l(wèi)ogger標簽中additivity屬性的用法說明,基于很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java顯示聲音波形圖示例

    java顯示聲音波形圖示例

    這篇文章主要介紹了java顯示聲音波形圖示例,需要的朋友可以參考下
    2014-05-05
  • 基于Bigdecimal科學計數(shù)問題

    基于Bigdecimal科學計數(shù)問題

    這篇文章主要介紹了基于Bigdecimal科學計數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot ResponseBody返回值處理的實現(xiàn)

    SpringBoot ResponseBody返回值處理的實現(xiàn)

    這篇文章主要介紹了SpringBoot ResponseBody返回值處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Spring示例講解條件注入方法

    Spring示例講解條件注入方法

    Spring支持按照條件來注入某些特定的bean,這也是Spring Boot實現(xiàn)自動化配置的底層方法,文中的示例代碼講解詳細,需要的可以參考一下
    2022-06-06
  • Spring Mybatis 基本使用過程(推薦)

    Spring Mybatis 基本使用過程(推薦)

    Mybatis是一個半自動ORM(Object Relational Mapping)框架,它可以簡化數(shù)據(jù)庫編程,讓開發(fā)者更專注于SQL本身,本文給大家介紹Spring Mybatis 基本使用過程,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • 詳解Java HashMap實現(xiàn)原理

    詳解Java HashMap實現(xiàn)原理

    HashMap是基于哈希表的Map接口實現(xiàn),提供了所有可選的映射操作,并允許使用null值和null建,不同步且不保證映射順序。本文將記錄一下研究HashMap實現(xiàn)原理。
    2017-01-01
  • 教你如何正確了解java三大特性!!!!

    教你如何正確了解java三大特性!!!!

    所有的面向?qū)ο缶幊陶Z言的思路都是差不多的,而這三大特性,則是思路中的支柱點,接下來我就重點講解了一下java三大特性,感興趣的朋友跟隨腳本之家小編一起看看吧
    2021-07-07

最新評論

顺义区| 班玛县| 孟连| 郸城县| 博乐市| 阿拉善右旗| 盐边县| 扶风县| 灯塔市| 唐河县| 当阳市| 通城县| 厦门市| 漳浦县| 南靖县| 广丰县| 泾源县| 永州市| 东乌珠穆沁旗| 靖安县| 大邑县| 南汇区| 新竹市| 吴堡县| 金川县| 庄河市| 雅安市| 淳安县| 玛纳斯县| 荃湾区| 平果县| 穆棱市| 桂平市| 霸州市| 临邑县| 额敏县| 克什克腾旗| 从江县| 黄山市| 贵南县| 蛟河市|