/**
*
* @param filePath 文件全路径
* @param ftpPath 上传到目的端目录
* @param username
* @param password
* @param host
* @param port
*/
public static void uploadFile(String filePath, String ftpPath, String username, String password, String host, Integer port) {
FileInputStream input = null;
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
//获取session 账号-ip-端口
com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
//添加密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
//严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
//开启session连接
sshSession.connect();
//获取sftp通道
sftp = (ChannelSftp) sshSession.openChannel("sftp");
//开启
sftp.connect();
//文件乱码处理
/*Class<ChannelSftp> c = ChannelSftp.class;
Field f = c.getDeclaredField("server_version");
f.setAccessible(true);
f.set(sftp, 2);
sftp.setFilenameEncoding("GBK");*/
//判断目录是否存在
try {
Vector ls = sftp.ls(ftpPath); //ls()得到指定目录下的文件列表
/*if (ls == null) { //ls不会为null,哪怕它是一个空目录
sftp.mkdir(ftpPath);
}*/
} catch (SftpException e) {
sftp.mkdir(ftpPath);
}
sftp.cd(ftpPath);
String filename = filePath.substring(filePath.lastIndexOf(File.separator) + 1); //附件名字
//filename = new String(filename.getBytes("GBK"), StandardCharsets.ISO_8859_1);
input = new FileInputStream(new File(filePath));
sftp.put(input, filename);
//设定777权限,转为8进制放入chmod中
//sftp.chmod(Integer.parseInt("777", 8), ftpPath + filename);
input.close();
sftp.disconnect();
sshSession.disconnect();
System.out.println("================上传成功!==================");
} catch (Exception e) {
System.out.println("================上传失败!==================");
e.printStackTrace();
}
} /**
* @param directory SFTP服务器的文件路径
* @param downloadFile SFTP服务器上的文件名
* @param saveFile 保存到本地路径
* @param username
* @param password
* @param host
* @param port
*/
public static void downloadFile(String directory, String downloadFile, String saveFile, String username, String password, String host, Integer port) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
//获取session 账号-ip-端口
com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
//添加密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
//严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
//开启session连接
sshSession.connect();
//获取sftp通道
sftp = (ChannelSftp) sshSession.openChannel("sftp");
//开启
sftp.connect();
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
FileOutputStream output = new FileOutputStream(new File(saveFile));
sftp.get(downloadFile, output);
output.close();
sftp.disconnect();
sshSession.disconnect();
System.out.println("================下载成功!==================");
} catch (SftpException | FileNotFoundException | JSchException e) {
log.error("文件下载异常!", e);
} catch (IOException e) {
e.printStackTrace();
}
}

获取文件所在绝对路径:

new ClassPathResource("").getURI().getPath()

ResourceUtils.getURL("classpath:").getPath()

java实现sftp文件上传下载的更多相关文章

  1. Java中实现文件上传下载的三种解决方案

    第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...

  2. java中的文件上传下载

    java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...

  3. java+web+大文件上传下载

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

  4. java 通过sftp服务器上传下载删除文件

    最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...

  5. Java实现FTP与SFTP文件上传下载

    添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...

  6. Jsch - java SFTP 文件上传下载

    使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...

  7. 我的代码库-Java8实现FTP与SFTP文件上传下载

    有网上的代码,也有自己的理解,代码备份 一般连接windows服务器使用FTP,连接linux服务器使用SFTP.linux都是通过SFTP上传文件,不需要额外安装,非要使用FTP的话,还得安装FTP ...

  8. Java FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  9. java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题

    //文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,String ...

随机推荐

  1. python练习册 每天一个小程序 第0006题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...

  2. chili

    靶机准备 首先将靶机ova文件导入 网络模式改为NAT 扫描ip netdiscover -r 192.168.164.0/24 kali:192.168.164.137 渗透测试 扫描端口 nmap ...

  3. 洛谷 P1020 [NOIP1999 普及组] 导弹拦截

    Coidng #include <iostream> #include <algorithm> #include <cstring> #include <ve ...

  4. 中国软件杯---电力客户行为分析---图表联动echarts-demo(flask)

    中国软件杯---电力客户行为分析---图表联动echarts-demo(flask) 题目链接(可下载原始CSV数据集):http://www.cnsoftbei.com/plus/view.php? ...

  5. 运行 Spring Boot 有哪几种方式?

    打包用命令或者放到容器中运行用 Maven/ Gradle 插件运行直接执行 main 方法运行

  6. mybatis插件机制原理

    mybatis插件机制及分页插件原理 参考链接:mybatis插件机制及分页插件原理 如何编写一个自定义mybatis插件 参考链接:mybatis 自定义插件的使用

  7. Citus 分布式 PostgreSQL 集群 - SQL Reference(手动查询传播)

    手动查询传播 当用户发出查询时,Citus coordinator 将其划分为更小的查询片段,其中每个查询片段可以在工作分片上独立运行.这允许 Citus 将每个查询分布在集群中. 但是,将查询划分为 ...

  8. webpack+vue-cli+ElementUI+vue-resource 前端开发

    线上商城项目(电脑0环境)1.安装node.js (参考http://www.runoob.com/nodejs/nodejs-install-setup.html): 2.安装 webpack (全 ...

  9. 深入理解ES6(二)(解构赋值)

    变量的解构赋值 (1) 数组的解构赋值 1.基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring ). 只要等号两边的模式相同,左边的变量 ...

  10. 手机上无法显示Toast信息

    关于手机上无法显示Toast信息, 是因为手机上的权限没有开, 在应用管理处将所有权限都打开,就可以显示了.