Implement SFTP server functions in JAVA

Source: Internet
Author: User

Because the project needs to encrypt and upload files, it is inconvenient to use the SFTP service provided by the Linux system. Therefore, you need to implement a set of SFTP services by yourself. Here we use the SSHD Protocol implemented by MINA, an open-source software of Apache. Specific URL: http://mina.apache.org/sshd-project/embedding_ssh.html

 

You need to reference some Apache open-source architecture packages,

The server segment is implemented as follows:

 

Package com. song. sftpServer; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. apache. sshd. sshServer; import org. apache. sshd. client. userAuth; import org. apache. sshd. common. namedFactory; import org. apache. sshd. common. keyprovider. fileKeyPairProvider; import org. apache. sshd. server. command; import org. apache. sshd. server. passwordAuthenticator; import org. apache. sshd. server. auth. userAuthNone; import org. apache. sshd. server. command. scpCommandFactory; import org. apache. sshd. server. keyprovider. simpleGeneratorHostKeyProvider; import org. apache. sshd. server. sftp. sftpSubsystem; import org. apache. sshd. server. shell. processShellFactory; public class SftpServer {public static void main (String [] args) {// factory method. Use the default property to create an ssh service object SshServer sshd = SshServer. setupdefaserver server (); sshd. setPort (4444); String stest = "F: \ MyEclipse \ SFTPServer \ key. pem "; String [] sKyepPath = {stest}; // Save the session security verification information, key. pem and key. ser is the file name sshd. setKeyPairProvider (new FileKeyPairProvider (sKyepPath); List <NamedFactory <Command> namedFactoryList = new ArrayList <NamedFactory <Command> (); namedFactoryList. add (new SftpSubsystem. factory (); sshd. setSubsystemFactories (namedFactoryList); sshd. setCommandFactory (new ScpCommandFactory (); sshd. setShellFactory (new ProcessShellFactory (); sshd. setPasswordAuthenticator (new Authcator (); try {sshd. start ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

 

Certification

package com.song.SftpServer;import org.apache.sshd.server.PasswordAuthenticator;import org.apache.sshd.server.session.ServerSession;public class Authcator implements PasswordAuthenticator {@Overridepublic boolean authenticate(String arg0, String arg1, ServerSession arg2) {// TODO Auto-generated method stubif(arg0.endsWith("song")&&arg1.endsWith("song")){return true;}else{return false;}}}

 

 

The implementation is as follows. You can use FileZile to successfully connect to the server. There is no Chinese problem on the Linux server. The only drawback is that Chinese decoding is not supported for the time being and will be studied later.

650) this. width = 650; "title =" qq 30905214115.jpg "src =" http://img1.51cto.com/attachment/201309/214235746.jpg "/>

This article is from the "Feng qing yang song" blog, please be sure to keep this source http://2309998.blog.51cto.com/2299998/1289616

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.