SFTP工具类

package cn.com.yitong.util;


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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值