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上传文件的更多相关文章

  1. jsch上传文件到服务器

    需求就是上传文件到服务器,服务器的存储地址由程序决定然后可以自动创建. 使用第三方:jsch JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文 ...

  2. jsch上传文件功能

    转载:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch是Java Secure Channel的缩写.JSch是一个 ...

  3. java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器

    首先需要用到jsch-0.1.54.jar 包: 链接: https://pan.baidu.com/s/1kZR6MqwpCYht9Pp_D6NKQw 密码: gywx 直接上代码: package ...

  4. 使用SFTP上传文件到服务器的简单使用

    最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询 /** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp ...

  5. 用sftp上传文件至linux服务器

    1.项目环境 框架:springmvc    项目管理工具:maven 2.必须使用的jar com.jcraft jsch 0.1.27 test 3.新建一个FileUpDown工具类,在类中添加 ...

  6. Java实现上传文件到指定服务器指定目录

    前言需求 使用freemarker生成的静态文件,统一存储在某个服务器上.本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch.毕竟有现成的就很舒服,在此介绍给大家. 具体实现 引入的pom ...

  7. 上传文件到服务器指定位置 & 从服务器指定位置下载文件

    需要的jar包: 去maven仓库自己搜索com.jcraft下载jar包 <dependency> <groupId>com.jcraft</groupId> & ...

  8. Java上传文件至SFTP服务器

    Windows搭建SFTP服务器 https://www.cnblogs.com/wangjunguang/p/9453611.html 注意点: 1.以管理员权限运行FreeSSHd 2.如果无法启 ...

  9. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

随机推荐

  1. Webpack配置及使用

    ##如何将js模块化 ### module.exports() ### module.require() ### 自定义文件,进入时需要./ ### npm下载得到文件,不需要./ ##如果使用第三方 ...

  2. docekr-image的区别和container;docker run和start,create

    copy by: https://www.simapple.com/326.html 容器(container)的定义和镜像(image)几乎一模一样,也是一堆层的统一视角,唯一区别在于容器的最上面那 ...

  3. ROS 进阶学习笔记(12) - Communication with ROS through USART Serial Port

    Communication with ROS through USART Serial Port We always need to communicate with ROS through seri ...

  4. IntelliJ IDEA 注释模板设置

    1.idea类注释 打开:file->setting->Editor->Filr and Code Templates->Includes->File Header 类注 ...

  5. tomcat及nginx相关,格式化输出,配置及日志解析

    1.https://www.cnblogs.com/jingmoxukong/p/8258837.html?utm_source=gold_browser_extension       Tomcat ...

  6. Django 模板格式化日期

    在模板中格式化日期: {{ post.date|date:”Y-m-d H:i:s” }}

  7. PE 装机

    PE 装机 下载PE安装到硬盘启动. 下载win7安装原始文件ISO镜像文件,解压ISO到文件夹. 重启电脑,选择PE菜单. 打开windows安装器大全>选择 winntSetup> i ...

  8. UI5-学习篇-18-云端UI5应用部署到Fiori Launchpad

    UI5应用发布SCP 选择UI5应用项目,右键 Deploy - Deploy to SAP Cloud Platform 输入云平台子账号,项目名称,应用名称,如下图所示: 点击Open the r ...

  9. 09_组件三大属性(3)_refs和事件处理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Logstash收集nginx日志之使用grok过滤插件解析日志

    grok作为一个logstash的过滤插件,支持根据模式解析文本日志行,拆成字段. nginx日志的配置: log_format main '$remote_addr - $remote_user [ ...