通过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 ...
随机推荐
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- javascript基本对象
1 String对象 创建对象 var string1 = new String("Hello"); var string2 = "Hello" //也可以创建 ...
- javascript的console.log用法
f1.html代码 <iframe id="frame2" name="frame1" src="ww.html"></i ...
- Eclipse换背景色
上班后,长时间看代码,眼睛感觉有些疲惫,就想想如果能换个肤色就好了,于是在网上搜了一下,果真Eclipse提供了这个方面功能,心情小激动, 顿时感觉萌萌哒,于是乐呵呵的把肤色改了.在这感谢网上的亲们, ...
- 我的第一个wcf
vs2012中新建一个解决方案 新建WCF项目RestApi 添加实体类 [DataContract] public class Employee { private Guid id; private ...
- 【译】使用 CocoaPods 模块化iOS应用
原文翻译自:Using CocoaPods to Modularize a Big iOS App 为你的移动应用选择正确的架构是一件相当大的事情,这会对你的工作流程造成影响,陷入面对的问题,可能是一 ...
- 我们平时是怎么写html和css的?
文章的起因,我只是为了回复一个帖子,http://bbs.csdn.net/topics/390908928?page=1 结果,一扯就根本停不下来.索性,一捅为快,反正是周末. 拿到效果图时,有这么 ...
- DI中Transient Scoped Singleton Instance的区别
Observe which of the OperationId values varies within a request, and between requests. Transient obj ...
- ps命令
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- 全文检索解决方案(lucene工具类以及sphinx相关资料)
介绍两种全文检索的技术. 1. lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...