sftp 文件上傳 跨服務器 java


最近公司搞服務器遷移想把大量圖片搞一個文件服務器 後續上傳圖片就採用sftp協議方式上傳  參考了一篇blog  

http://blog.csdn.net/haidage/article/details/6859716          研究了一天總算搞出來了  

package baojia.util;

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 SftpClientUtil {
	private String host = "xxx.xx.xxx.xxx";  
    private String username="XXXXXXXXX";  
    private String password="********";  
    private int port = 2246;  
    private ChannelSftp sftp = null;  
    private String localPath = "D:/image/";  
    private String remotePath = "/home/zhanghua/data";  
    private String fileListPath = "D:/image/"; 
    private final String seperator = "/"; 
    /** 
     * 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();  
            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");  
            }  
        }  
  
    }  
 
    /** 
     * upload all the files to the server 
     */  
    public void upload(String path,String fileName) {  
     //   List<String> fileList = this.getFileEntryList(fileListPath); 
    	this.localPath=path;
        try {  
           
                    String localFile = this.localPath + this.seperator+ fileName;  
                    File file = new File(localFile);  
                      
                    if(file.isFile()){  
                        System.out.println("localFile : " + file.getAbsolutePath());  
                        String remoteFile = this.remotePath + this.seperator + fileName;  
                        System.out.println("remotePath:" + remoteFile);  
                        this.sftp.put(new FileInputStream(file), remoteFile);  
                        System.out.println("=========upload down for " + localFile);  
                    }  
          
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SftpException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }         
    }    
    
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章