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;
	}
	 
}   



猜你喜欢

转载自blog.csdn.net/qq_30285985/article/details/75007714