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