1. 程式人生 > >SSH ftp 上傳下載

SSH ftp 上傳下載

通過Sftp 進行上傳下載

package com.bdsoft.ftp;

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

import com.bdsoft.service.TimeTaskFtp;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 * WRITER--:ZCJ 
 * EXPLAIN-:該方法是進行FTP伺服器的連線,檔案上傳,檔案下載的公共靜態方法使用時可以直接類名呼叫
 */
public class MessageSFtpTools {
	private static  ChannelSftp sftp;
	private static   Channel channel;
	private static   Session session;
	public static void downloadFile(String other_flag,String other_content,String my_flag,String my_content,String hostname,int port,String username,String password) throws JSchException, SftpException{
		GetSftp(hostname, username, password, port);
		Vector list = sftp.ls(other_flag); //獲取檔案
        if(list.size()>0)
        {
        	 Iterator it = list.iterator();
             while (it.hasNext())
             {
               LsEntry entry = (LsEntry) it.next();
               String filename = entry.getFilename();
               //判斷xml 結尾的檔案
               if(filename.endsWith("xml"))
               {
            	   TimeTaskFtp.logger.info("download "+filename+" to 介面機");
            	   sftp.get(other_flag+filename,my_flag);   //下載檔案
            	   sftp.rm(other_flag+filename);  //刪除
               }
             }
        }
        sftp.disconnect();
        channel.disconnect();
        session.disconnect();
	}
	public static void uploadFile(String my_up_flag,String my_up_content,String other_flag,String other_up_content,String hostname,int port ,String username,String password) throws SocketException, IOException, JSchException, SftpException {
		 GetSftp(hostname, username, password, port);
		 File rootDir = new File(my_up_flag);
		 String[] fileList =  rootDir.list();
		 if(!rootDir.isDirectory()){
			 TimeTaskFtp.logger.info("資料夾 不存在"+rootDir.getAbsolutePath());
		 }else{
			 for (int i = 0; i < fileList.length; i++) {
				    String filename = rootDir.getAbsolutePath()+"/"+fileList[i];
				    TimeTaskFtp.logger.info("up "+filename+" to 簡訊伺服器");
				    File tempFile =new File(filename); 
				    sftp.put(filename,other_flag);  //上傳
					tempFile.delete();
				}
		 }
		  sftp.disconnect();
		channel.disconnect();
        session.disconnect();
	}
	/** 
	* 方法說明: 獲取連線
	* @return 
	*/
	public static void GetSftp(String hostname,String username,String password,int port) throws JSchException
	{
		String ftpHost = hostname;
        String ftpUserName = username; 
        String ftpPassword = password;
        JSch jsch = new JSch(); // 建立JSch物件
        session = jsch.getSession(ftpUserName, ftpHost,port); // 根據使用者名稱,主機ip,埠獲取一個Session物件
        if (ftpPassword != null) {
            session.setPassword(ftpPassword); // 設定密碼
        }
        session.setConfig("StrictHostKeyChecking", "no");
        session.setTimeout(300000); // 設定timeout時間
        session.connect(); // 通過Session建立連結
        channel= session.openChannel("sftp"); // 開啟SFTP通道
        channel.connect(); // 建立SFTP通道的連線
        sftp = (ChannelSftp) channel;
	}
	 
}