SFTP ChannelSftp file download

   public static ChannelSftp connect(String username,String password,String hostname,String remoteDir,int port){

        //remoteDir remote directory file download directory

        ChannelSftp sftp =null;

        Session sshSession=null;

        String loaclpacth="d:\\download ";//download to the original path

        String fileName=" ";//File name

        String zipName=" ";//Can be passed through parameters

      try{

       JSch jsch=new Jsch();

       sshSession =jsch.getSession(username,hostname,port);

       sshSession.setPassword(password);

       Properties sshConfig=new Properties();

       sshConfig.put("StrictHostKeyChecking","no");

       sshSession.setConfig(sshConfig);

       sshSession.connect();

      Channel channle=sshSession.openChannel("sftp");

      channel.connect();

       sftp=(Channel)channel;

       //The following paragraph is only used for linux systems, and part of the window can be omitted

      Voctor list = sftp.ls(remoteDir);

     String listName="";

     File remoteDirFile=null;

     for(int i=0;i<list.size();i++){

         listName=list.get(i).toString();

         String [] arr=listName.split(" ");

       if(arr !=null && arr[arr.length-1].equals(zipName){

          flag=true;

         break;

        }

      }

      if(flag){

        String unzipUrl= remoteDir+"/aaa.csv";//Remote download address and file name

        SftpATTRS attr=sftp.stat(unzipUrl);

       long filezie=attr.getSize();

       String fileName=loaclpacth+"/aaa.csv";//Download to local path

       OutputStream out = new FileOutputStream(fileName);

      sftp.get(unzipUrl,fileName);//Download unzipUrl remote download address to local fileName;

      //Unzipping the zip file. . . . .

     unzip(filename, zip file path)

     }

     }catch(exception e){


    }finally{

     sftp.quit();

    sshSession.disconnect();

  }

      return sftp;


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938590&siteId=291194637