您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > Sftp-JAVA全操作
packageword2pdf;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjava.util.Properties;importjava.util.Vector;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.jcraft.jsch.Channel;importcom.jcraft.jsch.ChannelSftp;importcom.jcraft.jsch.ChannelSftp.LsEntry;importcom.jcraft.jsch.JSch;importcom.jcraft.jsch.Session;publicclassSFtpCollectionUtil{/***初始化日志引擎*/privatefinalLoggerlogger=LoggerFactory.getLogger(SFtpCollectionUtil.class);/**Sftp*/ChannelSftpsftp=null;/**主机*/privateStringhost=;/**端口*/privateintport=0;/**用户名*/privateStringusername=;/**密码*/privateStringpassword=;/***构造函数**@paramhost*主机*@paramport*端口*@paramusername*用户名*@parampassword*密码**/publicSFtpCollectionUtil(Stringhost,intport,Stringusername,Stringpassword){this.host=host;this.port=port;this.username=username;this.password=password;}/***连接sftp服务器**@throwsException*/publicvoidconnect()throwsException{JSchjsch=newJSch();SessionsshSession=jsch.getSession(this.username,this.host,this.port);logger.debug(SFtpCollectionUtil.class+Sessioncreated.);sshSession.setPassword(password);PropertiessshConfig=newProperties();sshConfig.put(StrictHostKeyChecking,no);sshSession.setConfig(sshConfig);sshSession.connect(20000);logger.debug(SFtpCollectionUtil.class+Sessionconnected.);logger.debug(SFtpCollectionUtil.class+OpeningChannel.);Channelchannel=sshSession.openChannel(sftp);channel.connect();this.sftp=(ChannelSftp)channel;logger.debug(SFtpCollectionUtil.class+Connectedto+this.host+.);}/***Disconnectwithserver**@throwsException*/publicvoiddisconnect()throwsException{if(this.sftp!=null){if(this.sftp.isConnected()){this.sftp.disconnect();}elseif(this.sftp.isClosed()){logger.debug(SFtpCollectionUtil.class+sftpisclosedalready);}}}/***上传单个文件**@paramdirectory*上传的目录*@paramuploadFile*要上传的文件**@throwsException*/publicvoidupload(Stringdirectory,StringuploadFile)throwsException{this.sftp.cd(directory);Filefile=newFile(uploadFile);this.sftp.put(newFileInputStream(file),file.getName());}/***上传目录下全部文件**@paramdirectory*上传的目录**@throwsException*/publicvoiduploadByDirectory(Stringdirectory)throwsException{StringuploadFile=;ListStringuploadFileList=this.listFiles(directory);IteratorStringit=uploadFileList.iterator();while(it.hasNext()){uploadFile=it.next().toString();this.upload(directory,uploadFile);}}/***下载单个文件**@paramdirectory*下载目录*@paramdownloadFile*下载的文件*@paramsaveDirectory*存在本地的路径**@throwsException*/publicvoiddownload(Stringdirectory,StringdownloadFile,StringsaveDirectory)throwsException{StringsaveFile=newString((saveDirectory+//+downloadFile).getBytes(gbk),utf-8);this.sftp.cd(directory);Filefile=newFile(saveFile);if(file.exists()){logger.info(saveFile+已经存在,不再下载);return;}OutputStreamos=newFileOutputStream(file);this.sftp.get(downloadFile,os);os.close();//关闭输出流}/***下载目录下全部文件**@paramdirectory*下载目录**@paramsaveDirectory*存在本地的路径**@throwsException*/publicvoiddownloadByDirectory(Stringdirectory,StringsaveDirectory)throwsException{StringdownloadFile=;ListStringdownloadFileList=this.listFiles(directory);IteratorStringit=downloadFileList.iterator();while(it.hasNext()){downloadFile=it.next().toString();if(downloadFile.toString().indexOf(.)0){Filefile=newFile(saveDirectory+\\+downloadFile);if(!file.exists()){file.mkdir();}downloadByDirectory(directory+/+downloadFile,saveDirectory+/+downloadFile);//迭代执行}else{this.download(directory,downloadFile,saveDirectory);}}}/***删除文件**@paramdirectory*要删除文件所在目录*@paramdeleteFile*要删除的文件**@throwsException*/publicvoiddelete(Stringdirectory,StringdeleteFile)throwsException{this.sftp.cd(directory);this.sftp.rm(deleteFile);}/***列出目录下的文件**@paramdirectory*要列出的目录**@returnlist文件名列表**@throwsException*/@SuppressWarnings(unchecked)publicListStringlistFiles(Stringdirectory)throwsException{VectorfileList;ListStringfileNameList=newArrayListString();fileList=this.sftp.ls(directory);Iteratorit=fileList.iterator();while(it.hasNext()){StringfileName=((LsEntry)it.next()).getFilename();if(..equals(fileName)||...equals(fileName)){continue;}fileNameList.add(fileName);}returnfileNameList;}/***更改文件名**@paramdirectory*文件所在目录*@paramoldFileNm*原文件名*@paramnewFileNm*新文件名**@throwsException*/publicvoidrename(Stringdirectory,StringoldFileNm,StringnewFileNm)throwsException{this.sftp.cd(directory);this.sftp.rename(oldFileNm,newFileNm);}publicvoidcd(Stringdirectory)throwsException{this.sftp.cd(directory);}publicInputStreamget(Stringdirectory)throwsException{InputStreamstreatm=this.sftp.get(directory);returnstreatm;}}
本文标题:Sftp-JAVA全操作
链接地址:https://www.777doc.com/doc-4274479 .html