The FTP file server operation that can be seen at a glance, Java, all!

background:

Recently, there is a need to connect to the system of an overseas client company. Based on security considerations, the other party uses SFTP to save files. We need to connect to their FTP server, read the file and save it to our own server. Since the file operation of the FTP server will be used, the whole process has been recorded, and I hope it can be useful to everyone.

get connection

public static ChannelSftp getSFtpChannel(String host, int port, String username, final String password) {
        try {
            log.info("=====begin FTP connect=====");
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            log.info("=====Session connected!=====");
            Channel  channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            log.info("=====FTP Channel connected!=====");
            return sftp;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

close connection

/**
     * 关闭连接
     */
    public static void closeChannel() {
        log.info("=====sftp object closing=====");
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();
            }
        }
        if (sshSession != null) {
            if (sshSession.isConnected()) {
                sshSession.disconnect();
            }
        }
    }

get file list

/**
     * 列出指定目录下的文件
     * @param sftp
     * @param dir
     * @return
     */
    public static List<String> listFileNames(ChannelSftp sftp, String dir) {
        List<String> list = new ArrayList<String>();
        try {
            Vector<?> vector = sftp.ls(dir);
            for (Object item:vector) {
                LsEntry entry = (LsEntry) item;
                log.info(entry.getFilename());
                list.add(entry.getFilename());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeChannel();
        }
        return list;
    }

Read FTP files and upload to OSS


    /**
     * Read the file according to the file name and upload it to Alibaba Cloud OSS
     * @param sftp
     * @param fileName
     * @return
     */
    public static List<String> getOSSFilePathForFTP(ChannelSftp sftp,String dir, String fileName,OSSClient ossClient, String ossBucketName){         List<String> urlList = new ArrayList<String>();         try {             String[] fileNameList = fileName.split(",");             String absolutePath = StringUtils.isBlank(dir)?BASE_DIR:(BASE_DIR+dir );             for(String name:fileNameList) {                 String src = absolutePath+name;                 //Read FTP file, get file input stream                 InputStream is = sftp.get(src);








                log.info("=====Begin read file=====");
                String tempFileUrl = TEMP_PATH+name;
                File tempFile = new File(tempFileUrl);
                //Save the file stream to the file
                inputStreamToFile(is,tempFile );
                //Upload to Alibaba Cloud server, get address
                String fileUrl = OSSClientUtils.uploadFile2Oss(tempFile, ossClient, ossBucketName, OSSClientUtils.FTP_DIR);
                log.info("=====fileName:{},fileUrl:{}" ,name,fileUrl);
                if(StringUtils.isNotBlank(fileUrl)) {                     fileUrl = fileUrl.split("\\?")[0];                 }                 urlList.add(fileUrl);                 if(is!=null) {




                    is.close();
                }
                //Delete temporary files
                FileUtils.deleteQuietly(tempFile);
            }
        } catch (Exception e) {             log.error("=====get file data exception {}", e);         }finally {             closeChannel();         }         return urlList;     }





Upload files to FTP

/**
     * Upload the file to the specified folder
     * @param sftp
     * @param dir
     * @param fileName
     * @param out
     * @return
     */
    public static boolean uploadNew(ChannelSftp sftp, String dir, String fileName, InputStream inputStream) {         boolean flag = true;         try {             if(StringUtils.isBlank(dir)) {                 sftp.cd(BASE_DIR);             }else{                 sftp.cd(BASE_DIR+dir);             }             log.info("=====cd directory success====={}",BASE_DIR+dir);         } catch (SftpException e) {             e.printStackTrace();










            try {
                sftp.mkdir(BASE_DIR+dir);
                sftp.cd(BASE_DIR+dir);
                log.info("=====create directory success====={}",BASE_DIR+dir);
            } catch (SftpException e1) {
                flag = false;
                log.info("=====create directory fail====={}",BASE_DIR+dir);
                e1.printStackTrace();
            }
        }
        log.info("=======>dicrectory flag==>{},uploadFile==>{}", flag, fileName);
        try{
            sftp.put(inputStream, fileName+".ing");
            sftp.rename(BASE_DIR+dir + "/" + fileName+".ing", BASE_DIR+dir + "/" + fileName);
            log.info("the fifth step =========>{}", "rename success");
            if(inputStream!=null) {
                inputStream.close();
            }
        } catch (Exception e) {
            flag = false;
            throw new RuntimeException("sftp excetion" + e);
        } finally {
            closeChannel();
        }
        return flag;
    }
 

 The complete tool class is as follows: More ideas are welcome to supplement the tool class

package com.aaa.store.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.aliyun.oss.OSSClient;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FtpUtilPlus {
    private static ChannelSftp sftp = null;
    private static Session sshSession = null;
    private static final String BASE_DIR = "/";
    private static final String TEMP_PATH = "/temp/";
    
    
    /**
     * 获取SFTP连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @return
     */
    public static ChannelSftp getSFtpChannel(String host, int port, String username, final String password) {
        try {
            log.info("=====begin FTP connect=====");
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            log.info("=====Session connected!=====");
            Channel  channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            log.info("=====FTP Channel connected!=====");
            return sftp;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Read the file according to the file name and upload it to Alibaba Cloud OSS
     * @param sftp
     * @param fileName
     * @return
     */
    public static List<String> getOSSFilePathForFTP(ChannelSftp sftp,String dir, String fileName,OSSClient ossClient,String ossBucketName){         List<String> urlList = new ArrayList<String>();         try {             String[] fileNameList = fileName.split(",");             String absolutePath = StringUtils. isBlank(dir)?BASE_DIR:(BASE_DIR+dir);             for(String name:fileNameList) {                 String src = absolutePath+name;






                //Read FTP file, get file input stream
                InputStream is = sftp.get(src);
                log.info("=====Begin read file=====");
                String tempFileUrl = TEMP_PATH+name;
                File tempFile = new File(tempFileUrl);
                //Save the file stream to a file
                inputStreamToFile(is,tempFile);
                //Upload to Alibaba Cloud server and get the address
                String fileUrl = OSSClientUtils.uploadFile2Oss(tempFile, ossClient, ossBucketName, OSSClientUtils.FTP_DIR) ;
                log.info("=====fileName:{},fileUrl:{}",name,fileUrl);
                if(StringUtils.isNotBlank(fileUrl)) {                     fileUrl = fileUrl.split("\\?")[ 0];                 }


                urlList.add(fileUrl);
                if(is!=null) {                     is.close();                 }                 //Delete temporary files                 FileUtils.deleteQuietly(tempFile);             }         } catch (Exception e) {             log.error("=== ==get file data exception {}", e);         }finally {             closeChannel();         }         return urlList;     }     /**      * List the files in the specified directory      * @param sftp      * @param dir      * @return      */     public static List<String> listFileNames(ChannelSftp sftp, String dir) {












    







        List<String> list = new ArrayList<String>();
        try {             Vector<?> vector = sftp.ls(dir);             for (Object item:vector) {                 LsEntry entry = (LsEntry) item;                 log.info(entry .getFilename());                 list.add(entry.getFilename());             }         } catch (Exception e) {             e.printStackTrace();         } finally {             closeChannel();         }         return list;     }     /**      * Upload the file Go to the specified folder below      * @param sftp      * @param dir      * @param fileName      * @param out













    






     * @return
     */
    public static boolean uploadNew(ChannelSftp sftp, String dir, String fileName, InputStream inputStream) {
        boolean flag = true;
        try {
            if(StringUtils.isBlank(dir)) {
                sftp.cd(BASE_DIR);
            }else{
                sftp.cd(BASE_DIR+dir);
            }
            log.info("=====cd directory success====={}",BASE_DIR+dir);
        } catch (SftpException e) {
            e.printStackTrace();
            try {
                sftp.mkdir(BASE_DIR+dir);
                sftp.cd(BASE_DIR+dir);
                log.info("=====create directory success====={}",BASE_DIR+dir);
            } catch (SftpException e1) {
                flag = false;
                log.info("=====create directory fail====={}",BASE_DIR+dir);
                e1.printStackTrace();
            }
        }
        log.info("=======>dicrectory flag==>{},uploadFile==>{}", flag, fileName);
        try{
            sftp.put(inputStream, fileName+".ing");
            sftp.rename(BASE_DIR+dir + "/" + fileName+".ing", BASE_DIR+dir + "/" + fileName);
            log.info("the fifth step =========>{}", "rename success");
            if(inputStream!=null) {
                inputStream.close();
            }
        } catch (Exception e) {
            flag = false;
            throw new RuntimeException("sftp excetion" + e);
        } finally {
            closeChannel();
        }
        return flag;
    }

    
    /**
     * Close the connection
     */
    public static void closeChannel() {         log.info("=====sftp object closing=====");         if (sftp != null) {             if (sftp.isConnected( )) {                 sftp.disconnect();             }         }         if (sshSession != null) {             if (sshSession.isConnected()) {                 sshSession.disconnect();             }         }     }     /**      * Save the content of the input stream to the file      * @param ins      * @param file      */     private static void inputStreamToFile(InputStream ins, File file) {












    






        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 Code words are not easy, give a like and follow

Guess you like

Origin blog.csdn.net/wangerrong/article/details/126670641