SFTP 上传文件 MultipartFile mfile 接收(坑!!!!!!),需要将MultipartFile 转为 File,上传的文件内容才不是空文件

接受file必须为MultipartFile类型

下面展示一些 内联代码片

接收参数

@RequestParam("file") MultipartFile mfile

MultipartFile 转为为 File 类型

File file = new File(mfile.getOriginalFilename());
FileUtils.copyInputStreamToFile(mfile.getInputStream(), file);

上传方法 new FileInputStream(file) ,file文件转为流

SFTPUtils.upload(host,Integer.valueOf(port),username,password,basePath,fileName,new FileInputStream(file));

具体上传方法 upload

/**
     * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory
     *  basePath  服务器的基础路径
     *  directory  上传到该目录
     *  sftpFileName  sftp端文件名
     *  in   输入流
     */
    public static boolean upload(String host, int port, String username, String password
            , String basePath, String sftpFileName, InputStream input) {
        logger.info("SftpUtils upload begin");
        boolean success = true;
        //sftp的链接
        ChannelSftp sftp = login(host, port, username, password);
        try {
            sftp.cd(basePath);
        } catch (SftpException e) {
            //目录不存在,则创建文件夹
            String [] dirs = basePath.split("/");
            String tempPath = "";
            for(String dir:dirs){
                if(null== dir || "".equals(dir)) {
                    continue;
                }
                tempPath+="/"+dir;
                try {
                    sftp.cd(tempPath);
                } catch (SftpException e1) {
                    try {
                        sftp.mkdir(tempPath);
                        sftp.cd(tempPath);
                    } catch (SftpException e2) {
                        logger.error("创建文件夹失败", e2);
                        //throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("创建文件夹失败"));
                    }
                }
                try{
                    sftp.cd(tempPath);
                }catch(SftpException e1){
                    logger.error("SFTP服务器异常", e1);
                    //throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("SFTP服务器异常"));
                }
            }
        }
        try {
            //上传文件
            sftp.put(input, sftpFileName);
            input.close();
        } catch (SftpException | IOException e) {
            // TODO Auto-generated catch block
            logger.error("上传文件失败", e);
//            throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("上传文件失败"));
            success = false;
        } finally {
            logout(sftp);
        }
        logger.info("SftpUtils upload end");
        return success;
    }

sftp 的链接方法 login

/**
     * 连接sftp服务器
     */
    public static ChannelSftp login(String host, int port, String username, String password) {
        logger.info("SftpUtils login begin");
        Session session = null;
        String config = "Port "+port+"\n" +
                "\n" +
                "Host foo\n" +
                " User "+username+"\n" +
                " Hostname "+host+"\n" +
                " Host *\n" +
                " ConnectTime 3000\n" +
                " PerferredAuthentications Keyboard-interact,password,publicKey\n" +
                " #ForwardAgent yes\n" +
                " #StrictHostKeyChecking no\n" +
                " #identiyFile ~/.ssh/id_rsa\n" +
                " #UserKnownHostFile ~/.shh/known_hosts";
        ConfigRepository configRepository = null;
        try {
            configRepository = OpenSSHConfig.parse(config);
            JSch jSch = new JSch();

            jSch.setConfigRepository(configRepository);

            //"foo" is from "Host foo" in the above config
            session = jSch.getSession("foo");
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(18000);

            Channel channel = session.openChannel("sftp");
            channel.connect();

            ChannelSftp sftp = (ChannelSftp) channel;

            logger.info("SftpUtils login end");
            return sftp;
        } catch (JSchException | IOException e) {
            logger.error("连接SFTP服务器失败", e);
            return null;
        }
    }

业务类TemporaryImportDataController


package org.apache.dolphinscheduler.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.dolphinscheduler.api.utils.SFTPUtils;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.*;
import java.sql.Timestamp;

/**
 *
 * 临时数据导入,暂存线上服务
 *
 */
@RestController
@RequestMapping("temporaryImport")
public class TemporaryImportDataController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TemporaryImportDataController.class);

    /**
     * sftp获取上传暂存文件
     * @param sftpFileName 文件名称
     * @param splitSign 分隔符
     * @param loginUser 用户
     * @param head 文件是否有 头
     * @return
     * @throws FileNotFoundException
     */
    @PostMapping("/getHeadBack")
    @ResponseBody
    public JSONObject getHeadBack(
            @RequestParam("file") MultipartFile mfile
            ,@RequestParam("sftpFileName") String sftpFileName
            ,@RequestParam("splitSign") String splitSign
            ,@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser
            ,@RequestParam("head") String head ) throws FileNotFoundException {
        JSONObject all = new JSONObject();
        JSONArray heads = new JSONArray();
        JSONArray bodys = new JSONArray();
        String headFile = sftpFileName.split("\\.")[0];
        String typeFile = sftpFileName.split("\\.")[1];
        String fileName = headFile + "_" + loginUser.getId()+ "_" +getTimetamp()+"."+typeFile;
        try {

            File file = new File(mfile.getOriginalFilename());
            FileUtils.copyInputStreamToFile(mfile.getInputStream(), file);

            byte [] byteArr=mfile.getBytes();
            InputStream fis = new ByteArrayInputStream(byteArr);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line;
            int i = 0;
            while ((line = br.readLine()) != null) {//获取文件每一行的 字符串
                if(i==0&&"1".equals(head)){// 有头的情况下
                    String[] headarray = line.split(splitSign);
                    for(int j=0;j<headarray.length;j++){
                        JSONObject ob = new JSONObject();
                        ob.put("title",headarray[j]);
                        ob.put("key","column"+j);
                        heads.add(ob);
                    }
                }else{// 没有头的情况下
                    String[] bodyarray = line.split(splitSign);
                    if(i==0){//有头部的情况下需要建造头部
                        for(int j=0;j<bodyarray.length;j++){
                            JSONObject ob = new JSONObject();
                            ob.put("title","column"+j);
                            ob.put("key","column"+j);
                            heads.add(ob);
                        }
                    }
                    JSONObject body = new JSONObject();
                    for(int j=0;j<bodyarray.length;j++){
                        body.put("column"+j,bodyarray[j]);
                    }
                    bodys.add(body);
                }
                i = i + 1;
            }
            fis.close();
            String host = "192.168.1.92";
            String port = "22";
            String username = "root";
            String password = "rootroot";
            String basePath = "/opt/testSFTP/";
            //String sftpFileName = "wx";
            //上传文件
            SFTPUtils.upload(host,Integer.valueOf(port),username,password,basePath,fileName,new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }

        all.put("heads",heads);
        all.put("bodys",bodys);
        all.put("fileName",fileName);
        all.put("head",head);
        return all;
    }





    /**
     * 时间戳 20210603 wx
     * @return
     */
    public static String  getTimetamp() {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        return  String.valueOf(timestamp.getTime());
    }



}

工具类 SFTPUtils

package org.apache.dolphinscheduler.api.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.List;
import java.util.Properties;
import java.util.Vector;


/**
 * @author Administrator
 *
 */

public  class SFTPUtils {

    private final static Logger logger = LoggerFactory.getLogger(SFTPUtils.class);

    protected final static int CLIENT_TIMEOUT = 1000 * 180;


    /**
     * 连接sftp服务器
     */
    public static ChannelSftp login(String host, int port, String username, String password) {
        logger.info("SftpUtils login begin");
        Session session = null;
        String config = "Port "+port+"\n" +
                "\n" +
                "Host foo\n" +
                " User "+username+"\n" +
                " Hostname "+host+"\n" +
                " Host *\n" +
                " ConnectTime 3000\n" +
                " PerferredAuthentications Keyboard-interact,password,publicKey\n" +
                " #ForwardAgent yes\n" +
                " #StrictHostKeyChecking no\n" +
                " #identiyFile ~/.ssh/id_rsa\n" +
                " #UserKnownHostFile ~/.shh/known_hosts";
        ConfigRepository configRepository = null;
        try {
            configRepository = OpenSSHConfig.parse(config);
            JSch jSch = new JSch();

            jSch.setConfigRepository(configRepository);

            //"foo" is from "Host foo" in the above config
            session = jSch.getSession("foo");
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(18000);

            Channel channel = session.openChannel("sftp");
            channel.connect();

            ChannelSftp sftp = (ChannelSftp) channel;

            logger.info("SftpUtils login end");
            return sftp;
        } catch (JSchException | IOException e) {
            logger.error("连接SFTP服务器失败", e);
            return null;
        }
    }

    /**
     * 关闭连接 server
     */
    public static void logout(ChannelSftp sftp){
        logger.info("SftpUtils logout begin");
        if (sftp != null) {
            Session session = null;
            try {
                session = sftp.getSession();
                if (sftp.isConnected()) {
                    sftp.disconnect();
                }
            } catch (JSchException e) {
                logger.error("获取session失败", e);
            } finally {
                if (session != null) {
                    session.disconnect();
                }
            }
        }
        logger.info("SftpUtils logout end");
    }

    /**
     * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory
     *  basePath  服务器的基础路径
     *  directory  上传到该目录
     *  sftpFileName  sftp端文件名
     *  in   输入流
     */
    public static boolean upload(String host, int port, String username, String password
            , String basePath, String sftpFileName, InputStream input) {
        logger.info("SftpUtils upload begin");
        boolean success = true;
        ChannelSftp sftp = login(host, port, username, password);
        try {
            sftp.cd(basePath);
        } catch (SftpException e) {
            //目录不存在,则创建文件夹
            String [] dirs = basePath.split("/");
            String tempPath = "";
            for(String dir:dirs){
                if(null== dir || "".equals(dir)) {
                    continue;
                }
                tempPath+="/"+dir;
                try {
                    sftp.cd(tempPath);
                } catch (SftpException e1) {
                    try {
                        sftp.mkdir(tempPath);
                        sftp.cd(tempPath);
                    } catch (SftpException e2) {
                        logger.error("创建文件夹失败", e2);
                        //throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("创建文件夹失败"));
                    }
                }
                try{
                    sftp.cd(tempPath);
                }catch(SftpException e1){
                    logger.error("SFTP服务器异常", e1);
                    //throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("SFTP服务器异常"));
                }
            }
        }
        try {
            //上传文件
            sftp.put(input, sftpFileName);
            input.close();
        } catch (SftpException | IOException e) {
            // TODO Auto-generated catch block
            logger.error("上传文件失败", e);
//            throw new BusinessException(ExceptionEnum.EXECUTE_RUNTIME_EXCP.setExceptionMsg("上传文件失败"));
            success = false;
        } finally {
            logout(sftp);
        }
        logger.info("SftpUtils upload end");
        return success;
    }

    /**
     * 下载文件。
     *  directory 下载目录
     *  downloadFile 下载的文件
     *  saveFile 存在本地的路径
     */
    public static boolean download(String host, int port, String username, String password, String filePath, final OutputStream outputStream) {
        logger.info("SftpUtils download begin");
        boolean downloaded = true;
        ChannelSftp sftp = login(host, port, username, password);
        String fileName = null;
        try {
            if (filePath != null && !"".equals(filePath)) {
                String directory = filePath.substring(0, filePath.lastIndexOf(File.separator));
                sftp.cd(directory);
            }
            fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);

            sftp.get(fileName, outputStream);
        } catch (SftpException e) {
            downloaded = false;
            logger.error("FTP服务器异常", e);
        } finally {
            logout(sftp);
        }
        logger.info("SftpUtils download end");
        return downloaded;
    }

    /**
     * 删除文件
     *  directory 要删除文件所在目录
     *  deleteFile 要删除的文件
     */
    public static boolean delete(String host, int port, String username, String password, String filePath){
        logger.info("SftpUtils delete begin");
        boolean success = true;
        ChannelSftp sftp = null;
        try {
            sftp = login(host, port, username, password);
            String directory = filePath.substring(0, filePath.lastIndexOf(File.separator));
            sftp.cd(directory);
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
            sftp.rm(fileName);
        } catch (SftpException e) {
            logger.error("文件删除失败", e);
            success = false;
        } finally {
            logout(sftp);
        }
        logger.info("SftpUtils delete end");
        return success;
    }

    /**
     * 列出目录下的文件
     *  directory 要列出的目录
     *  sftp
     */
    public static boolean isFileExist(String host, int port, String username, String password, String filePath) {
        boolean success = false;
        ChannelSftp sftp = null;
        try {
            sftp = login(host, port, username, password);
            String directory = filePath.substring(0, filePath.lastIndexOf(File.separator));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
            Vector<?> vector = sftp.ls(directory);
            for (Object obj : vector) {
                if (obj != null) {
                    LsEntry lsEntry = (LsEntry) obj;
                    if (fileName.equals(lsEntry.getFilename())) {
                        success = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            logger.error("sftp服务器异常");
        } finally {
            logout(sftp);
        }
        return success;
    }

    /**
     * 获取文件内容
     * @param host ip
     * @param port 端口
     * @param username 名称
     * @param password 密码
     * @param filePath 文件路径
     * @param sftpFileName 文件名称
     * @param splitSign 分隔符
     * @return
     */
    public static JSONArray getSftpContent(String host, int port, String username, String password
            , String filePath, String sftpFileName, String head,String splitSign) throws Exception{
        JSONArray bodys = new JSONArray();
        ChannelSftp sftp = login(host, port, username, password);
        try {
            if (filePath != null && !"".equals(filePath)) {
                String sftpBillResources = filePath+sftpFileName;
                InputStream is = sftp.get(sftpBillResources);//通过文件名获取流
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                int i = 0;
                while ((line = br.readLine()) != null) {
                    if("0".equals(head)){//没有头
                        JSONObject body = new JSONObject();
                        body.put(String.valueOf(i),line);//代表第几行数据
                        bodys.add(body);
                    }else{
                        if(i!=0){
                            JSONObject body = new JSONObject();
                            body.put(String.valueOf(i-1),line);//代表第几行数据
                            bodys.add(body);
                        }
                    }
                    i = i + 1;
                }
            }
        } catch (SftpException | IOException e) {
            logger.error("FTP服务器异常", e);
        } finally {
            logout(sftp);
        }
        return bodys;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值