SFTP上传文件到服务器 工具类(新)

package com.tellhow.oms.webservice.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;

public class SFTPUtil {

/**
 * SFTP上传文件
 * 
 * @param file 上传文件
 * @return E001 访问SFTP服务器错误
 *         E002 SFTP服务器目录或文件不存在
 *         E003 读取文件异常
 */
public static String setSftpFile(File file) {
	
	String retStr = "";

	Session sshSession = null;

	// 加载配置文件
	PropertyUtil proper = new PropertyUtil();
	// IP地址
	String ipAdress = proper.getProperty("FTP_IP");
	// 访问端口
	String port = proper.getProperty("FTP_PORT");
	// 访问用户名
	String sftpUser = proper.getProperty("FTP_USER");
	// 访问密码
	final String sftpPw = proper.getProperty("FTP_PW");
	// 访问路径
	String sftpPath = proper.getProperty("FTP_PATH");
	// 端口转换成整型
	int iPort = Integer.parseInt(port);

	JSch jsch = new JSch();
	try {
		// 设置用户和地址
		sshSession = jsch.getSession(sftpUser, ipAdress);
		// 设置密码
		sshSession.setPassword(sftpPw);
		// 设置端口
		sshSession.setPort(iPort);

		sshSession.setUserInfo(new UserInfo() {

			public String getPassphrase() {
				return null;
			}

			public String getPassword() {
				return sftpPw;
			}

			public boolean promptPassphrase(String arg0) {
				return true;
			}

			public boolean promptPassword(String arg0) {
				return true;
			}

			public boolean promptYesNo(String arg0) {
				return true;
			}

			public void showMessage(String arg0) {

			}
		});

		sshSession.setTimeout(60000);// 连接的Timeout时间
		sshSession.connect();
		// 创建sftp通信通道
		Channel channel = sshSession.openChannel("sftp");
		channel.connect();
		System.out.println("已经连接上SFTP服务器...");
		ChannelSftp sftp = (ChannelSftp) channel;
		// 进入服务器指定的文件夹
		sftp.cd(sftpPath);
		// 输出流
		OutputStream outstream = sftp.put(file.getName());
		// 输入流
		InputStream instream = new FileInputStream(file);
		byte b[] = new byte[1024];
		int n;
		while ((n = instream.read(b)) != -1) {
			outstream.write(b, 0, n);
		}
		 outstream.flush();
		 outstream.close();
		 instream.close();
		 System.out.println(file.getName() + "上传SFTP服务器成功");
		 channel.disconnect();
		 
		 retStr = "OK";
		 return retStr;
	} catch (Exception e) {
		retStr = e.getMessage();
		e.printStackTrace();
	} finally {
		if(sshSession != null) {
			sshSession.disconnect();
			System.out.println("已经断开SFTP服务器...");
		}
	}
	return retStr;
}

/**
 * SFTP下载文件
 * 
 * @param file 接收文件
 * @return E001 访问SFTP服务器错误
 *         E002 SFTP服务器目录或文件不存在
 *         E003 读取文件异常
 */
public static boolean getSftpFile(File file) {

	Session sshSession = null;

	// 加载配置文件
	PropertyUtil proper = new PropertyUtil();
	// IP地址
	String ipAdress = proper.getProperty("FTP_IP");
	// 访问端口
	String port = proper.getProperty("FTP_PORT");
	// 访问用户名
	String sftpUser = proper.getProperty("FTP_USER");
	// 访问密码
	final String sftpPw = proper.getProperty("FTP_PW");
	// 访问路径
	String sftpPath = proper.getProperty("FTP_PATH");
	// 端口转换成整型
	int iPort = Integer.parseInt(port);

	JSch jsch = new JSch();
	try {
		// 设置用户和地址
		sshSession = jsch.getSession(sftpUser, ipAdress);
		// 设置密码
		sshSession.setPassword(sftpPw);
		// 设置端口
		sshSession.setPort(iPort);

		sshSession.setUserInfo(new UserInfo() {

			public String getPassphrase() {
				return null;
			}

			public String getPassword() {
				return sftpPw;
			}

			public boolean promptPassphrase(String arg0) {
				return true;
			}

			public boolean promptPassword(String arg0) {
				return true;
			}

			public boolean promptYesNo(String arg0) {
				return true;
			}

			public void showMessage(String arg0) {

			}
		});

		sshSession.setTimeout(60000);// 连接的Timeout时间
		sshSession.connect();
		// 创建sftp通信通道
		Channel channel = sshSession.openChannel("sftp");
		channel.connect();
		System.out.println("已经连接上SFTP服务器...");
		ChannelSftp sftp = (ChannelSftp) channel;
		// 进入服务器指定的文件夹
		sftp.cd(sftpPath);

		// 下载
		InputStream instream = sftp.get(file.getName());
		OutputStream outstream = new FileOutputStream(file);
		byte b[] = new byte[1024];
		int n;
		while ((n = instream.read(b)) != -1) {
			outstream.write(b, 0, n);
		}
		outstream.flush();
		outstream.close();
		instream.close();
		channel.disconnect();
		System.out.println(file.getName() + "下载成功");
		return true;
	} catch (JSchException e) {
		e.printStackTrace();
	} catch (SftpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if(sshSession != null) {
			sshSession.disconnect();
			System.out.println("已经断开SFTP服务器...");
		}
	}
	return false;
}

public static boolean setSftpFileGX(File file) {
	Session sshSession = null;

	// 加载配置文件
	PropertyUtilGX proper = new PropertyUtilGX();
	// IP地址
	String ipAdress = proper.getProperty("FTP_IP");
	// 访问端口
	String port = proper.getProperty("FTP_PORT");
	// 访问用户名
	String sftpUser = proper.getProperty("FTP_USER");
	// 访问密码
	final String sftpPw = proper.getProperty("FTP_PW");
	// 访问路径
	String sftpPath = proper.getProperty("FTP_PATH");
	// 端口转换成整型
	int iPort = Integer.parseInt(port);

	JSch jsch = new JSch();
	try {
		// 设置用户和地址
		sshSession = jsch.getSession(sftpUser, ipAdress);
		// 设置密码
		sshSession.setPassword(sftpPw);
		// 设置端口
		sshSession.setPort(iPort);

		sshSession.setUserInfo(new UserInfo() {

			public String getPassphrase() {
				return null;
			}

			public String getPassword() {
				return sftpPw;
			}

			public boolean promptPassphrase(String arg0) {
				return true;
			}

			public boolean promptPassword(String arg0) {
				return true;
			}

			public boolean promptYesNo(String arg0) {
				return true;
			}

			public void showMessage(String arg0) {

			}
		});

		sshSession.setTimeout(60000);// 连接的Timeout时间
		sshSession.connect();
		// 创建sftp通信通道
		Channel channel = sshSession.openChannel("sftp");
		channel.connect();
		System.out.println("已经连接上SFTP服务器...");
		ChannelSftp sftp = (ChannelSftp) channel;
		// 进入服务器指定的文件夹
		sftp.cd(sftpPath);
		// 输出流
		OutputStream outstream = sftp.put(file.getName());
		// 输入流
		InputStream instream = new FileInputStream(file);
		byte b[] = new byte[1024];
		int n;
		while ((n = instream.read(b)) != -1) {
			outstream.write(b, 0, n);
		}
		 outstream.flush();
		 outstream.close();
		 instream.close();
		 System.out.println(file.getName() + "上传SFTP服务器成功");
		 channel.disconnect();
		 return true;
	} catch (JSchException e) {
		e.printStackTrace();
	} catch (SftpException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if(sshSession != null) {
			sshSession.disconnect();
			System.out.println("已经断开SFTP服务器...");
		}
	}
	return false;
}

}

屁!!
新公司用框架就好了,晕
// 把文件传到文件服务器上

				FileInputStream fis = new FileInputStream(realSavePath + File.separator + saveFilename);
				BufferedInputStream bis = new BufferedInputStream(fis);
				
				this.collectAgent.addOrUpdateFile(sche_attachId, bis);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值