通过JSch编写上传、下载文件
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编写上传、下载文件的更多相关文章
- 【WCF】利用WCF实现上传下载文件服务
引言 前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...
- Jmeter 上传下载文件
最近很多同学都在问jmeter上传.下载文件的脚本怎么做,要压测上传.下载文件的功能,脚本怎么做,网上查了都说的很含糊,这次呢,咱们就好好的把jmeter的上传下载文件好好缕缕,都整明白了,怎么个过程 ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- SFTP远程连接服务器上传下载文件-qt4.8.0-vs2010编译器-项目实例
本项目仅测试远程连接服务器,支持上传,下载文件,更多功能开发请看API自行开发. 环境:win7系统,Qt4.8.0版本,vs2010编译器 qt4.8.0-vs2010编译器项目实例下载地址:CSD ...
- linux下常用FTP命令 上传下载文件【转】
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- C#实现http协议支持上传下载文件的GET、POST请求
C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...
- HttpClient上传下载文件
HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...
- 初级版python登录验证,上传下载文件加MD5文件校验
服务器端程序 import socket import json import struct import hashlib import os def md5_code(usr, pwd): ret ...
随机推荐
- DOG检测
共享一个代码算了,太忙鸟,有时间在补充. function [] = dog_learn() img = imread('/Users/img/lena.png'); img = rgb2gray(i ...
- selenium 等待页面加载完成
一.隐形加载等待: file:///C:/Users/leixiaoj/Desktop/test.html 该页面负责创建一个div <html> <head> <tit ...
- mybatis : trim标签, “等于==”经验, CDATA标签 ,模糊查询CONCAT,LIKE
一.My Batis trim标签有点类似于replace效果. trim 属性, prefix:前缀覆盖并增加其内容 suffix:后缀覆盖并增加其内容 prefixOverrides:前缀判断的条 ...
- Linux下安装 MySQL
Ubuntu环境 使用二进制安装包安装,相对简单绿色 1.到官网下载二进制压缩包http://dev.mysql.com/downloads/mysql/ 2.选择需要的版本 目前最新为5.7.之后选 ...
- vtkTubeFilter实例
filter that generates tubes around lines vtkTubeFilter is a filter that generates a tube around each ...
- jpeg huffman coding table
亮度DC系数的取值范围及序号: 序号(size) 取值范围 0 0 1 - ...
- STM32f10xxx 之 GPIO口配置
背景 配置stm32f103使其完成PWM输出的过程中,在配置GPIO口的时候,按照习惯配置GPIO口的speed为50MHZ,突然就意识到,为什么大部分例程习惯配置为50MHZ,而不是其它值,即有了 ...
- mysql_config 问题
1 .you should have mysql_config available in $PATH For CentOS: yum install mysql-devel For openSUSE: ...
- ecshop 秒杀并发时库存会被减到小于0的解决办法
ecshop 秒杀并发时库存会被减到小于0更新库存后,再进行库存检查,如果库存为负数,则执行事务的回滚. begin();//开始一个事物处理开始 $sql = "UPDATE " ...
- MVC路由配置例
public static void RegisterRoutes(RouteCollection routes) { string suffix = string.Empty; routes.Ign ...