SftpUtil FTP文件上传
package ftputil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SftpUtil {
// private static final Logger LOGGER =
// LoggerFactory.getLogger(SftpUtil.class);
public static void upload(String host, int port, String username, String password, InputStream inputStream,
String remoteDirectory, String remoteFileName) throws Exception {
Sftp sftp = new Sftp();
sftp.getConnect(host, port, username, password);
sftp.upload(inputStream, remoteDirectory, remoteFileName);
sftp.disconnect();
}
public static void upload(String host, int port, String username, String password, String localFilePathName,
String remoteDirectory, String remoteFileName) throws Exception {
Sftp sftp = new Sftp();
sftp.getConnect(host, port, username, password);
File file = new File(localFilePathName);
sftp.upload(new FileInputStream(file), remoteDirectory, remoteFileName);
sftp.disconnect();
}
public static void uploadBatch(String host, int port, String username, String password,
List<String> localFilePathNameList, List<String> remoteDirectoryList, List<String> remoteFileNameList)
throws Exception {
if ((localFilePathNameList.size() != remoteDirectoryList.size())
|| (localFilePathNameList.size() != remoteFileNameList.size())) {
throw new RuntimeException("参数错误!本地文件list 和 远端目录list 以及 远端文件名 list 大小不一致。");
}
Sftp sftp = new Sftp();
sftp.getConnect(host, port, username, password);
for (int i = 0; i < localFilePathNameList.size(); ++i) {
String localFilePathName = (String) localFilePathNameList.get(i);
String remoteDirectory = (String) remoteDirectoryList.get(i);
String remoteFileName = (String) remoteFileNameList.get(i);
File file = new File(localFilePathName);
sftp.upload(new FileInputStream(file), remoteDirectory, remoteFileName);
}
sftp.disconnect();
}
public static void download(String host, int port, String username, String password, String remoteDirectory,
String remoteFileName, String localDirectorys, String localFileName) throws Exception {
Sftp sftp = new Sftp();
sftp.getConnect(host, port, username, password);
sftp.download(remoteDirectory, remoteFileName, localDirectorys, localFileName);
sftp.disconnect();
}
public static void delete(String host, int port, String username, String password, String remoteDirectory,
String remoteFileName) throws Exception {
Sftp sftp = new Sftp();
sftp.getConnect(host, port, username, password);
sftp.delete(remoteDirectory, remoteFileName);
sftp.disconnect();
}
public static void main(String[] args) throws Exception {
test();
}
private static void test() throws Exception {
long b = System.currentTimeMillis();
String host = "172.16.10.208";
short port = 22;
String username = "ftpuser";
String password = "123456";
String localFilePathName = "C:/Users/Administrator/Desktop/新建文本文档 (5).txt";
String remoteDirectory = "/home/test/";
String remoteFileName = "remoteFile_1.txt";
String localDirectorys = "C:/Users/Administrator/Desktop/";
String localFileName = "localFile_1.txt";
// upload(host, port, username, password, localFilePathName,
// remoteDirectory, remoteFileName);
download(host, port, username, password, remoteDirectory, remoteFileName, localDirectorys, localFileName);
long e = System.currentTimeMillis();
System.err.println("总耗时:" + (e - b) / 1000.0D + "秒");
}
private static class Sftp {
private static final Logger LOGGER = LoggerFactory.getLogger(Sftp.class);
private Session session;
private Channel channel;
private ChannelSftp sftp;
private Sftp() {
this.session = null;
this.channel = null;
this.sftp = null;
}
private void getConnect(String host, int port, String username, String password) throws Exception {
LOGGER.debug("开始创建sftp连接...");
JSch jsch = new JSch();
this.session = jsch.getSession(username, host, port);
this.session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
this.session.setConfig(config);
this.session.connect();
this.channel = this.session.openChannel("sftp");
this.channel.connect();
this.sftp = ((ChannelSftp) this.channel);
LOGGER.debug("创建sftp连接结束...");
}
private void upload(InputStream inputStream, String remoteDirectory, String remoteFileName) throws Exception {
this.sftp.cd("/");
try {
if ((!remoteDirectory.equals("")) && (remoteDirectory.trim() != "")) {
String[] dd = remoteDirectory.split("/");
for (String directory : dd) {
if (directory == null)
continue;
if ("".equals(directory.trim()))
continue;
try {
this.sftp.cd(directory);
} catch (SftpException sException) {
if (2 == sException.id) {
LOGGER.info("创建目录【{}】", new Object[] { directory });
this.sftp.mkdir(directory);
this.sftp.cd(directory);
}
}
}
}
this.sftp.put(inputStream, remoteFileName);
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
}
}
private void download(String remoteDirectory, String remoteFileName, String localDirectorys,
String localFileName) throws Exception {
try {
this.sftp.cd(remoteDirectory);
File file = new File(localDirectorys);
if (!file.exists()) {
file.mkdirs();
}
this.sftp.get(remoteFileName, new FileOutputStream(new File(localDirectorys, localFileName)));
} catch (Exception e) {
} finally {
disconnect();
}
}
private void delete(String directory, String deleteFile) throws Exception {
try {
this.sftp.cd(directory);
this.sftp.rm(deleteFile);
} catch (Exception e) {
} finally {
disconnect();
}
}
private void disconnect() throws Exception {
if (this.sftp != null) {
this.sftp.disconnect();
this.sftp.exit();
this.sftp = null;
}
if (this.channel != null) {
this.channel.disconnect();
this.channel = null;
}
if (this.session != null) {
this.session.disconnect();
this.session = null;
}
}
}
}
SftpUtil FTP文件上传的更多相关文章
- Java实现FTP文件上传与下载
实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式 package com.cl ...
- java/struts/Servlet文件下载与ftp文件上传下载
1.前端代码 使用超链接到Struts的Action或Servlet <a target="_blank" href="ftpFileAction!download ...
- FTP文件上传并支持断点续传(一)—— win10 本地环境 ftp站点构建
由于之前项目开发是采用是采用的FTP文件上传,就一直想学习,但由于FTP服务器是公司的,为了方便就像把本地变成ftp站点,其实很简单,但也有很多坑 这里简单介绍一下自己遇到的坑 一:开通本地的ftp权 ...
- Python 基于Python实现Ftp文件上传,下载
基于Python实现Ftp文件上传,下载 by:授客 QQ:1033553122 测试环境: Ftp客户端:Windows平台 Ftp服务器:Linux平台 Python版本:Python 2.7 ...
- Java使用comms-net jar包完成ftp文件上传进度的检测功能
本文章只讲述大致的思路与本次功能对应的一些开发环境,具体实现请结合自己的开发情况,仅供参考,如果有不对的地方,欢迎大家指出! 准备环境:JDK1.7 OR 1.8.eclipse.ftp服务器(可自行 ...
- 基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式【Options】处理常规上传和FTP文件上传
在基于SqlSugar的开发框架的服务层中处理文件上传的时候,我们一般有两种处理方式,一种是常规的把文件存储在本地文件系统中,一种是通过FTP方式存储到指定的FTP服务器上.这种处理应该由程序进行配置 ...
- 【FTP】FTP文件上传下载-支持断点续传
Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...
- SecureCRT中的ftp文件上传
原文地址:http://www.blogbus.com/jjuan-flake-logs/59745331.html SecureCRT与SshClient不同的就是,SecureCRT没有图形化的文 ...
- FTP文件上传 支持断点续传 并 打印下载进度(二) —— 单线程实现
这个就看代码,哈哈哈哈哈 需要用到的jar包是: <dependency> <groupId>commons-net</groupId> <artifact ...
随机推荐
- python_并发编程——多进程
from multiprocessing import Process import os def func1(): print('子进程1',os.getpid()) #子进程:获取当前进程的进程号 ...
- httpclient工具使用(org.apache.httpcomponents.httpclient)
httpclient工具使用(org.apache.httpcomponents.httpclient) 引入依赖 <dependency> <groupId>org.apac ...
- “猜你喜欢”的背后揭秘--10分钟教你用Python打造推荐系统
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 话说,最近的瓜实在有点多,从我科校友李雨桐怒锤某男.陈羽凡吸毒被捕.蒋劲夫家暴的三连瓜,到不知知网翟博士,再到邓紫棋解约蜂鸟.王思聪花千芳隔 ...
- (4)Go程序结构和流程控制
Go程序主要由以下几部分组成:(具体可以参考2选择结构中的实例) *包声明 *导入包 *函数 *变量 *语句和表达式 *注释 流程控制 1.顺序结构 2.选择结构 (1)if else if 和 e ...
- P2502 [HAOI2006]旅行——暴力和并查集的完美结合
P2502 [HAOI2006]旅行 一定要看清题目数据范围再决定用什么算法,我只看着是一个蓝题就想到了记录最短路径+最小生成树,但是我被绕进去了: 看到只有5000的边,我们完全可以枚举最小边和最大 ...
- Android中活动的最佳实践(如何很快的看懂别人的代码activity)
这种方法主要在你拿到别人的代码时候很多activity一时半会儿看不懂,用了这个方法以后就可以边实践操作就能够知道具体哪个activity是干什么用的 1.新建一个BaseActivity的类,让他继 ...
- 关于密码重用参数PASSWORD_REUSE_TIME,PASSWORD_REUSE_MAX之间的关系及其演示
转自: https://blog.51cto.com/carefree/1382811 测试环境:10.2.0.2.0测试用户:SCOTT测试用的三组密码:oracle1 oracle2 oracle ...
- IDEA控制台乱码终极解决方案
1. 问题描述 由于本机的IDEA 2019.1出现了无法连接插件商店和Spring Boot模板的问题,就重装了了最新的IDEA 2019.2.4版本,使用了一段时间以后,没有改任何的配置,控制台的 ...
- iOS开发之如何在用户删除应用后保持一些数据
在开发过程中我们有时候在用户删除时候保存一些信息在用户下次安装应用时候使用,这个时候我们可以使用剪切版UIPasteboard的FindUIPasteboard和钥匙串keychain的使用 剪切版剪 ...
- Enhancer | 增强子 专题
要做就做深做精! Everything needs good justification. The interpretation should be biologically and statisti ...