Transfer folder and subfolders using channelsftp in JSch?

后端 未结 3 705
生来不讨喜
生来不讨喜 2020-12-03 09:16

I want to transfer a folder and a subfolder using channelsftp. I can successfully transfer files using channelsftp.put(src,dest) command but this does not work

3条回答
  •  猫巷女王i
    2020-12-03 10:01

    Above code(by zon) works for download as per my understanding.I need to upload to a remote server.I wrote below code to achieve the same.Please try and comment if any issue(it ignores files starting with ".")

    private static void lsFolderCopy(String sourcePath, String destPath,
                ChannelSftp sftpChannel) throws SftpException,   FileNotFoundException {
        File localFile = new File(sourcePath);
    
    if(localFile.isFile())
    {
    
        //copy if it is a file
        sftpChannel.cd(destPath);
    
        if(!localFile.getName().startsWith("."))
        sftpChannel.put(new FileInputStream(localFile), localFile.getName(),ChannelSftp.OVERWRITE);
    }   
    else{
        System.out.println("inside else "+localFile.getName());
        File[] files = localFile.listFiles();
    
        if(files!=null && files.length > 0 && !localFile.getName().startsWith("."))
        {
    
            sftpChannel.cd(destPath);
            SftpATTRS  attrs = null;
    
        //check if the directory is already existing
        try {
            attrs = sftpChannel.stat(destPath+"/"+localFile.getName());
        } catch (Exception e) {
            System.out.println(destPath+"/"+localFile.getName()+" not found");
        }
    
        //else create a directory   
        if (attrs != null) {
            System.out.println("Directory exists IsDir="+attrs.isDir());
        } else {
            System.out.println("Creating dir "+localFile.getName());
            sftpChannel.mkdir(localFile.getName());
        }
    
        //System.out.println("length " + files.length);
    
         for(int i =0;i

提交回复
热议问题