java sftp文件上传核心代码

来源:互联网 发布:软件系统测试报告用途 编辑:程序博客网 时间:2024/05/17 01:28
package sftp;


import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.util.Properties;  
  
import com.jcraft.jsch.Channel;  
import com.jcraft.jsch.ChannelSftp;  
import com.jcraft.jsch.JSch;  
import com.jcraft.jsch.Session;  
import com.jcraft.jsch.SftpException;  
  
public class SFTPUtil {  
      
    private String host = "172.30.104.40";  
    private String username="weblogic";  
    private String password="weblogic";  
    private int port = 22;  
    private ChannelSftp sftp = null;  
    private static final  String linuxFileSeparator = "/";
    
    public SFTPUtil(){}
    
    public SFTPUtil(String host, String username, String password, int port){
    this.host = host;
    this.username = username;
    this.password = password;
    this.port = port;
    }
    /** 
     * connect server via sftp 
     */  
    public void connect() {  
        try {  
            if(sftp != null){  
                System.out.println("sftp is not null");  
            }  
            JSch jsch = new JSch();  
            jsch.getSession(username, host, port);  
            Session sshSession = jsch.getSession(username, host, port);  
            System.out.println("Session created.");  
            sshSession.setPassword(password);  
            Properties sshConfig = new Properties();  
            sshConfig.put("StrictHostKeyChecking", "no");  
            sshSession.setConfig(sshConfig);  
            sshSession.connect();  
            sshSession.setTimeout(1000*60*10);
            System.out.println("Session connected.");  
            System.out.println("Opening Channel.");  
            Channel channel = sshSession.openChannel("sftp");  
            channel.connect();  
            sftp = (ChannelSftp) channel;  
            System.out.println("Connected to " + host + ".");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    /** 
     * Disconnect with server 
     */  
    public void disconnect() {  
        if(this.sftp != null){  
            if(this.sftp.isConnected()){  
                this.sftp.disconnect();  
            }else if(this.sftp.isClosed()){  
                System.out.println("sftp is closed already");  
            }  
        }  
  
    }  
  
      
    public void download(String remoteDirectory, String remoteDownloadFileName, String localSaveFilePath) {  
        try {  
            this.sftp.cd(remoteDirectory);  
            File file = new File(localSaveFilePath);  
            this.sftp.get(remoteDownloadFileName, new FileOutputStream(file)); 
            System.out.println("download remote file[" + remoteDirectory + linuxFileSeparator + remoteDownloadFileName + "] to local " + localSaveFilePath);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    /** 
     * upload one file to the server 
     */  
    public void upload(String localFilePath, String remoteFileDirectory) {  
        try {  
            File file = new File(localFilePath);  
              
            if(file.isFile()){  
                System.out.println("localFile : " + file.getAbsolutePath());  
                System.out.println("remotePath:" + remoteFileDirectory);  
                this.sftp.cd(remoteFileDirectory);
                this.sftp.put(new FileInputStream(file), file.getName());  
                System.out.println("=========upload file for " + localFilePath);  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (SftpException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * @return the host 
     */  
    public String getHost() {  
        return host;  
    }  
  
    /** 
     * @param host the host to set 
     */  
    public void setHost(String host) {  
        this.host = host;  
    }  
  
    /** 
     * @return the username 
     */  
    public String getUsername() {  
        return username;  
    }  
  
    /** 
     * @param username the username to set 
     */  
    public void setUsername(String username) {  
        this.username = username;  
    }  
  
    /** 
     * @return the password 
     */  
    public String getPassword() {  
        return password;  
    }  
  
    /** 
     * @param password the password to set 
     */  
    public void setPassword(String password) {  
        this.password = password;  
    }  
  
    /** 
     * @return the port 
     */  
    public int getPort() {  
        return port;  
    }  
  
    /** 
     * @param port the port to set 
     */  
    public void setPort(int port) {  
        this.port = port;  
    }  
  
    /** 
     * @return the sftp 
     */  
    public ChannelSftp getSftp() {  
        return sftp;  
    }  
  
    /** 
     * @param sftp the sftp to set 
     */  
    public void setSftp(ChannelSftp sftp) {  
        this.sftp = sftp;  
    }  
  
      
    public static void main(String[] args) {  
        SFTPUtil ftp= new SFTPUtil();  
        ftp.connect();  
        ftp.upload("c:\\RpStatisticsAmountVO.java", "/home/weblogic/job");  
        ftp.download("/home/weblogic/job", "RpStatisticsAmountVO.java", "d:\\RpStatisticsAmountVO.java");
        ftp.disconnect();  
    }  
  
  

}  



项目下载地址:http://download.csdn.net/download/qq_14946627/9282783

0 0