linus jsch上传文件
package com.osplat.util;
import java.io.*;
import com.jcraft.jsch.*;import com.osplat.bean.Resultmodel;import org.springframework.web.multipart.MultipartFile;
/** * @Company:wftdlx * @Author: wjf * @Description: linus 文件上传 * @Date: Created in 10:33 2019/2/14 */public class FtpsFileList {
//jar包/*
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.49</version></dependency>*/
//测试程序
/*@PostMapping("/uploadImage")@ResponseBodypublic Resultmodel uploadImage(@RequestParam MultipartFile file) {
if (file==null) { String msg="上传文件不能为空"; Log.info(msg); return new Resultmodel(0,"上传失败"); } Resultmodel resultmodel=null; try { resultmodel = FtpsFileList.sshSftp(file); } catch (Exception e) { System.out.println("上传失败"+e.getMessage()); return new Resultmodel(0,"上传失败"); } Object fileName=resultmodel.getData(); return new Resultmodel(0,"上传成功",fileName);}*/
public static String user = "root"; public static String ip = "111.111.1.11"; public static int port = 22; public static String psw = "123456"; public static String uploadFileName = UuidUtil.get32UUID();
/** * 利用JSch包实现SFTP下载、上传文件(用户名密码方式登陆) * <p> * ip 主机IP * user 主机登陆用户名 * psw 主机登陆密码 * port 主机ssh2登陆端口,如果取默认值(默认值22),传-1 */ public static Resultmodel sshSftp(MultipartFile file) throws Exception { Log.info("开始用户名密码方式登陆"); 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);//设置密码 session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); //设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); //设置登陆超时时间 session.connect(2000000); String allName = uploadFileName + file.getOriginalFilename(); sftp(session, allName, file);//allName 上传文件后重命名 Log.info("sftp成功"); return new Resultmodel(0, "文件上传成功", allName); }
private static void sftp(Session session, String uploadFileName, MultipartFile file) throws Exception {
Channel channel = null; try { //创建sftp通信通道 channel = (Channel) session.openChannel("sftp"); channel.connect(1000); ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹 sftp.cd("/home/numa/image");
//列出服务器指定的文件列表 /*Vector v = sftp.ls("*.txt"); for (int i = 0; i < v.size(); i++) { System.out.println(v.get(i)); }*/ //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了 OutputStream outstream = sftp.put(uploadFileName);//上传文件重名
//MultipartFile转File File f = null; try { f = File.createTempFile("tmp", null); file.transferTo(f); } catch (Exception e) { e.printStackTrace(); }
//上传流 InputStream instream = new FileInputStream(f);//上传文件内容 byte b[] = new byte[1024]; int n; while ((n = instream.read(b)) != -1) { outstream.write(b, 0, n); }
outstream.flush(); outstream.close(); instream.close(); } catch (Exception e) { e.printStackTrace(); Log.error("文件上传util报错" + e.getMessage()); } finally { session.disconnect(); channel.disconnect(); } }
/** * 利用JSch包实现SFTP下载、上传文件(秘钥方式登陆) * * @param ip 主机IP * @param user 主机登陆用户名 * @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1 * @param privateKey 密钥文件路径 * @param passphrase 密钥的密码 */ /* public static void sshSftp2(String ip, String user , int port, String privateKey, String passphrase) throws Exception { System.out.println("开始秘钥方式登陆"); Session session = null;
JSch jsch = new JSch();
//设置密钥和密码 //支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了 if (privateKey != null && !"".equals(privateKey)) { if (passphrase != null && "".equals(passphrase)) { //设置带口令的密钥 jsch.addIdentity(privateKey, passphrase); } else { //设置不带口令的密钥 jsch.addIdentity(privateKey); } }
if (port <= 0) { //连接服务器,采用默认端口 session = jsch.getSession(user, ip); } else { //采用指定的端口连接服务器 session = jsch.getSession(user, ip, port); }
//如果服务器连接不上,则抛出异常 if (session == null) { throw new Exception("session is null"); }
//设置第一次登陆的时候提示,可选值:(ask | yes | no) session.setConfig("StrictHostKeyChecking", "no"); //设置登陆超时时间 session.connect(30000);
sftp(session, "bb.log"); System.out.println("sftp成功"); }*/}
linus jsch上传文件的更多相关文章
- jsch上传文件到服务器
需求就是上传文件到服务器,服务器的存储地址由程序决定然后可以自动创建. 使用第三方:jsch JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文 ...
- jsch上传文件功能
转载:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch是Java Secure Channel的缩写.JSch是一个 ...
- java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器
首先需要用到jsch-0.1.54.jar 包: 链接: https://pan.baidu.com/s/1kZR6MqwpCYht9Pp_D6NKQw 密码: gywx 直接上代码: package ...
- 使用SFTP上传文件到服务器的简单使用
最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询 /** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp ...
- 用sftp上传文件至linux服务器
1.项目环境 框架:springmvc 项目管理工具:maven 2.必须使用的jar com.jcraft jsch 0.1.27 test 3.新建一个FileUpDown工具类,在类中添加 ...
- Java实现上传文件到指定服务器指定目录
前言需求 使用freemarker生成的静态文件,统一存储在某个服务器上.本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch.毕竟有现成的就很舒服,在此介绍给大家. 具体实现 引入的pom ...
- 上传文件到服务器指定位置 & 从服务器指定位置下载文件
需要的jar包: 去maven仓库自己搜索com.jcraft下载jar包 <dependency> <groupId>com.jcraft</groupId> & ...
- Java上传文件至SFTP服务器
Windows搭建SFTP服务器 https://www.cnblogs.com/wangjunguang/p/9453611.html 注意点: 1.以管理员权限运行FreeSSHd 2.如果无法启 ...
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
随机推荐
- Java实现图像对比类
package com.function; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import jav ...
- LeetCode 失败的尝试 10. regular expression matching & 正则
Regular Expression Matching 看到正则就感觉头大,因为正则用好了就很强大.有挑战的才有意思. 其实没有一点思路.循环的话,不能一一对比,匹配模式解释的是之前的字符.那就先遍历 ...
- elk之[logstash-input-file]插件使用详解
https://www.cnblogs.com/xing901022/p/4805586.html http://www.cnblogs.com/xing901022/p/4802822.html ...
- Mapper的.xml文件的delete的参数问题
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...
- nginx 虚拟主机、反向代理服务器及负载均衡,多台主机分离php-fpm实验
一.简介 本章介绍一些架构原理基础知识, 1.1.LNMP及php-fpm 请参考https://www.cnblogs.com/zhangxingeng/p/10242902.html 1.2.透明 ...
- nodejs爬虫设置动态userAgent
动态 userAgent 这是我收集到的常用的浏览器头部信息,每次爬取的时候从中随机选取一个,并使用 superAgent 设置请求头部的 User-Agent 字段就好了. userAgent.js ...
- Spring Cloud (6)A config 服务端配置 与github通信
1. 在git上创建一个库,并复制库地址 2.用git clone项目到本地,并创建application.yml application.yml 内容 --下一步 3.将application.ym ...
- 源码编译php5.4 ./configure参数
./configure \--prefix=/usr/local/php/5.4 \--with-config-file-path=/usr/local/php/5.4/etc \--with-con ...
- HTML学习-1网页基础知识
HTML超文本标记语言:HyperText Markup Language. 由浏览器运行解析. 它包括了静态页面.html .htm.动态页面.php .aspx .jsp,从数据库提取. 今天 ...
- Windows下如何查看某个端口被谁占用
开发时经常遇到端口被占用的情况,这个时候总是很令人抓狂,知道被哪个进程占用还好,结束就是了,要是不知道我们该怎么办呢? 我告诉大家一个方法,^_^. 1. 开始—->运行—->cmd,或者 ...