Thursday, 15 July 2010

how to transfer a file through SFTP in java? -



how to transfer a file through SFTP in java? -

this question has reply here:

java sftp server library? [closed] 8 answers transfer files via sftp 1 reply

how transfer file through sftp in java? want sample code sftp client. want embed sftp server in application , client should able send file application.

try code.

public void send (string filename) { string sftphost = "host:ip"; int sftpport = 22; string sftpuser = "username"; string sftppass = "password"; string sftpworkingdir = "file/to/transfer"; session session = null; channel channel = null; channelsftp channelsftp = null; system.out.println("preparing host info sftp."); seek { jsch jsch = new jsch(); session = jsch.getsession(sftpuser, sftphost, sftpport); session.setpassword(sftppass); java.util.properties config = new java.util.properties(); config.put("stricthostkeychecking", "no"); session.setconfig(config); session.connect(); system.out.println("host connected."); channel = session.openchannel("sftp"); channel.connect(); system.out.println("sftp channel opened , connected."); channelsftp = (channelsftp) channel; channelsftp.cd(sftpworkingdir); file f = new file(filename); channelsftp.put(new fileinputstream(f), f.getname()); log.info("file transfered host."); } grab (exception ex) { system.out.println("exception found while tranfer response."); } finally{ channelsftp.exit(); system.out.println("sftp channel exited."); channel.disconnect(); system.out.println("channel disconnected."); session.disconnect(); system.out.println("host session disconnected."); } }

java file sftp edi b2b

No comments:

Post a Comment