SFTP文件下载、上传
来源:互联网 发布:网络销售药品许可证 编辑:程序博客网 时间:2024/04/30 04:20
本篇文章介绍,通过SFTP,连接远程服务器,并从服务器上下载文件。
下载后可以打印,也可以保存到数据库。
用户需要自己手动下载 jsch包。
SFTP工具类
package com.huchi.common.utils.jsch;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Map;import java.util.Properties;import java.util.Vector;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;/** * @author : lzq * @group : huchi * @Date : 2015-2-26 上午10:13:30 * @Comments : 通过SFTP,读取数据 * @Version : 1.0.0 */public class SFTPChannel {Session session = null;Channel channel = null;private static final Logger LOG = Logger.getLogger(SFTPChannel.class);/** * @MethodName : getChannel * @Description : 得到Channel * @param sftpDetails * @param timeout * 超时时间 * @return * @throws JSchException */public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout)throws JSchException {String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);JSch jsch = new JSch(); // 创建JSch对象session = jsch.getSession(ftpUserName, ftpHost, Integer.parseInt(port)); // 根据用户名,主机ip,端口获取一个Session对象LOG.debug("Session created.");if (ftpPassword != null) {session.setPassword(ftpPassword); // 设置密码}Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config); // 为Session对象设置propertiessession.setTimeout(timeout); // 设置timeout时间session.connect(); // 通过Session建立链接LOG.debug("Session connected.");LOG.debug("Opening Channel.");channel = session.openChannel("sftp"); // 打开SFTP通道channel.connect(); // 建立SFTP通道的连接LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = "+ ftpUserName + ", returning: " + channel);return (ChannelSftp) channel;}/** * @MethodName : closeChannel * @Description : 关闭channel * @throws Exception */public void closeChannel() throws Exception {if (channel != null) {channel.disconnect();}if (session != null) {session.disconnect();}}/** * @MethodName : getSFTPChannel * @Description : 得到SFTPChannel 类实例对象 * @return */public SFTPChannel getSFTPChannel() {return new SFTPChannel();}/** * @MethodName: download * @Description: 下载文件 * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 存在本地的路径 * @param sftp */public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {try {sftp.cd(directory);File file = new File(saveFile);sftp.get(downloadFile, new FileOutputStream(file));} catch (Exception e) {e.printStackTrace();}}/** * @MethodName: upload * @Description: 上传文件 * @param directory 上传的目录 * @param uploadFile 要上传的文件 * @param sftp */public void upload(String directory, String uploadFile, ChannelSftp sftp) {try {sftp.cd(directory);File file = new File(uploadFile);sftp.put(new FileInputStream(file), file.getName());} catch (Exception e) {e.printStackTrace();}}/** * @MethodName: delete * @Description: 删除文件 * @param directory 要删除文件所在目录 * @param deleteFile 要删除的文件 * @param sftp */public void delete(String directory, String deleteFile, ChannelSftp sftp) {try {sftp.cd(directory);sftp.rm(deleteFile);} catch (Exception e) {e.printStackTrace();}}/** * @MethodName: listFiles * @Description: 列出目录下的文件 * @param directory 要列出的目录 * @param sftp sftp * @return * @throws SftpException */public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException {return sftp.ls(directory);}}
读取配置文件工具类
public class BackendUtils {/** * @MethodName: GetConfigProperty * @Description: 去读配置文件中的property属性 * @param fileName 配置文件名称 * @param propName 获取的属性 * @return propMap */public static Map<String, String> GetConfigProperty(String fileName,String... propName){Properties prop = new Properties(); Map<String, String> propMap = new HashMap<String, String>(); try { prop.load(BackendUtils.class.getClassLoader().getResourceAsStream(fileName)); for (String propery : propName) { propMap.put(propery, prop.getProperty(propery));} } catch(IOException e) { e.printStackTrace(); }return propMap; }}
测试
public static void main(String[] args) throws Exception {// 设置主机ip,端口,用户名,密码Map<String, String> sftpDetails = BackendUtils.GetConfigProperty("property/account.properties", SFTPConstants.SFTP_REQ_HOST,SFTPConstants.SFTP_REQ_USERNAME, SFTPConstants.SFTP_REQ_PASSWORD,SFTPConstants.SFTP_REQ_PORT, SFTPConstants.SFTP_PARTNER);SFTPChannel channel = new SFTPChannel();ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000);// 获取昨天Calendar cal = Calendar.getInstance();cal.add(Calendar.DATE, -1);String yesterday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); // 昨天//连连文件目录String directory = "/bjmx/"+sftpDetails.get(SFTPConstants.SFTP_PARTNER)+"/";//连连交易记录文件String downloadFile = "JYMX_"+sftpDetails.get(SFTPConstants.SFTP_PARTNER)+"_"+yesterday+".txt";//保存本地的文件全路径String saveFile = "D:\\lianTradeRecord\\JYMX_"+sftpDetails.get(SFTPConstants.SFTP_PARTNER)+"_"+yesterday+".txt";//下载文件channel.download(directory, downloadFile, saveFile, chSftp); //中文正则表达式String regEx = "[\\u4e00-\\u9fa5]"; Pattern p = Pattern.compile(regEx); try {BufferedReader br = new BufferedReader(new FileReader(saveFile));String str;while ((str = br.readLine()) != null) {Matcher m = p.matcher(str.substring(0, 1));if (m.find()) {continue;}String[] rows = str.split(",");for (String row : rows) {System.out.println(row);}}} catch (Exception e) {e.printStackTrace();} finally {chSftp.quit();channel.closeChannel();}}
系统常量
/** * @author : lzq * @group : huchi * @Date : 2015-2-27 下午2:28:46 * @Comments : sftp文件下载常量 * @Version : 1.0.0 */public class SFTPConstants {public static final String SFTP_REQ_HOST = "host"; //IP地址public static final String SFTP_REQ_PORT = "port"; //端口public static final String SFTP_REQ_USERNAME = "username"; public static final String SFTP_REQ_PASSWORD = "password";public static final String SFTP_PARTNER = "partner"; //商户号}
配置文件信息:account.properties
host=115.238.110.126username=rootpassword=root123port=20partner=5413165
df待续
0 0
- SFTP文件下载、上传
- SFTP文件上传和下载
- 基于sftp来上传和下载文件
- SFtp ssh2.0 version 上传、下载文件
- Java连接SFTP服务器下载上传文件
- sftp文件的上传与下载
- java中sftp文件上传与下载
- SFTP连接、上传、下载、删除文件
- 通过SHELL脚本实现SFTP文件自动上传与下载
- JSch:纯JAVA实现SFTP文件上传和下载
- JSch:纯JAVA实现SFTP文件上传和下载
- JSch:纯JAVA实现SFTP文件上传和下载
- 使用Renci.SshNet实现sftp文件上传和下载
- python ssh 公钥私钥 密码认证 sftp 服务器文件下载上传
- JSch:纯JAVA实现SFTP文件上传和下载
- JSch:纯JAVA实现SFTP文件上传和下载
- c#winforms程序 SFTP协议 上传/下载文件
- 使用sftp对文件进行上传或者是下载
- testng对失败时截图处理
- BM算法
- iTunes出现“iTunes不能读取iPhone的内容,请前往iPhone偏好设置的摘要选项卡,然后点击“恢复””的解决办法
- SQL Server 数据库备份命令
- Eclipse搭建SpringMVC遇到的问题过程
- SFTP文件下载、上传
- iOS中,MRC和ARC混编
- 关于NAND、NOR启动程序的具体走向
- 黑马程序员 -- OC语言 封装 继承 多态
- 关于对SMS_SDK v1.0.7短信验证码的学习
- 如何利用SVN合并代码
- Hbase Java Api 基本操作
- 指针数组和数组指针的区别
- WARNING:keystoneclient.httpclient:Failed to retrieve management_url from token