package com.hct.util;

/**
* @作者: HCT
* @时间:2016年12月29日下午3:13:20
* @描述:
*
*/
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import com.jcraft.jsch.*; public class UpAndDownFileSftp {
/**
* 利用JSch包实现SFTP下载、上传文件(用户名密码方式登陆)
* @param ip 主机IP
* @param user 主机登陆用户名
* @param psw 主机登陆密码
* @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1
*
*/
public Session connect(String ip, int port,String user, String psw ) throws Exception{
System.out.println("开始用户名密码方式登陆");
Session session = null; JSch jsch = new JSch(); if(port <=0){
//连接服务器,采用默认端口
session = jsch.getSession(user, ip);
}else{
//采用指定的端口连接服务器
session = jsch.getSession(user, ip ,port);
} //如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
} //设置登陆主机的密码
session.setPassword(psw);//设置密码
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
//设置登陆超时时间
session.connect(30000); return session; } /**
* @Title: sftpUpLoadFile
* @Description: 上傳指定目錄下面的指定文件到遠程指定目錄
* @param uploadFileName----上傳到遠程指定文件夾下面文件的名字,eg,uploadFileName="trade_balance_file_20161220_301.txt";
* @param uploadfilepath----上傳到遠程指定文件夾名,eg,uploadfilepath="/alidata1/6080/share/20161222/301";
* @param uploadfile----要從本地什麼文件夾及文件名上傳,eg,uploadfile="C:/Users/hechangting/Desktop/file/trade_balance_file_20161220_301.txt";
* @return void 返回类型
* @throws
*/
public void sftpUpLoadFile(Session session, String uploadFileName,String uploadfilepath,String uploadfile) throws Exception {
Channel channel = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel; //进入服务器指定的文件夹
sftp.cd(uploadfilepath); //列出服务器指定的文件列表
Vector v = sftp.ls("*.txt");
for(int i=0;i<v.size();i++){
System.out.println(v.get(i));
} //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
OutputStream outstream = sftp.put(uploadFileName);
InputStream instream = new FileInputStream(new File(uploadfile)); byte b[] = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
System.out.println("上傳文件==="+uploadFileName+"成功");
outstream.flush();
outstream.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
channel.disconnect();
}
} /**
* @Title: sftpDownLoadFile
* @Description: 下載指定目錄下面的指定文件
* @param downloadFileName----要把文件下載到本地什麼地方、名字叫什麼,eg,downloadFileName="C:/Users/hechangting/Desktop/file/hhhhh.txt";
* @param downloadfilepath----要從遠程什麼目錄下面下載文件,eg,downloadfilepath="/alidata1/6080/share/20161222/301";
* @param downloadfile----要從遠程什麼目錄下面下載的文件的名字,eg,downloadfile="redemption_balance_confirm_file_20161222_301.txt";
* @return void 返回类型
* @throws
*/
public void sftpDownLoadFile(Session session, String downloadFileName,
String downloadfilepath, String downloadfile) throws Exception {
Channel channel = null;
try {
// 创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel; // 进入服务器指定的文件夹
sftp.cd(downloadfilepath); // 列出服务器指定的文件列表
Vector v = sftp.ls("*.txt");
for (int i = 0; i < v.size(); i++) {
System.out.println(v.get(i));
} // 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
InputStream instream = sftp.get(downloadfile);
OutputStream outstream = new FileOutputStream(new File(
downloadFileName)); byte b[] = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
System.out.println("下載文件" + downloadfile + "成功!");
outstream.flush();
outstream.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
channel.disconnect();
}
}
}

  

测试代码

package com.hct.util;

import com.jcraft.jsch.Session;

/**
* @作者: HCT
* @时间:2016年12月29日下午3:49:25
* @描述:
*
*/
public class TestUPandDownFile { /**
* @Title: main
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param 参数描述
* @return void 返回类型
* @throws
*/
public static void main(String[] args) throws Exception {
String ip="10.139.108.102";
int port=22;
String user="6080";
String psw="6080";
Session session = null;
String uploadFileName="trade_balance_file_20161220_301.txt";
UpAndDownFileSftp upanddownfile = new UpAndDownFileSftp();
session = upanddownfile.connect(ip, port, user, psw); String uploadfilepath="/alidata1/6080/share/20161222/301";
String uploadfile="C:/Users/hechangting/Desktop/file/trade_balance_file_20161220_301.txt";
// upanddownfile.sftpUpLoadFile(session, uploadFileName, uploadfilepath, uploadfile); String downloadFileName="C:/Users/hechangting/Desktop/file/hhhhh.txt";
String downloadfilepath="/alidata1/6080/share/20161222/301/";
String downloadfile="redemption_balance_confirm_file_20161222_301.txt";
// upanddownfile.sftpDownLoadFile(session, downloadFileName, downloadfilepath, downloadfile); String downloadDirectorypath="/alidata1/6080/share/20161222/301/";
String downloadLocalDirectorypath="C:/Users/hechangting/Desktop/file/";
upanddownfile.sftpDownLoadDirectory(session, downloadDirectorypath, downloadLocalDirectorypath); } }

  

通过JSch编写上传、下载文件的更多相关文章

  1. 【WCF】利用WCF实现上传下载文件服务

    引言     前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...

  2. Jmeter 上传下载文件

    最近很多同学都在问jmeter上传.下载文件的脚本怎么做,要压测上传.下载文件的功能,脚本怎么做,网上查了都说的很含糊,这次呢,咱们就好好的把jmeter的上传下载文件好好缕缕,都整明白了,怎么个过程 ...

  3. rz和sz上传下载文件工具lrzsz

    ######################### rz和sz上传下载文件工具lrzsz ####################################################### ...

  4. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  5. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  6. SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例

    本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...

  7. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  8. C#实现http协议支持上传下载文件的GET、POST请求

    C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...

  9. HttpClient上传下载文件

    HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...

  10. 初级版python登录验证,上传下载文件加MD5文件校验

    服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...

随机推荐

  1. BZOJ2117: [2010国家集训队]Crash的旅游计划

    裸点分,点分树每层维护有序表,查询二分,复杂度$O(nlog^3n)$. #include<bits/stdc++.h> #define M (u+v>>1) #define ...

  2. New Plan!

    很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...

  3. Dom4J解析技术

    前面的话  本文主要讲解有关Dom4j技术和xpath配合下的优化!   目录:    为什么需要Dom4J    DOM4J怎么用    xpath怎么配合DOM4J 一  为什么需要Dom4J 一 ...

  4. SQL Server数据库定时自动备份

    SQL Server 数据库定时自动备份[转]   在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求 ...

  5. C++ 之namespace常见用法

    一.背景 需要使用Visual studio的C++,此篇对namespace的常用用法做个记录. 二.正文 namespace通常用来给类或者函数做个区间定义,以使编译器能准确定位到适合的类或者函数 ...

  6. Windows下将nginx安装为服务运行

    今天看到nginx这个小服务器软件正式版更新到了1.4.2,想玩下它.这个服务器软件虽小,但功能强大,是开源软件,有着良好的性能,被很多个人.企业,甚至大型企业所使用! 由于是在Windows下,所以 ...

  7. web 安全杂谈

    以前写过一篇关于session.cookie的博文,都是简单的介绍.不过session和cookie和网络安全可有着密切的关系. 今天主要从这几个方面总结下最近学到的东西: 1. session 两种 ...

  8. 错误:java.util.Map is an interface, and JAXB can't handle interfaces.

    问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...

  9. 排序算法总结第二弹----冒泡排序---javascript描述

    上篇博文总结了选择排序,这篇来看冒泡排序,接上篇. 冒泡排序思想:若是正再将一组数据升序排序, 第一趟:比较相邻的数据,当左侧值大于右侧值将他们进行交换,将较小值向前浮动,大值向后冒泡,直至比较到最后 ...

  10. 本地mysql打不开的解决方法

    今天打开本地mysql的数据库,却一直打开报错.错误代码是10061. 让同事过来一看,发现是自己的mysql服务器并未启动.右下角任务管理器的mysql服务器为红色未启动状态.