sftp Java Upload

Source: Internet
Author: User

1. Attention ISSUES

Format of Uri: Sftp://zhangsan:[email protected]:22

Dir question: determine if there is no creation and then go into the analogy

Config problem: stricthostkeychecking=no

Progress issue: Variable Saving required

2. code

    Private voidTransfer_sftp (String attrpath, Element groupnode,LongSizethrowsjschexception, sftpexception, Unsupportedencodingexception{attrpath= Attrpath.replaceall ("sftp://", "" "); string[] Tmps= Attrpath.split (":"); String Dstusername= Tmps[0]; intPort = 22; String Portstr= Tmps[2].replaceall ("/", "" "); Try{port=Integer.parseint (portstr); } Catch(numberformatexception E) {e.printstacktrace (); } string[] tmp= Tmps[1].split ("@"); String Dstpassword= Tmp[0]; String Sftpip= Tmp[1]; List<?> files = groupnode.selectnodes ("File");  for(intk=0; K<files.size (); k++) {jsch Jsch=NewJsch (); Session Session=jsch.getsession (dstusername, sftpip, port); Logger.info ("jsch Session created:user:" +dstusername + ", password=" +dstpassword+ ", sftpip=" +sftpip+ ", port=" +port);            Session.setpassword (dstpassword); Properties Sshconfig=NewProperties (); Sshconfig.put ("stricthostkeychecking", "no");            Session.setconfig (sshconfig);            Session.connect (); Logger.info ("jsch Session connected > user:" +dstusername); Channelsftp channelsftp= (channelsftp) Session.openchannel ("sftp");            Channelsftp.connect (); Logger.info ("Connected to" +sftpip); Element file=(Element) Files.get (k); String Srcfilepath= File.elementtext ("srcfilename"); String Dstfilepath= File.elementtext ("dstfilename"); Logger.info ("upload file on SFTP protocol >> srcfilepath=" +srcfilepath + ", dstfilepath=" +dstfilepath); intindex = Dstfilepath.lastindexof ("/"); String Path= dstfilepath.substring (0, Index + 1); String filename= dstfilepath.substring (index + 1, Dstfilepath.length ()); if(path.startswith ("/")) Path= Path.substring (1); Logger.info ("dest path:" +path); if(filename.startswith ("/")) filename= Filename.substring (1); string[] Patharray= Path.split ("/");  for(String Pathstr:patharray) {Try{logger.info ("channelsftp dir::" +pathstr); Vector ls=Channelsftp.ls (pathstr); if(ls! =NULL) {channelsftp.cd (pathstr); }                } Catch(Exception e) {logger.info ("channelsftp Catch mkdir::" +pathstr);                    Channelsftp.mkdir (pathstr);                    CHANNELSFTP.CD (pathstr);                E.printstacktrace (); }            }//string dir = new string (path.getbytes ("gb2312"), "iso-8859-1");//string destname = new string (filename.getbytes ("gb2312"), "iso-8859-1");//System.out.println ("upload directory name:::" +dir);//System.out.println ("file name after uploading:::" +destname);//channelsftp.put (srcfilepath, dstfilepath, new Sftpprogressmonitor () {Channelsftp.put (srcfilepath, filename,Newsftpprogressmonitor () {Longn = 0L; Long_max = 0L;  public Longgetn () {returnn; }                 public voidSETN (LongN) { this. N =n; } @Override public voidInitintarg0, string arg1, string arg2,Longmax) {                     this. _max =max; Logger.info ("SFTP Transferring begin....._max=" +_max); } @Override public voidEnd () {logger.info ("SFTP Transferring End ...."); } @Override public BooleanCountLongCount) {n+=count;//logger.info ("SFTP Transferring count=" +count+ ", n=" +n);                    if(_max!=0) {run_progress= (int) ((n*100)/_max);//logger.info ("the Progress of upload file through sftp:" + run_progress + "%");                    }                    return true;            }}, channelsftp.overwrite);            Channelsftp.quit ();                    Session.disconnect (); }    }

sftp Java Upload

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.