通过JSch编写上传、下载文件

package com.hct.util;

/**
 * @作者: HCT
 * @时间:2016年12月29日下午3:13:20
 * @描述:
 *
 */
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jcraft.jsch.*;

public class UpAndDownFileSftp {
	/**
	 * 利用JSch包实现SFTP下载、上传文件(用户名密码方式登陆)
	 * @param ip 主机IP
	 * @param user 主机登陆用户名
	 * @param psw  主机登陆密码
	 * @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1
	 * 
	 */
	public Session connect(String ip, int port,String user, String psw ) throws Exception{
		System.out.println("开始用户名密码方式登陆");
		Session session = null;
		
		JSch jsch = new JSch();
		
		if(port <=0){
			//连接服务器,采用默认端口
			session = jsch.getSession(user, ip);
		}else{
			//采用指定的端口连接服务器
			session = jsch.getSession(user, ip ,port);
		}

		//如果服务器连接不上,则抛出异常
		if (session == null) {
			throw new Exception("session is null");
		}
		
		//设置登陆主机的密码
		session.setPassword(psw);//设置密码   
		//设置第一次登陆的时候提示,可选值:(ask | yes | no)
		session.setConfig("StrictHostKeyChecking", "no");
		//设置登陆超时时间   
		session.connect(30000);
		
		return session;
			
	}
	
	/**  
	* @Title: sftpUpLoadFile  
	* @Description: 上傳指定目錄下面的指定文件到遠程指定目錄  
	* @param uploadFileName----上傳到遠程指定文件夾下面文件的名字,eg,uploadFileName="trade_balance_file_20161220_301.txt"; 
	* @param uploadfilepath----上傳到遠程指定文件夾名,eg,uploadfilepath="/alidata1/6080/share/20161222/301";
	* @param uploadfile----要從本地什麼文件夾及文件名上傳,eg,uploadfile="C:/Users/hechangting/Desktop/file/trade_balance_file_20161220_301.txt";
	* @return void    返回类型  
	* @throws  
	*/
	public  void sftpUpLoadFile(Session session, String uploadFileName,String uploadfilepath,String uploadfile) throws Exception {
		Channel channel = null;
		try {
			//创建sftp通信通道
			channel = (Channel) session.openChannel("sftp");
			channel.connect(1000);
			ChannelSftp sftp = (ChannelSftp) channel;
			
			
			//进入服务器指定的文件夹
			sftp.cd(uploadfilepath);
			
			//列出服务器指定的文件列表
			Vector v = sftp.ls("*.txt");
			for(int i=0;i<v.size();i++){
				System.out.println(v.get(i));
			}
			
			//以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
			OutputStream outstream = sftp.put(uploadFileName);
			InputStream instream = new FileInputStream(new File(uploadfile));
			
			byte b[] = new byte[1024];
			int n;
		    while ((n = instream.read(b)) != -1) {
		    	outstream.write(b, 0, n);
		    }
		    System.out.println("上傳文件==="+uploadFileName+"成功");
		    outstream.flush();
		    outstream.close();
		    instream.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.disconnect();
			channel.disconnect();
		}
	}
	
	/**  
	* @Title: sftpDownLoadFile  
	* @Description: 下載指定目錄下面的指定文件 
	* @param downloadFileName----要把文件下載到本地什麼地方、名字叫什麼,eg,downloadFileName="C:/Users/hechangting/Desktop/file/hhhhh.txt"; 
	* @param downloadfilepath----要從遠程什麼目錄下面下載文件,eg,downloadfilepath="/alidata1/6080/share/20161222/301"; 
	* @param downloadfile----要從遠程什麼目錄下面下載的文件的名字,eg,downloadfile="redemption_balance_confirm_file_20161222_301.txt";
	* @return void    返回类型  
	* @throws  
	*/
	public void sftpDownLoadFile(Session session, String downloadFileName,
			String downloadfilepath, String downloadfile) throws Exception {
		Channel channel = null;
		try {
			// 创建sftp通信通道
			channel = (Channel) session.openChannel("sftp");
			channel.connect(1000);
			ChannelSftp sftp = (ChannelSftp) channel;

			// 进入服务器指定的文件夹
			sftp.cd(downloadfilepath);

			// 列出服务器指定的文件列表
			Vector v = sftp.ls("*.txt");
			for (int i = 0; i < v.size(); i++) {
				System.out.println(v.get(i));
			}

			// 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
			InputStream instream = sftp.get(downloadfile);
			OutputStream outstream = new FileOutputStream(new File(
					downloadFileName));

			byte b[] = new byte[1024];
			int n;
			while ((n = instream.read(b)) != -1) {
				outstream.write(b, 0, n);
			}
			System.out.println("下載文件" + downloadfile + "成功!");
			outstream.flush();
			outstream.close();
			instream.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.disconnect();
			channel.disconnect();
		}
	}
}

  

测试代码

package com.hct.util;

import com.jcraft.jsch.Session;

/**
 * @作者: HCT
 * @时间:2016年12月29日下午3:49:25
 * @描述:
 *
 */
public class TestUPandDownFile {

	/**  
	 * @Title: main  
	 * @Description: TODO(这里用一句话描述这个方法的作用)  
	 * @param 参数描述  
	 * @return void    返回类型  
	 * @throws  
	 */
	public static void main(String[] args) throws Exception {
		String ip="10.139.108.102";
		int port=22;
		String user="6080";
		String psw="6080";
		Session session = null;
		String uploadFileName="trade_balance_file_20161220_301.txt";
		UpAndDownFileSftp upanddownfile = new UpAndDownFileSftp();
		session = upanddownfile.connect(ip, port, user, psw);
		
		String uploadfilepath="/alidata1/6080/share/20161222/301";
		String uploadfile="C:/Users/hechangting/Desktop/file/trade_balance_file_20161220_301.txt";
//		upanddownfile.sftpUpLoadFile(session, uploadFileName, uploadfilepath, uploadfile);
		
		
		
		String downloadFileName="C:/Users/hechangting/Desktop/file/hhhhh.txt";
		String downloadfilepath="/alidata1/6080/share/20161222/301/";
		String downloadfile="redemption_balance_confirm_file_20161222_301.txt";
//		upanddownfile.sftpDownLoadFile(session, downloadFileName, downloadfilepath, downloadfile);
		
		String downloadDirectorypath="/alidata1/6080/share/20161222/301/";
		String downloadLocalDirectorypath="C:/Users/hechangting/Desktop/file/";
		upanddownfile.sftpDownLoadDirectory(session, downloadDirectorypath, downloadLocalDirectorypath);
		
	}

}

  

原文地址:https://www.cnblogs.com/HCT118/p/6234525.html