Springboot上传文件到远程文件服务器(linux系统)

1.添加依赖

        
        
            com.jcraft
            jsch
            0.1.54
        

2.工具类

package com.nwpusct.csal.common.util;

import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @ProjectName SecurityTest
 * @Package com.nwpusct.csal.common.util
 * @Name NdeUploadFileServerUtil
 * @Author HB
 * @Date 2020/5/7 0007 下午 14:10
 * @Version 1.0  上传文件到文件服务器上(CentOS系统服务器)
 */
@Component
public class FTPUtil {


    private static String host;
    @Value(value = "${ftp-centos.host}")
    public  void setHost(String hostName){
        host = hostName;
    }

    private static Integer port;
    @Value(value = "${ftp-centos.port}")
    public  void setPort(Integer portName) {
        port = portName;
    }

    private static String user;
    @Value(value = "${ftp-centos.user}")
    public  void setUser(String userName) {
      user = userName;
    }

    private static String password;
    @Value(value = "${ftp-centos.password}")
    public  void setPassword(String passwordName) {
        password = passwordName;
    }



    private static String basePath;
    @Value(value = "${ftp-centos.basePath}")
    public  void setBasePath(String basePath) {
        FTPUtil.basePath = basePath;
    }

    public static void sshSftp(byte[] bytes, String fileName) throws Exception {
        Session session = null;
        Channel channel = null;
        JSch jsch = new JSch();
        if (port<= 0) {
            //连接服务器,采用默认端口
            session = jsch.getSession(user, host);
        } else {
            //采用指定的端口连接服务器
            session = jsch.getSession(user, host, 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(basePath);
            //列出服务器指定的文件列表
//            Vector v = sftp.ls("*");
//            for(int i=0;i

3.远程服务器配置

#上传文件到文件服务器的配置
ftp-centos:
  #IP
  host: 121.46.19.23
  #端口
  port: 20623
  user: amind
  password: 123456
  basePath: /home/file

4.接口代码

package com.nwpusct.csal.controller.uploadfileserver;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.nwpusct.csal.common.util.FTPUtil;
import com.nwpusct.csal.common.util.RestResult;
import com.nwpusct.csal.common.util.RestResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


/**
 * @ProjectName SecurityTest
 * @Package com.nwpusct.csal.controller.uploadfileserver
 * @Name NdeUploadFileServer
 * @Author HB
 * @Date 2020/5/7 0007 上午 11:07
 * @Version 1.0  上传文件到文件服务器(CentOS文件服务器)
 */
@RestController
@RequestMapping(value = "/uploadFileServer")
public class NdeUploadFileServerController {


    @ApiOperation(value = "文件上传到文件服务器")
    @RequestMapping(value = "/uploadFolder", method = RequestMethod.POST)
    public RestResult uploadFolder(MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            FTPUtil.sshSftp(bytes, file.getOriginalFilename());
            return RestResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResultUtil.failed();
    }
}








你可能感兴趣的:(后台,Ubuntu)