废话不多说, 关键代码如下所示:
แพ็คเกจ com.eastrobot.remote; นำเข้า java.util.list; นำเข้า java.util.concurrent.executorservice; นำเข้า java.util.concurrent.executors; นำเข้า org.apache.commons.logging.log; นำเข้า org.apache.commons.logging.logfactory; นำเข้า com.eastrobot.util.propertiesutil; /** * full.zhang * * ftp/sftp 抽象方法类 * */บทคัดย่อระดับสาธารณะ Fileremote {สตริงสุดท้ายคงที่ private ftp_mode = "ftp"; สตริงสุดท้ายคงที่ส่วนตัว sftp_mode = "sftp"; สตริงคงที่สาธารณะ ftproot; โหมดสตริงคงที่สาธารณะ โฮสต์สตริงสาธารณะคงที่; ชื่อผู้ใช้สตริงสาธารณะ รหัสผ่านสตริงคงที่สาธารณะ พอร์ตสตริงคงที่สาธารณะ ไคลเอนต์ fileremote แบบคงที่ส่วนตัว = null; // 最大一次性下载 50 个文件 int public Static int สูงสุด = 50; logger logs คงสุดท้ายสุดท้าย = logfactory.getLog (fileremote.class); public Static fileremote getInstance () {if (client == null) {ftproot = propertiesUtil.getString ("transfer.root"); โหมด = propertiesutil.getString ("transfer.mode"); host = propertiesutil.getString ("transfer.host"); ชื่อผู้ใช้ = ProperTiesUtil.getString ("transfer.username"); รหัสผ่าน = propertiesutil.getString ("transfer.password"); พอร์ต = propertiesutil.getString ("transfer.port"); if (mode.equals (ftp_mode)) {client = new fileftpremote (); } else if (mode.equals (sftp_mode)) {client = ใหม่ filesftpremote (); }} ส่งคืนไคลเอนต์; } / ** * 执行定时任务 * / กระบวนการโมฆะสาธารณะ () {logger.debug ("---------------------------------------- 进入定时下载远程文件"); // 创建线程池 ExecutorService exec = executors.newsingLethreadexecutor (); exec.execute (ใหม่ runnable () {@Override public void run () {// 建立连接 initftpinfo (โฮสต์, พอร์ต, ชื่อผู้ใช้, รหัสผ่าน); // 远程服务所有源文件路径集合 list <string> listsourcepath = listremotefilepath (ftproot); ถ้า client.loseconnection (); - Exec.Shutdown (); } / ** * 初始化连接 * * @param host * @param พอร์ต * @param username * @param password * @throws Exception * @return * / บทคัดย่อสาธารณะเป็นโมฆะ initftpinfo (สตริงโฮสต์สตริงพอร์ตสตริงชื่อผู้ใช้สตริง); / ** * 下载远程服务下文件到本地服务 * * @param Path * @return * @throws Exception */ Public Abstract Void DownloadRemoteFile (String FilePath); / ** * 获取远程服务下指定目录下的所有文件路径集合 (包含子目录下文件) * * @param Path * @return */ รายการบทคัดย่อสาธารณะ <String> ListreMoteFilePath (เส้นทางสตริง); / ** * 释放连接 */ บทคัดย่อสาธารณะเป็นโมฆะ closeconnection (); } [java] ดู plain copypackage com.eastrobot.remote; นำเข้า Java.io.File; นำเข้า Java.io.FileOutputStream; นำเข้า java.io.ioException; นำเข้า Java.io.OutputStream; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า org.apache.commons.io.ioutils; นำเข้า org.apache.commons.lang.stringutils; นำเข้า org.apache.commons.logging.log; นำเข้า org.apache.commons.logging.logfactory; นำเข้า org.apache.commons.net.ftp.ftpclient; นำเข้า org.apache.commons.net.ftp.ftpfile; นำเข้า org.apache.commons.net.ftp.ftpreply; นำเข้า com.eastrobot.command.commander; Public Class FileftPremote ขยาย fileremote {ป้องกัน ftpClient ftpClient; การเข้ารหัสสตริงส่วนตัว = "UTF-8"; บูลีนส่วนตัว binaryTransfer = true; logger logs คงสุดท้ายสุดท้าย = logfactory.getLog (fileftpremote.class); @Override โมฆะสาธารณะ initftpinfo (โฮสต์สตริง, พอร์ตสตริง, ชื่อผู้ใช้สตริง, รหัสผ่านสตริง) {ลอง {// 构造一个 ftpClient 实例 ftpClient = new ftpClient (); // 设置字符集 ftpclient.setControlencoding (การเข้ารหัส); // 连接 ftp 服务器 ftpclient.connect (โฮสต์, stringutils.isnotblank (พอร์ต)? integer.valueof (พอร์ต): 21); // 连接后检测返回码来校验连接是否成功 int ตอบ = ftpClient.getReplyCode (); if (ftpreply.ispositiveCompletion (ตอบกลับ)) {// 登陆到 ftp 服务器ถ้า (ftpclient.login (ชื่อผู้ใช้, รหัสผ่าน)) {setFileType (); } ftpClient.login (ชื่อผู้ใช้, รหัสผ่าน); } else {ftpClient.disconnect (); logger.error ("ftp 服务拒绝连接!"); }} catch (exception e) {if (ftpClient.isconnected ()) {ลอง {ftpClient.disconnect (); // 断开连接} catch (ioexception e1) {logger.error ("ftp 服务连接断开失败!"); }} logger.error ("ftp 服务连接失败!"); }} / ** * 设置文件传输类型 * / โมฆะส่วนตัว setFileType () {ลอง {ถ้า (binaryTransfer) {ftpClient.setFileType (ftpClient.binary_file_type); } else {ftpClient.setFileType (ftpClient.ascii_file_type); }} catch (ioexception e) {e.printstacktrace (); }} @Override โมฆะสาธารณะดาวน์โหลด RemoteFile (String filePath) {ถ้า (StringUtils.endswith (filepath, "/") || stringutils.endswith (filepath, file.Sparator)) {filePath = filePath.SubString } ไฟล์ saveFile = ไฟล์ใหม่ (filePath); if (savefile.exists ()) {return; } // 文件所在目录เส้นทางสตริง = filepath.substring (0, filepath.lastindexof ("/")); if (! stringUtils.endswith (path, "/") &&! stringUtils.endswith (path, file.separator)) {ถ้า (commander.islinux) {path = path + file.Saperator; } else {path = path + "/"; }} outputStream output = null; ลอง {// 创建目标文件路径 if (! savefile.getParentFile (). มีอยู่ ()) {savefile.getParentFile (). mkdirs (); } saveFile.CreatenewFile (); // 转移到 ftp 服务器目录 ftpclient.changeworkingdirectory (เส้นทาง); output = ใหม่ fileOutputStream (saveFile); ftpclient.retrieveFile (filepath, เอาต์พุต); } catch (ioexception e) {logger.debug ("文件:" + filepath + "______________________ 下载失败!"); E.PrintStackTrace (); } ในที่สุด {logger.debug ("文件:" + filepath + "______________________ 下载成功!"); ioutils.closequietly (เอาท์พุท); }} @Override รายการสาธารณะ <String> ListreMoteFilePath (เส้นทางสตริง) {รายการ <String> list = new ArrayList <String> (); ลอง {if (! stringutils.endswith (path, "/") &&! stringutils.endswith (path, file.separator)) {ถ้า (commander.islinux) {path = path + file.separator; } else {path = path + "/"; }} boolean changeir = ftpClient.changeWorkingDirectory (เส้นทาง); if (changeir) {ftpClient.setControlencoding (การเข้ารหัส); ftpfile [] files = ftpClient.listfiles (); สำหรับ (ไฟล์ ftpfile: ไฟล์) {if (list.size ()> = สูงสุด) {break; } if (file.isdirectory ()) {if (! stringutils.endswith (path, "/") &&! stringutils.endswith (path, file.separator)) {ถ้า (commander.islinux) {path = path + file.Spearator; } else {path = path + "/"; }} list.addall (this.listremoteFilePath (path + file.getName ())); } อื่นถ้า (เปลี่ยนไป) {if (! stringUtils.endswith (พา ธ , "/") &&! stringutils.endswith (path, file.separator)) {if (commander.islinux) {path = path + file.separator; } else {path = path + "/"; }} ไฟล์ saveFile = ไฟล์ใหม่ (path + file.getName ()); if (! savefile.exists ()) {list.add (path + file.getName ()); }}}}} catch (Exception E) {E.printStackTrace (); } return list; } @Override โมฆะสาธารณะ closeconnection () {ถ้า (ftpClient! = null) {ลอง {ftpClient.logout (); } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {if (ftpClient.isconnected ()) {ลอง {ftpClient.disconnect (); } catch (ioexception e) {e.printstacktrace (); }}}}}} [java] ดู plain copypackage com.eastrobot.remote; นำเข้า Java.io.File; นำเข้า Java.io.FileOutputStream; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า java.util.properties; นำเข้า java.util.vector; นำเข้า org.apache.commons.io.ioutils; นำเข้า org.apache.commons.lang.stringutils; นำเข้า org.apache.commons.logging.log; นำเข้า org.apache.commons.logging.logfactory; นำเข้า com.eastrobot.command.commander; นำเข้า com.jcraft.jsch.channel; นำเข้า com.jcraft.jsch.channelsftp; นำเข้า com.jcraft.jsch.jsch; นำเข้า com.jcraft.jsch.session; นำเข้า com.jcraft.jsch.sftpattrs; นำเข้า com.jcraft.jsch.sftpexception; นำเข้า com.jcraft.jsch.channelsftp.lsentry; คลาสสาธารณะ filesftpremote ขยาย fileremote {เซสชันการป้องกัน = null; Channelsftp channel = null; logger logs คงสุดท้ายสุดท้าย = logfactory.getLog (filesftpremote.class); @Override โมฆะสาธารณะ initftpinfo (สตริงโฮสต์, พอร์ตสตริง, ชื่อผู้ใช้สตริง, รหัสผ่านสตริง) {ลอง {jsch jsch = new jsch (); // 创建 jsch 对象เซสชัน = jsch.getSession (ชื่อผู้ใช้, โฮสต์, stringutils.isnotblank (พอร์ต)? integer.valueof (พอร์ต): 22); session.setPassword (รหัสผ่าน); // 设置密码คุณสมบัติ config = คุณสมบัติใหม่ (); config.put ("stricthostkeychecking", "no"); session.setConfig (config); // 为เซสชัน对象设置คุณสมบัติเซสชัน Settimeout (60000); // 设置หมดเวลา时间เซสชัน connect (); // 通过เซสชัน建立链接 channel chan = session.openchannel ("sftp"); // 打开 sftp 通道 chan.connect (); // 建立 sftp 通道的连接 channel = (channelsftp) chan; } catch (exception e) {logger.error ("SFTP 连接失败"); E.PrintStackTrace (); }} @Override โมฆะสาธารณะดาวน์โหลด RemoteFile (String filePath) {ถ้า (StringUtils.endswith (filepath, "/") || stringutils.endswith (filepath, file.Sparator)) {filePath = filePath.SubString } ไฟล์ saveFile = ไฟล์ใหม่ (filePath); fileOutputStream output = null; ลอง {ถ้า (savefile.exists ()) {return; } // 创建目标文件路径ถ้า (! savefile.getParentFile (). มีอยู่ ()) {saveFile.getParentFile (). mkdirs (); } saveFile.CreatenewFile (); // 文件所在目录 String Path = filepath.substring (0, filepath.lastindexof ("/")); if (! stringUtils.endswith (path, "/") &&! stringUtils.endswith (path, file.separator)) {ถ้า (commander.islinux) {path = path + file.Saperator; } else {path = path + "/"; }} channel.cd (เส้นทาง); channel.get (filepath, ใหม่ fileOutputStream (savefile)); logger.debug ("文件:" + filepath + "____________________________________________ 下载成功!"); } catch (exception e) {logger.debug ("文件:" + filepath + "________________________________________________ 下载失败!"); E.PrintStackTrace (); } ในที่สุด {ioutils.closequietly (เอาท์พุท); }} @SuppressWarnings ("ไม่ได้ตรวจสอบ") @Override รายการสาธารณะ <String> ListreMoteFilePath (เส้นทางสตริง) {รายการ <String> list = new ArrayList <String> (); เวกเตอร์ <lsentry> v = null; ลอง {if (! stringUtils.endswith (path, "/") && stringutils.endswith (path, file.separator)) {path = path + file.separator; } v = channel.ls (เส้นทาง); } catch (sftpexception e) {e.printstacktrace (); } สำหรับ (lsentry lsentry: v) {ถ้า (list.size ()> = สูงสุด) {break; } if (! ".". เท่ากับ (lsentry.getFilename ()) &&! ".. ". เท่ากับ (lsentry.getFileName ())) {sftpattrs attrs = lsentry.getattrs (); if (attrs.isdir ()) {if (! stringutils.endswith (path, "/") &&! stringutils.endswith (path, file.separator)) {ถ้า (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)) {ถ้า (commander.islinux) {path = path + file.separator; } else {path = path + "/"; }} ไฟล์ saveFile = ไฟล์ใหม่ (path + lsentry.getFilename ()); if (! savefile.exists ()) {list.add (พา ธ + lsentry.getFilename ()); }}}} return list; } @Override โมฆะสาธารณะ closeconnection () {ลอง {if (channel! = null) {channel.quit (); channel.disconnect (); } if (เซสชัน! = null) {session.disconnect (); }} catch (exception e) {e.printstacktrace (); }} เซสชันสาธารณะ getSession () {return session; } การตั้งค่าโมฆะสาธารณะ (เซสชันเซสชัน) {this.session = เซสชัน; } ช่องสาธารณะ channelsftp getChannel () {return channel; } โมฆะสาธารณะ setChannel (ช่องช่องสัญญาณ) {this.channel = channel; -以上所述是小编给大家介绍的 sftp 和 ftp 根据配置远程服务器地址下载文件到当前服务, 希望对大家有所帮助, 如果大家有任何疑问请给我留言,, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!