SFPT上传文档
sftp上传文档有两种形式 :
1、通过路径上传,直接读取目标路径数据上传到sftp服务器指定路径下,需要传入源文件路径和目标位置路径
2、通过流的方式上传,获取到文件流和需要存放的目录即可;
sftp上传文档需要三个步骤
1、建立连接
2、上传文档
3、关闭连接
1、建立连接
public static JschDTO connectCftp( SftpConnectDTO connectDTO){
//服务ID地址
String host = connectDTO.getHost();
//用户名
String user = connectDTO.getUsername();
//密码
String pass = connectDTO.getPassword();
//端口号
int port = connectDTO.getPort();
//创建服务连接对象
JSch jSch = new JSch();
Session session = null;
ChannelSftp channelSftp = null;
try {
logger.info("链接SFTP服务器,username{},IP{},port{}", user, host, port);
//使用端口号地址和用户信息建立会话
session = jSch.getSession(user, host, port);
session.setPassword(pass);
//设置连接参数
Properties properties = new Properties();
properties.setProperty("StrictHostKeyChecking", "no");
session.setConfig(properties);
//建立连接
session.connect();
//选择要使用的服务打开sftp通道
channelSftp = (ChannelSftp) session.openChannel(SFTP);
//建立通道连接
channelSftp.connect();
}catch (Exception e){
throw new BusinessException("sftp服务器链接异常");
}
return new JschDTO(session,channelSftp);
}
··
2、上传文档和关闭服务
··
/**
* 从共享磁盘上传到sftp服务器
* @param sourcePath
* @param targetPath
*/
public static void pushFileToSftp(String sourcePath, String targetPath, SftpConnectDTO connectDTO){
JschDTO jschDTO = null;
ChannelSftp channelSftp = null;
try {
//建立连接
jschDTO = connectCftp(connectDTO);
channelSftp = jschDTO.getChannelSftp();
//上传文档
channelSftp.put(sourcePath, targetPath);
}catch (Exception e){
logger.error("文件上传sftp失败:"+e.getMessage());
throw new BusinessException("文件上传sftp失败"+e.getMessage());
}finally {
//关闭通道和会话
if (channelSftp!=null){
channelSftp.disconnect();
}
if (jschDTO!=null&&jschDTO.getSession()!=null){
jschDTO.getSession().disconnect();
}
}
}
3、使用流的方式上传文档
/**
* 直接想数据写进sftp服务器
* @param targetPath
*/
public static void pushFileToSftp(byte[] fileData, String targetPath,
SftpConnectDTO connectDTO){
JschDTO jschDTO = null;
ChannelSftp channelSftp = null;
try {
jschDTO = connectCftp(connectDTO);
channelSftp = jschDTO.getChannelSftp();
InputStream is = new ByteArrayInputStream(fileData);
channelSftp.put(is, targetPath);
}catch (Exception e){
logger.error("文件上传sftp失败"+e.getMessage());
throw new BusinessException("文件上传sftp失败"+e.getMessage());
}finally {
if (channelSftp!=null){
channelSftp.disconnect();
}
if (jschDTO!=null&&jschDTO.getSession()!=null){
jschDTO.getSession().disconnect();
}
}
}
4、从sftp服务器上拉去文件
public static byte[] pullFileFromSftp(String sourcePath,SftpConnectDTO connectDTO){
JschDTO jschDTO = null;
ChannelSftp channelSftp = null;
try {
jschDTO = connectCftp(connectDTO);
channelSftp = jschDTO.getChannelSftp();
try {
//验证文件是不是存在
SftpATTRS lstat = channelSftp.lstat(sourcePath);
if (lstat==null){
logger.error("文件路径不存在,sourcePath={}",sourcePath);
return null;
}
}catch (SftpException sftpException){
logger.error("文件路径不存在,sourcePath={}",sourcePath);
return null;
}
//获取文件数据
InputStream inputStream = channelSftp.get(sourcePath);
byte[] bytes = IOUtil.readFile(inputStream);
return bytes;
}catch (Exception e){
logger.error("文件下载失败:sourcePath={}"+e.getMessage(),sourcePath);
throw new BusinessException("文件下载失败:"+e.getMessage());
}finally {
if (channelSftp!=null){
channelSftp.disconnect();
}
if (jschDTO!=null&&jschDTO.getSession()!=null){
jschDTO.getSession().disconnect();
}
}
}