import java.io.InputStream;
import org.apache.log4j.Logger;
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;
public class SFTPHandler {
protected Logger logger = YTLog.getLogger(this.getClass());
private String username;
private String identity;
private String host;
private int port;
private Session sshSession = null;
private ChannelSftp sftp = null;
public ChannelSftp getSftp() {
return sftp;
}
public SFTPHandler(String host, int port, String username, String identity) {
this.username = username;
this.identity = identity;
this.host = host;
this.port = port;
}
public boolean connect() {
try {
JSch jsch = new JSch();
sshSession = jsch.getSession(username, host, port);
if (null == sshSession) {
logger.info("session is null");
return false;
}
sshSession.setPassword(identity);
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect(30000);
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
logger.info(e);
return false;
}
return true;
}
public InputStream get(String src){
try {
logger.info("get(src)========"+src);
return sftp.get(src);
} catch (SftpException e) {
logger.info(e);
}
return null;
}
public boolean put(String src, String dst) {
try {
logger.info("src========"+src+"dst===================="+dst);
sftp.put(src, dst);
sftp.disconnect();
return true;
} catch (SftpException e) {
logger.info(e);
return false;
}
}
public boolean putStream(InputStream src,String dst){
try {
logger.info("src========"+src+"dst===================="+dst);
sftp.put(src, dst);
sftp.disconnect();
return true;
} catch (SftpException e) {
logger.info(e);
return false;
}
}
public void disconnect() {
if (sftp != null) {
sftp.disconnect();
}
if (sshSession != null) {
sshSession.disconnect();
}
}
}
package cn.com.yitong.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import cn.com.yitong.framework.servlet.ServerInit;
public class SFTPUtil {
private final static String host = ServerInit.getString("SFTP_DOWN_HOST");
private final static String username = ServerInit.getString("SFTP_DOWN_USER_NAME");
private final static String password = ServerInit.getString("SFTP_DOWN_USER_PW");
private final static int port = ServerInit.getInt("SFTP_DOWN_PORT");
private static SFTPHandler handler = null;
private final static String upload = ServerInit.getString("SFTP_UPLOAD_FILE_PATH");
/**
* connect server via sftp
*/
public static boolean connect() {
handler = new SFTPHandler(host, port, username, password);
return handler.connect();
}
public static String downBatch(String direct, String fileName) {
// 返回文件string
String line = null;
StringBuffer sb = new StringBuffer();
BufferedReader bf = null;
InputStream is = handler.get(direct + "/" + fileName);
try {
if(null == is){
return "";
}
bf = new BufferedReader(new InputStreamReader(is, "GBK"));
while ((line = bf.readLine()) != null) {
sb.append(line).append("|#|");
}
return sb.toString();
} catch (IOException e) {
return "";
} finally {
handler.disconnect();
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
}
}
}
}
public static String downReport(String direct, String fileName) {
// 返回文件string
String line = null;
StringBuffer sb = new StringBuffer();
BufferedReader bf = null;
InputStream is = handler.get(direct + "/" + fileName);
try {
if(null == is){
return "";
}
bf = new BufferedReader(new InputStreamReader(is, "GBK"));
while ((line = bf.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
return "";
} finally {
handler.disconnect();
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
}
}
}
}
public static boolean downFile(String dir,String direct, String fileName) {
// 返回文件string
String line = null;
StringBuffer sb = new StringBuffer();
BufferedReader bf = null;
InputStream is = handler.get(direct + "/" + fileName);
File file = new File(dir);
if(!file.exists()){
file.mkdir();
}
file = new File(dir,fileName);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"));
bf = new BufferedReader(new InputStreamReader(is, "GBK"));
while ((line = bf.readLine()) != null) {
bw.write(line);
}
return true;
} catch (IOException e) {
return false;
} finally {
handler.disconnect();
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
}
}
}
}
/*
* 征信调查报告html文件生成图片
*/
public static void downCredit(String url) throws Exception{
try {
HtmlToImage.htmlToPng(url);
} catch (Exception e) {
throw new Exception();
} finally {
handler.disconnect();
}
}
/**
* upload all the files to the server
*/
public static boolean upload(String src, String dst) {
return handler.put(src, dst);
}
/**
* upload all the files to the server
*/
public static boolean uploadStream(String fileName, InputStream in) {
return handler.putStream(in, upload + "/" + fileName);
}
}