i need program file transfer using jsch library. have simple directory 2 folders -
in sftp_1
folder, have bitmap image. , sftp_2
folder empty folder. goal transfer image using sftp sftp_1 sftp_2 .
here code far :
import com.jcraft.jsch.*; import java.awt.desktop; import java.nio.channels.channel; public class filetransfer { public filetransfer() { super(); } public static void main (string[] args) { filetransfer filetransfer = new filetransfer(); jsch jsch = new jsch(); try { string host = "127.0.0.1"; int port = 22; string user = "user"; session session = jsch.getsession(user, host, port); session = jsch.getsession("username", "127.0.0.1", 22); session.connect(); //channel channel = session.openchannel("sftp"); channelsftp sftp = null; sftp = (channelsftp)session.openchannel("sftp") ; //channel; //channel.connect(); //channel channel = session.openchannel("shell"); sftp.rename("c:\\users\\admin\\desktop\\work\\connectone_bancorp\\java_work\\sftp_1\\house.bmp", "c:\\users\\admin\\desktop\\work\\connectone_bancorp\\java_work\\sftp_2\\house.bmp"); // /sftp_1/file.txt //sftpchannel.get("remotefile.txt", "localfile.txt"); //sftpchannel.exit(); session.disconnect(); } catch (jschexception e) { e.printstacktrace(); } catch (sftpexception e) { e.printstacktrace(); } } }
what transfer file 1 directory in machine, directory. tips appreciated, !
note copy between 2 folders, 1 doesn't need use sftp. 1 can copy 1 folder without involving sftp protocol primarly used copy files remotely, either local machine remote machine, or remote machine (the same or different) remote machine, or remote machine local machine.
that's because ftp network based protocol. using (or of it's related protocols) going use network (or simulated network).
the security jsch provides security designed protect kinds of attacks occur on networks. not provide security within machine.
to copy files between folders on single machine, simplest way not use jsch, so
private static void copyfileusingjava7files(file source, file dest) throws ioexception { files.copy(source.topath(), dest.topath()); }
there other techniques, , if want use jsch, need realize jsch must provided lot of "extra" information connect machine on, because try connect machine if connecting across network
session sessionread = jsch.getsession("username", "127.0.0.1", 22); sessionread.connect(); session sessionwrite = jsch.getsession("username", "127.0.0.1", 22); sessionwrite.connect(); channelsftp channelread = (channelsftp)sessionread.openchannel("sftp"); channelread.connect(); channelsftp channelwrite = (channelsftp)sessionwrite.openchannel("sftp"); channelwrite.connect(); pipedinputstream pin = new pipedinputstream(2048); pipedoutputstream pout = new pipedoutputstream(pin); channelread.get("/path/to/your/file/including/filename.txt", pout); channelwrite.put(pin, "/path/to/your/file/destination/including/filename.txt"); channelread.disconnect(); channelwrite.disconnect(); sessionread.disconnect(); sessionwrite.disconnect();
the above code lacks error checking, exception handling, , fall routines if files missing, networks not up, etc. should main idea.
it should obvious using network protocol no network protocol needs exist opens door lot more failure scenarios. use sftp method if program meant copy files not both located on machine.
Comments
Post a Comment