/*
* Project: book
*
* File Created at 2017年9月27日
*
* Copyright 2016 CMCC Corporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* ZYHY Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license.
*/
package com.whb.book.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.servlet.http.HttpServletResponse;
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.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
/**
* @Type SFTPUtil.java
* @Desc
* @author wanghb
* @date 2017年9月27日 下午7:12:01
* @version
*/
public class SFTPUtil {
public static final String CHARSET = "utf-8";
public static final String PROPERTY = System.getProperty("line.separator");
private String host;//服务器连接ip
private String username;//用户名
private String password;//密码
private int port = 22;//端口号
private ChannelSftp sftp = null;
private Session sshSession = null;
public SFTPUtil(String host, String username, String password, int port) {
super();
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
public SFTPUtil(String host, String username, String password) {
super();
this.host = host;
this.username = username;
this.password = password;
}
/**
* 连接sftp
*/
public void connect() {
JSch jsch = new JSch();
try {
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
throw new RuntimeException("连接SFTP失败");
}
}
/**
* 断开sftp
*/
public void disconnect() {
if (this.sftp != null && this.sftp.isConnected()) {
this.sftp.disconnect();
}
if (this.sshSession != null && this.sshSession.isConnected()) {
this.sshSession.disconnect();
}
}
/**
* 下载文件到本地
*
* @author whb
* @param remotePath
* @param remoteFileName
* @param localPath
* @param localName
* @return
* @since JDK 1.8
*/
public void downloadFile(String remotePath, String remoteFileName, String localPath,
String localName) {
try (InputStream is = sftp.get(remotePath + remoteFileName);
FileOutputStream fs = new FileOutputStream(new File(localPath + localName))) {
StringBuffer sb = new StringBuffer();
String buffer = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((buffer = reader.readLine()) != null) {
sb.append(buffer + PROPERTY);
}
fs.write(sb.toString().getBytes(CHARSET));
} catch (SftpException | IOException e) {
throw new RuntimeException("下载文件出错");
}
}
/**
* 下载文件到本地
*
* @author whb
* @param remotePath
* @param remoteFileName
* @param localPath
* @param localName
* @return 返回boolean类型
* @since JDK 1.8
*/
public boolean downloadFileBoolean(String remotePath, String remoteFileName, String localPath,
String localName) {
try (InputStream is = sftp.get(remotePath + remoteFileName);
FileOutputStream fs = new FileOutputStream(new File(localPath + localName))) {
StringBuffer sb = new StringBuffer();
String buffer = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((buffer = reader.readLine()) != null) {
sb.append(buffer + PROPERTY);
}
fs.write(sb.toString().getBytes(CHARSET));
return true;
} catch (SftpException | IOException e) {
return false;
}
}
/**
* 下载文件到浏览器
*
* @author whb
* @param remotePath
* @param remoteFileName
* @return
* @since JDK 1.8
*/
public void downloadFile(HttpServletResponse response, String remotePath, String remoteFileName) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(sftp.get(remotePath
+ remoteFileName)))) {
response.setHeader("content-disposition",
"attachment;filename=" + URLEncoder.encode(remoteFileName, CHARSET));
OutputStream out = response.getOutputStream();
StringBuffer sb = new StringBuffer();
String buffer = "";
while ((buffer = reader.readLine()) != null) {
sb.append(buffer + PROPERTY);
}
out.write(sb.toString().getBytes(CHARSET));
out.flush();
} catch (SftpException | IOException e) {
throw new RuntimeException(e);
}
}
/**
* 批量下载文件
* @param remotPath:远程下载目录(以路径符号结束,可以为相对路径eg:/home/download/)
* @param localPath:本地保存目录(以路径符号结束,D:\sftp\)
* @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
* @param fileEndFormat:下载文件格式(文件格式)
* @param del:下载后是否删除sftp文件
* @return
*/
@SuppressWarnings("rawtypes")
public List<String> batchDownLoadFile(String remotePath, String localPath, String fileFormat,
String fileEndFormat, boolean del) {
List<String> filenames = new ArrayList<String>();
Vector v = listFiles(remotePath);
if (v.size() > 0) {
System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
Iterator it = v.iterator();
while (it.hasNext()) {
LsEntry entry = (LsEntry) it.next();
String filename = entry.getFilename();
SftpATTRS attrs = entry.getAttrs();
if (!attrs.isDir()) {
boolean flag = false;
String localFileName = localPath + filename;
fileFormat = fileFormat == null ? "" : fileFormat.trim();
fileEndFormat = fileEndFormat == null ? "" : fileEndFormat.trim();
// 三种情况
if (fileFormat.length() > 0 && fileEndFormat.length() > 0) {
if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat)) {
flag = downloadFileBoolean(remotePath, filename, localPath, filename);
if (flag) {
filenames.add(localFileName);
if (flag && del) {