: : : :
حزمة com.easstrobot.remote ؛ استيراد java.util.list ؛ استيراد java.util.concurrent.executorservice ؛ استيراد java.util.concurrent.executors ؛ استيراد org.apache.commons.logging.log ؛ استيراد org.apache.commons.logging.logfactory ؛ استيراد com.easstrobot.util.propertiesUtil ؛ /** * full.zhang * * ftp/sftp 抽象方法类 * */Public Abstract Class fileremote {private Static Final Final ftp_mode = "ftp" ؛ Static Final String SFTP_MODE = "SFTP" ؛ السلسلة الثابتة العامة ftproot ؛ وضع السلسلة الثابتة العامة ؛ مضيف سلسلة ثابتة عامة ؛ اسم مستخدم السلسلة الثابتة العامة ؛ كلمة مرور السلسلة الثابتة العامة ؛ منفذ السلسلة الثابتة العامة ؛ عميل FileRemote ثابت خاص = فارغ ؛ // 最大一次性下载 50 个文件 static int max = 50 ؛ logger logger static Final Final = logfactory.getLog (fileremote.class) ؛ public fileremote getInstance () {if (client == null) {ftproot = propertiesutil.getString ("transfer.root") ؛ mode = propertiesUtil.getString ("transfer.mode") ؛ مضيف = propertiesUtil.getString ("transfer.host") ؛ اسم المستخدم = propertiesUtil.getString ("transfer.username") ؛ password = propertiesUtil.getString ("transfer.password") ؛ port = propertiesUtil.getString ("transfer.port") ؛ if (mode.equals (ftp_mode)) {client = new fileftpremote () ؛ } آخر إذا (mode.equals (sftp_mode)) {client = new filesftpremote () ؛ }} عميل الإرجاع ؛ } / ** * 执行定时任务 * / public void process () {logger.debug ("------------------------------------------ 进入定时下载远程文件") ؛ // 创建线程池 ExecutorService exec = eversors.newsingleThreadExecutor () ؛ exec.execute (new RunNable () {Override public void run () {// 建立连接 initftpinfo (المضيف ، المنفذ ، اسم المستخدم ، كلمة المرور) ؛ // 远程服务所有源文件路径集合 قائمة <string> listsourcepath = listremotefilepath (ftproot) ؛ if (sistsourcepthepty.ispty client.closeConnect () ؛ exec.shutdown () ؛ } / ** * 初始化连接 * * param host * param port * param username * param password * athrows استثناء * regurn * / public stustr void itftpinfo (مضيف سلسلة ، منفذ السلسلة ، اسم مستخدم السلسلة ، كلمة مرور السلسلة) ؛ / ** * 下载远程服务下文件到本地服务 * * param path * regurn * @throws استثناء */ public Abstract void downloadRemoteFile (string filepath) ؛ / *: / ** * 释放连接 */ Public Abstract void closeConnection () ؛ } [java] عرض copypackage com.easstrobot.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.easstrobot.command.commander ؛ يمتد FileftPremote من الفئة العامة fileremote {محمية ftpclient ftpclient ؛ سلسلة private تشفير = "UTF-8" ؛ خاص منطقي BinaryTransfer = صحيح ؛ logger logger static Final Final = logfactory.getLog (fileftpremote.class) ؛ Override public void initftpinfo (مضيف السلسلة ، منفذ السلسلة ، اسم مستخدم السلسلة ، كلمة مرور السلسلة) {try {// 构造一个 ftpclient 实例 ftpclient = new ftpclient () ؛ // 设置字符集 ftpclient.setControlenCoding (الترميز) ؛ // 连接 ftp 服务器 ftpclient.connect (مضيف ، stringUtils.isnotBlank (port)؟ integer.valueof (port): 21) ؛ // 连接后检测返回码来校验连接是否成功 int reply = ftpclient.getReplyCode () ؛ if (ftpreply.ispositivecompletion (الرد)) {// 登陆到 ftp 服务器 if (ftpclient.login (اسم المستخدم ، كلمة المرور)) {setFiLetype () ؛ } ftpclient.login (اسم المستخدم ، كلمة المرور) ؛ } آخر {ftpClient.Disconnect () ؛ logger.error ("ftp 服务拒绝连接!") ؛ }} catch (استثناء 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) ؛ } آخر {ftpclient.setFiletype (ftpclient.ascii_file_type) ؛ }} catch (ioException e) {E.PrintStackTrace () ؛ }} Override public void downloadRemoteFile (string filepath) {if (stringUtils.endswith (filePath ، "/") || stringUtilswith (filepath ، file.separator)) {filepath = filepath.subathring (0 ، filepath.length () - 1) ؛ } ملف SaveFile = ملف جديد (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 ؛ } آخر {path = path + "/" ؛ }} outputStream Output = null ؛ حاول {// 创建目标文件路径 if (! savefile.getParentFile (). } savefile.createNewFile () ؛ // 转移到 ftp 服务器目录 ftpclient.ChangeWorkingDirectory (path) ؛ الإخراج = FileOutputStream جديد (SaveFile) ؛ ftpClient.RetRieveFile (FilePath ، الإخراج) ؛ } catch (ioException e) {logger.debug ("文件:" + filepath + "__________________ 下载失败!") ؛ E.PrintStackTrace () ؛ } أخيرًا {logger.debug ("文件:" + filepath + "__________________ 下载成功!") ؛ ioutils.closequietly (الإخراج) ؛ }} Override Public List <string> listreMoteFilePath (مسار السلسلة) {list <string> list = new ArrayList <string> () ؛ جرب {if (! stringUtils.endswith (path ، "/") &&! stringUtils.endswith (path ، file.separator)) {if (commander.islinux) {path = path + file.separator ؛ } آخر {path = path + "/" ؛ }} boolean changeir = ftpClient.ChangeWorkingDirectory (path) ؛ if (changeir) {ftpclient.setControlenCoding (الترميز) ؛ ftpfile [] files = ftpclient.listfiles () ؛ لـ (ftpfile file: files) {if (list.size ()> = max) {break ؛ } if (file.isdirectory ()) {if (! stringUtils.endswith (path ، "/") &&! } آخر {path = path + "/" ؛ }} list.addall (this.listreMoteFilePath (path + file.getName ())) ؛ } آخر إذا (changeir) {if (! stringUtils.endswith (path ، "/") &&! stringUtils.endswith (path ، file.separator)) {if (commander.islinux) {path = path + file.separator ؛ } آخر {path = path + "/" ؛ }} file savefile = new file (path + file.getName ()) ؛ if (! savefile.exists ()) {list.add (path + file.getName ()) ؛ }}}}} catch (استثناء e) {E.PrintStackTrace () ؛ } قائمة الإرجاع ؛ } Override public void closeConnection () {if (ftpClient! = null) {try {ftpclient.logout () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } أخيرًا {if (ftpclient.isconnected ()) {try {ftpclient.disconnect () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ }}}}}} [Java] عرض 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.easstrobot.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 ؛ قناة القنوات المحمية = فارغة ؛ logger logger static Final Final = logfactory.getLog (filesftpremote.class) ؛ Override public void initftpinfo (مضيف السلسلة ، منفذ السلسلة ، اسم مستخدم السلسلة ، كلمة مرور السلسلة) {try {jsch jsch = new jsch () ؛ // 创建 jsch 对象 session = jsch.getSession (اسم المستخدم ، المضيف ، stringutils.isnotblank (port)؟ integer.valueof (port): 22) ؛ Session.setPassword (كلمة المرور) ؛ // 设置密码 properties config = خصائص جديدة () ؛ config.put ("stricthostkeychecking" ، "no") ؛ Session.setConfig (config) ؛ // 为 الجلسة 对象设置 Session.SettImeout (60000) ؛ // 设置 timeout 时间 session.connect () ؛ // 通过 الجلسة 建立链接 قناة chan = session.openchannel ("sftp") ؛ // 打开 sftp 通道 chan.connect () ؛ // 建立 sftp 通道的连接 channel = (channelsftp) chan ؛ } catch (استثناء e) {logger.error ("sftp 连接失败") ؛ E.PrintStackTrace () ؛ }} Override public void downloadRemoteFile (string filepath) {if (stringUtils.endswith (filePath ، "/") || stringUtilswith (filepath ، file.separator)) {filepath = filepath.subathring (0 ، filepath.length () - 1) ؛ } ملف SaveFile = ملف جديد (FilePath) ؛ FileOutputStream Output = null ؛ حاول {if (savefile.exists ()) {return ؛ } // 创建目标文件路径 if (! savefile.getParentFile (). } 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 ؛ } آخر {path = path + "/" ؛ }} channel.cd (path) ؛ Channel.get (FilePath ، FileOutputStream (SaveFile)) ؛ logger.debug ("文件 :" + filepath + "________________________________________ 下载成功!") ؛ } catch (استثناء e) {logger.debug ("文件 :" + filepath + "____________________________________ 下载失败!") ؛ E.PrintStackTrace () ؛ } أخيرًا {ioutils.closequietly (الإخراج) ؛ }} suppressWarnings ("uncheced") Override Public List <String> listremotefilepath (مسار السلسلة) {list <string> list = new ArrayList <String> () ؛ المتجه <lsentry> v = null ؛ جرب {if (! stringUtils.endswith (path ، "/") && stringUtils.endswith (path ، file.separator)) {path = path + file.separator ؛ } v = channel.ls (path) ؛ } catch (sftpexception e) {E.PrintStackTrace () ؛ } لـ (lsentry lsentry: v) {if (list.size ()> = max) {break ؛ } if (! ".". يساوي (lsentry.getFilename ()) &&! "..". if (attrs.isdir ()) {if (! stringutils.endswith (path ، "/") &&! stringUtils.endswith (path ، file.separator)) {if (commander.islinux) {path = path + file.separator ؛ } آخر {path = path + "/" ؛ }} list.addall (this.listreMoteFilePath (path + lsentry.getFilename ())) ؛ } آخر {if (! stringUtils.endswith (path ، "/") &&! stringUtils.endswith (path ، file.separator)) {if (commander.islinux) {path = path + file.separator ؛ } آخر {path = path + "/" ؛ }} ملف SaveFile = ملف جديد (path + lsentry.getFilename ()) ؛ if (! savefile.exists ()) {list.add (path + lsentry.getFilename ()) ؛ }}}} قائمة الإرجاع ؛ } Override public void closeConnection () {try {if (channel! = null) {channel.quit () ؛ Channel.Disconnect () ؛ } if (session! = null) {session.disconnect () ؛ }} catch (استثناء e) {E.PrintStackTrace () ؛ }} GetSession GetSession () {Greeding Session ؛ } setSession public void (جلسة الجلسة) {this.session = session ؛ } القنوات العامة getChannel () {return Channel ؛ } public void setChannel (Channelsftp Channel) {this.channel = Channel ؛ }}以上所述是小编给大家介绍的 sftp 和 ftp 根据配置远程服务器地址下载文件到当前服务 , 希望对大家有所帮助 如果大家有任何疑问请给我留言 , , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!