Transfer folder and subfolders using channelsftp in JSch?

后端 未结 3 687
生来不讨喜
生来不讨喜 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 09:58

    From: http://the-project.net16.net/Projekte/projekte/Projekte/Programmieren/sftp-synchronisierung.html

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Vector;
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.ChannelSftp.LsEntry;
    import com.jcraft.jsch.SftpException;
    
    public class FileMaster {
    	public boolean FileAction;
    	public File local;
    	public String serverDir;
    	public ChannelSftp channel;
    	
    	public FileMaster(boolean copyOrDelete, File local, String to, ChannelSftp channel){
    		this.FileAction = copyOrDelete;
    		this.local = local;
    		this.serverDir = to;
    		this.channel = channel;
    	}
    	
    	/*
    	 * If FileAction = true, the File local is copied to the serverDir, else the file is deleted.
    	 */
    	public void runMaster() throws FileNotFoundException, SftpException{
    		if(FileAction){
    			copy(local, serverDir, channel);
    		} else { 
    			delete(serverDir, channel);
    		}
    	}
    	
    	/*
    	 * Copies recursive
    	 */
    	public static void copy(File localFile, String destPath, ChannelSftp clientChannel) throws SftpException, FileNotFoundException{
    		if(localFile.isDirectory()){
    			clientChannel.mkdir(localFile.getName());
    			GUI.addToConsole("Created Folder: " + localFile.getName() + " in " + destPath);
    
    			destPath = destPath + "/" + localFile.getName();
    			clientChannel.cd(destPath);
    			
    			for(File file: localFile.listFiles()){
    				copy(file, destPath, clientChannel);
    			}
    			clientChannel.cd(destPath.substring(0, destPath.lastIndexOf('/')));
    		} else {
    			GUI.addToConsole("Copying File: " + localFile.getName() + " to " + destPath);
    			clientChannel.put(new FileInputStream(localFile), localFile.getName(),ChannelSftp.OVERWRITE);
    		}
    
    	}
    	
    	/*
    	 * File/Folder is deleted, but not recursive
    	 */
    	public void delete(String filename, ChannelSftp sFTPchannel) throws SftpException{		
    		if(sFTPchannel.stat(filename).isDir()){
    			@SuppressWarnings("unchecked")
    			Vector fileList = sFTPchannel.ls(filename);
    			sFTPchannel.cd(filename);
    			int size = fileList.size();
    			for(int i = 0; i < size; i++){
    				if(!fileList.get(i).getFilename().startsWith(".")){
    					delete(fileList.get(i).getFilename(), sFTPchannel);
    				}
    			}
    			sFTPchannel.cd("..");
    			sFTPchannel.rmdir(filename);
    		} else {
    			sFTPchannel.rm(filename.toString());
    		}
    		GUI.addToConsole("Deleted: " + filename + " in " + sFTPchannel.pwd());
    	}
    
    }

提交回复
热议问题