zoukankan      html  css  js  c++  java
  • JAVA代码实现上传文件至文件服务器(远程服务器、非项目当前所在服务器)

    步骤一:添加依赖

    <!--sftp文件上传-->
    <dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
    </dependency>

    步骤二:编写工具类

    package com.example.vue.vuedemo;


    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.*;

    import java.io.*;

    /**
    * Created by jlm on 2019-09-17 17:44
    */
    public class FtpUtils {

    /**
    * 利用JSch包实现SFTP上传文件
    * @param bytes 文件字节流
    * @param fileName 文件名
    * @throws Exception
    */
    public static void sshSftp(byte[] bytes,String fileName) throws Exception{
    //指定的服务器地址
    String ip = "服务器ip地址";
    //用户名
    String user = "用户名";
    //密码
    String password = "密码";
    //服务器端口 默认22
    int port = 22;
    //上传文件到指定服务器的指定目录 自行修改
    String path = "/root";

    Session session = null;
    Channel channel = 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(password);//设置密码
    //设置第一次登陆的时候提示,可选值:(ask | yes | no)
    session.setConfig("StrictHostKeyChecking", "no");
    //设置登陆超时时间
    session.connect(30000);


    OutputStream outstream = null;
    try {
    //创建sftp通信通道
    channel = (Channel) session.openChannel("sftp");
    channel.connect(1000);
    ChannelSftp sftp = (ChannelSftp) channel;


    //进入服务器指定的文件夹
    sftp.cd(path);

    //列出服务器指定的文件列表
    // Vector v = sftp.ls("*");
    // for(int i=0;i<v.size();i++){
    // System.out.println(v.get(i));
    // }

    //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
    outstream = sftp.put(fileName);
    outstream.write(bytes);

    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    //关流操作
    if(outstream != null){
    outstream.flush();
    outstream.close();
    }
    if(session != null){
    session.disconnect();
    }
    if(channel != null){
    channel.disconnect();
    }
    }
    }

    }

    步骤三:写一个接口上传文件,调用工具类方法即可

    package com.example.vue.vuedemo;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;

    import javax.servlet.http.HttpServletRequest;

    /**
    * Created by jlm on 2019-09-17 17:42
    */
    @RestController
    public class UploadController {


    @RequestMapping("file")
    public void upload(HttpServletRequest httpServletRequest, MultipartFile file) throws Exception {
    byte[] bytes = file.getBytes();
    FtpUtils.sshSftp(bytes,"1111.jpg");

    }

    }


    ————————————————
    版权声明:本文为CSDN博主「Rice_kil」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/Rice_kil/article/details/100934710

  • 相关阅读:
    奶酪工厂
    P1080 国王游戏(非高精版)
    【洛谷P2150】[NOI2015] 寿司晚宴
    【洛谷P3349】[ZJOI2016]小星星
    【洛谷P5785】[SDOI2012]任务安排
    【模板】严格次短路
    【洛谷P3647】[APIO2014]连珠线
    2021.10.27NOIP模拟总结
    【树形DP】CF1016F Road Projects
    2021CSP-S 总结
  • 原文地址:https://www.cnblogs.com/niudaxianren/p/12409134.html
Copyright © 2011-2022 走看看