If you are familiar with Linux systems, you are familiar with commands such as SSH,SFTP,SCP. SSH is a security protocol that is used to secure connections between different systems or servers. SSH encrypts all data during the connection and transfer process.
And today I'm going to introduce a jar package that is using Jsch. Jsch is a purely Java library that implements SSH functionality in Java.
The official address is: http://www.jcraft.com/jsch/
The GitHub address is: Https://github.com/vngx/vngx-jsch
The MAVEN configuration is as follows:
<!--Add sftp Dependency Package - <Dependency> <groupId>Com.jcraft</groupId> <Artifactid>Jsch</Artifactid> <version>0.1.50</version> </Dependency>
View Code
For this design we first design an FTP connection interface:
PackageCom.xuanyuan.tools;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.util.Vector;/*** FTP Interface *@authorPunk Lin * @email [email protected] * @date December 23, 2016 **/ Public InterfaceFtpservice {/*** Log in *@paramhost log-in address *@paramuserName User name *@paramPassword Password *@paramPort Ports *@throwsException*/ Public voidLogin (string host, String userName, string password,intPortthrowsException; /*** Exit*/ Public voidlogout (); /*** Upload file * *@paramIn * Input stream *@paramremotefilepath * Remote file absolute path *@throwsException*/ Public voidUploadFile (InputStream in, String Remotefilepath)throwsException; /*** File Download to local * *@paramsourcefilepath * Remote file absolute path *@paramLocalfilepath * Local directory or absolute path *@throwsException*/ Public voiddownloadFile (String sourcefilepath, string localfilepath)throwsException; /*** File download to output stream * *@paramsourcefilepath * Remote file absolute path *@paramout * Output stream *@throwsException*/ Public voidDownloadFile (String Sourcefilepath, outputstream out)throwsException; /*** Delete Files * *@paramDirectory * To delete directories in which files are located *@paramDeleteFile * files to delete * *@throwsException*/ Public voidDeleteFile (string directory, String fileName)throwsException; /*** List files in directory, including directory * *@paramDirectory * Directories to list * *@returnList file Name List * *@throwsException*/ PublicVector<?> listfiles (String directory)throwsException;}
View Code
For its specific implementation, the following are:
PackageCom.xuanyuan.tools.impl;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.util.Properties;ImportJava.util.Vector;Importcom.jcraft.jsch.ChannelSftp;ImportCom.jcraft.jsch.JSch;Importcom.jcraft.jsch.JSchException;Importcom.jcraft.jsch.Session;Importcom.jcraft.jsch.SftpException;ImportCom.xuanyuan.tools.FTPService; Public classSftpserviceimplImplementsFtpservice {/**SFTP Sessions Session*/ protectedSession sshsession =NULL; /**SFTP Channel*/ protectedChannelsftp SFTP =NULL; @Override Public voidLogin (string host, String userName, string password,intPort)throwsException {Try{Jsch Jsch=NewJsch (); This. sshsession =jsch.getsession (UserName, host, Port); This. Sshsession.setpassword (password); Properties Sshconfig=NewProperties (); Sshconfig.put ("Stricthostkeychecking", "no"); This. Sshsession.setconfig (Sshconfig); This. Sshsession.connect (20000); This. SFTP = (channelsftp) sshsession.openchannel ("Sftp"); This. Sftp.connect (); This. sftp.setfilenameencoding ("UTF-8"); } Catch(jschexception e) {Throw NewRuntimeException ("Cannot login with SFTP, please check username password or port"); }} @Override Public voidlogout () {if( This. sftp! =NULL) { This. Sftp.disconnect (); This. Sshsession.disconnect (); This. SFTP =NULL; This. sshsession =NULL; }} @Override Public voidUploadFile (InputStream in, String Remotefilepath)throwsException {Try{String filepath= remotefilepath.substring (0, Remotefilepath.lastindexof ("/")); String FileName=remotefilepath.substring (Remotefilepath.lastindexof ("/") + 1, Remotefilepath.length ()); This. SFTP.CD (filepath); This. Sftp.put (in, FileName, 0); } Catch(sftpexception e) {Throw NewRuntimeException ("An error occurred while uploading the file! Please check that the file path is correct "); }} @Override Public voiddownloadFile (String sourcefilepath, string localfilepath)throwsException {File file=NewFile (Localfilepath); FileOutputStream out=NULL; Try { if(File.isdirectory ()) { This. Sftp.get (Sourcefilepath, Localfilepath); } Else{ out=Newfileoutputstream (file); This. Sftp.get (Sourcefilepath, out); } } Catch(sftpexception e) {Throw NewRuntimeException ("There was an error downloading the file! Please check that the file path is correct "); } finally { if(Out! =NULL) Out.close (); }} @Override Public voidDownloadFile (String Sourcefilepath, outputstream out)throwsException {Try { This. Sftp.get (Sourcefilepath, out); } Catch(sftpexception e) {Throw NewRuntimeException ("There was an error downloading the file! Please check that the file path is correct "); }} @Override Public voidDeleteFile (string directory, String fileName)throwsException { This. SFTP.CD (directory); This. SFTP.RM (FileName); } @Override PublicVector<?> listfiles (String directory)throwsException {return This. Sftp.ls (directory); }}
View Code
The tool is now complete. In this way we can also provide, through this tool, Web pages such as the Evangelist server.
It is easier to believe in others than to believe in yourself, to be afraid of the unknown, or to be afraid of weakness.
How to use Jcraft to simulate SFTP login