废话不多说、关键代码如下所示:
パッケージcom.eastrobot.remote; java.util.listをインポートします。 java.util.concurrent.executorserviceをインポートします。 java.util.concurrent.executorsをインポートします。 Import org.apache.commons.logging.log; Import org.apache.commons.logging.logfactory; com.eastrobot.util.propertiesutilをインポートします。 /** * full.zhang * * ftp/sftp private static final string sftp_mode = "sftp"; public static string ftproot; public static stringモード; public static stringホスト。 public static stringユーザー名。 public static stringパスワード。 public static stringポート。 private static fileRemote client = null; //最大一次性下载50个文件public static int max = 50;プライベート最終static 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");パスワード= 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; } / ** *执行定时任务 * / public void process(){logger.debug( "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - //创建线程池executorservice exec = executors.newsinglethreadexecutor(); exec.execute(new runnable(){@override public void run(){// client.closeconection() exec.shutdown(); } / ** *初始化连接 * * @param host * @param port * @param username * @param password * @throws例外 * @return * / public abstract void initftpinfo(string host、string port、string username、string password); / ** *下载远程服务下文件到本地服务 * * @Param Path * @return * @Throws Exception */ public Abstract void boid downloadRemoteFile(String filepath); / ** *获取远程服务下指定目录下的所有文件路径集合(包含子目录下文件) * * @param path * @return */ public abstract list <string> listremotefilepath(string path); / ** *释放连接 */ public abstract void closeconnection(); } [Java] Plain CopyPackage com.easttrobot.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をインポートします。 Import org.apache.commons.logging.log; Import org.apache.commons.logging.logfactory; Import org.apache.commons.net.ftp.ftpclient; org.apache.commons.net.ftp.ftpfileをインポートします。 import org.apache.commons.net.ftp.ftpreply; com.eastrobot.command.commanderをインポートします。 public class fileftpremoteはfileRemoteを拡張します{Protected ftpclient ftpclient;プライベート文字列エンコード= "utf-8";プライベートブールバイナリトランスファー= true;プライベート最終static logger = logfactory.getLog(fileftpremote.class); @Override public void initftpinfo(string host、string port、string username、string password){try {// //设置字符集ftpclient.setControlencoding(エンコーディング); // ftp // if(ftpreply.ispositiveCompletion(reply)){// } ftpclient.login(username、password); } else {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); } else {ftpclient.setFiletype(ftpclient.ascii_file_type); }} catch(ioexception e){e.printstacktrace(); }} @Override public void downloadRemoteFile(string filepath){if(stringutils.endswith(filepath、 "/")|| stringutils.swith(filepath、file.separator)){filepath = filepath.substring(0、filepath.length()-1); } file savefile = new file(filepath); if(savefile.exists()){return; // if(!stringutils.endswith(path、 "/")&&!stringutils.endswith(path、file.separator)){if(commander.islinux){path = path + file.separator; } else {path = path + "/"; }} outputStream output = null; try {//创建目标文件路径if(!savefile.getParentFile()。exists()){savefile.getParentFile()。mkdirs(); } savefile.createNewfile(); // ftp output = new fileoutputStream(savefile); ftpclient.retrievefile(filepath、output); } catch(ioexception e){logger.debug( "文件:" + filepath + "______________________________________________!"); e.printstacktrace(); }最後に{logger.debug( "文件:" + filepath + "____________________________!"); ioutils.closequetly(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 chanderir = ftpclient.changeworkingdirectory(path); if(chandedir){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(chanderir){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(); }返品リスト。 } @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] 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をインポートします。 Import org.apache.commons.logging.log; Import org.apache.commons.logging.logfactory; com.eastrobot.command.commanderをインポートします。 com.jcraft.jsch.channelをインポートします。 com.jcraft.jsch.channelftpをインポートします。 com.jcraft.jsch.jschをインポートします。 com.jcraft.jsch.sessionをインポートします。 com.jcraft.jsch.sftpattrsをインポートします。 com.jcraft.jsch.sftpexceptionをインポートします。 com.jcraft.jsch.channelftp.lsentryをインポートします。 public class filesftpremote extends fileRemote {Protected Session Session = null; Protected ChannelsFTP Channel = null;プライベート最終static logger = logfactory.getLog(filesftpremote.class); @Override public void initftpinfo(string host、string port、string username、string password){try {jsch jsch = new jsch(); //创建jsch对象セッション= jsch.getSession(username、host、stringutils.isnotblank(port)?integer.valueof(port):22); session.setPassWord(パスワード); //设置密码プロパティconfig = new Properties(); config.put( "stricthostkeychecking"、 "no"); session.setconfig(config); //为セッション对象设置プロパティセッション。SettimeOut(60000); // //通过セッション建立链接チャンネルchan = session.openchannel( "sftp"); //打开sftp // } catch(Exception E){logger.error( "sftp连接失败"); e.printstacktrace(); }} @Override public void downloadRemoteFile(string filepath){if(stringutils.endswith(filepath、 "/")|| stringutils.swith(filepath、file.separator)){filepath = filepath.substring(0、filepath.length()-1); } file savefile = new file(filepath); fileoutputStream出力= null; try {if(savefile.exists()){return; } //创建目标文件路径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(); }最後に{ioutils.closequetly(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())&&! ".."。 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(例外e){e.printstacktrace(); }} public session getSession(){return session; } public void setSession(セッションセッション){this.session = session; } public channelsftp getChannel(){return channel; } public void setChannel(channelsftpチャンネル){this.channel = channel; }}以上所述是小编给大家介绍的sftp和ftp根据配置远程服务器地址下载文件到当前服务、希望对大家有所帮助、如果大家有任何疑问请给我留言、小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!