ftp,sftp读取文件

// ftp读取文件
package com.zway.lcs.an.util;

import cn.hutool.core.io.FileUtil;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.SocketException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.*;
import org.apache.commons.io.FileUtils;
@Slf4j
public class FtpUtils {

    public static void main(String[] args) throws Exception {
        FtpUtils ftpTools = new FtpUtils("192.168.182.222", 21, "ftpuser", "zway@123456");
        ftpTools.connectClient();
        File file = ftpTools.readFtpforFile("/home/test", "menu.sql");
        List<String> strings = ftpTools.readConfigFileForFTP("/home/test", "menu.sql");
        ftpTools.closeServer();
    }


    private String ftpHost;                                        // IP
    private int ftpPort;                                            // port
    private String ftpUserName;                                    // 用户
    private String ftpPassword;                                    // 密码
    private FTPClient ftpClient;                                    // IP

    public FtpUtils(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
        this.ftpHost = ftpHost;
        this.ftpPort = ftpPort;
        this.ftpUserName = ftpUserName;
        this.ftpPassword = ftpPassword;
    }

    /**
     * 连接ftp服务
     *
     * @return
     * @throws Exception
     */
    public FTPClient connectClient1() {
        log.info("***********************登录FTP***********************");
        ftpClient = new FTPClient();
        try {
            ftpClient.setConnectTimeout(1000 * 30);//设置连接超时时间
            ftpClient.setControlEncoding("utf-8");//设置ftp字符集
            ftpClient.enterLocalPassiveMode();//设置被动模式,文件传输端口设置
            ftpClient.setBufferSize(8096);
            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                log.info("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                log.info("***********************登录FTP成功***********************");
            }
        } catch (SocketException e) {
            log.error("FTP的IP地址可能错误,请正确配置。message={}", e);
        } catch (IOException e) {
            log.error("FTP的端口错误,请正确配置。message={}", e);
        } catch (Exception e) {
            log.error("FTP连接失败。message={}", e);
        }
        return ftpClient;
    }


    /**
     * 连接ftp服务
     *
     * @return
     * @throws Exception
     */
    public boolean connectClient() {
        log.info("***********************登录FTP***********************");
        ftpClient = new FTPClient();
        boolean connected = false;
        try {
            ftpClient.setConnectTimeout(1000 * 30);//设置连接超时时间
            ftpClient.setControlEncoding("utf-8");//设置ftp字符集
            ftpClient.enterLocalPassiveMode();//设置被动模式,文件传输端口设置
            ftpClient.setBufferSize(8096);
            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            connected = ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                log.info("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                log.info("***********************登录FTP成功***********************");
            }
        } catch (SocketException e) {
            log.error("FTP的IP地址可能错误,请正确配置。message={}", e);
        } catch (IOException e) {
            log.error("FTP的端口错误,请正确配置。message={}", e);
        } catch (Exception e) {
            log.error("FTP连接失败。message={}", e);
        }
        return connected;
    }

    /**
     * 检验指定路径的文件是否存在ftp服务器中
     *
     * @param filePath--指定绝对路径的文件
     * @return
     */
    public boolean isFTPFileExist(String filePath) {
        // 判断文件是否存在
        Boolean isexist = false;
        try {
            // 提取绝对地址的目录以及文件名
            filePath = filePath.replace("ftp://" + ftpHost + ":" + ftpPort + "/", "");
            String dir = filePath.substring(0, filePath.lastIndexOf("/"));
            String file = filePath.substring(filePath.lastIndexOf("/") + 1);
            // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
            ftpClient.changeWorkingDirectory(new String(dir.getBytes("GBK")));
            // 检验文件是否存在
            InputStream is = ftpClient.retrieveFileStream(new String(file.getBytes("GBK")));
            if (is == null || ftpClient.getReplyCode() == FTPReply.FILE_UNAVAILABLE) {
                return isexist;
            }
            if (is != null) {
                isexist = true;
                is.close();
                ftpClient.completePendingCommand();
            }
            return isexist;
        } catch (Exception e) {
            log.error("检验ftp指定路径的文件是否存在失败,message={}", e);
        }
        return isexist;
    }

    /**
     * 下载ftp服务器文件方法
     *
     * @param downloadFile      原文件(路径+文件名)
     * @param saveFileDirectory 下载路径
     * @return
     * @throws IOException
     */
    public List<String> downFile(String directory, String downloadFile, String saveFileDirectory) throws IOException {
        File localFile = new File(saveFileDirectory);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
        OutputStream os = null;
        os = new FileOutputStream(localFile);
        ftpClient.retrieveFile(new String(downloadFile.getBytes(), Charsets.UTF_8), os);
        os.close();
        log.info("下载成功--------");
        String saveFile = saveFileDirectory + "//" + downloadFile;
        File file = new File(saveFile);
        return FileUtil.readLines(file, Charsets.UTF_8);
    }


    private void readFiles(FTPClient ftpClient) throws IOException {
        ftpClient.changeWorkingDirectory("/aaa/bbb");
        final FTPFile[] ftpFiles = ftpClient.listFiles();
        for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.isFile()) {
                System.out.println("********文件" + ftpFile.getName() + "的内容如下**********");

                InputStream inputStream = ftpClient.retrieveFileStream(ftpFile.getName());
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                    while (true) {
                        final String s = reader.readLine();
                        if (s == null) {
                            break;
                        }
                        System.out.println(s);
                    }
                }
                inputStream.close();
                ftpClient.completePendingCommand(); // 每当读完一个文件时,要执行该语句
            }
        }
    }


    public static void mappedFile(Path filename) {
        try (FileChannel fileChannel = FileChannel.open(filename)) {
            long size = fileChannel.size();
            MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
            for (int i = 0; i < size; i++) {
                mappedByteBuffer.get(i);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @return File
     */
    public File readFtpforFile( String ftpPath, String fileName) {
        InputStream in = null;
        log.info("开始读取绝对路径" + ftpPath + "文件!");
        String remoteFile = ftpPath + "/" + fileName;
        File newFile = new File(remoteFile);
        try {
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
//            in = ftpClient.retrieveFileStream(fileName);
            // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
            ftpClient.changeWorkingDirectory(ftpPath);
            in = ftpClient.retrieveFileStream(ftpPath+"/"+fileName);
            FileUtils.copyToFile(in, newFile);
            in.close();
            ftpClient.completePendingCommand(); // 每当读完一个文件时,要执行该语句
        } catch (FileNotFoundException e) {
            log.error("没有找到" + ftpPath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            log.error("连接FTP失败.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件读取错误。");
            e.printStackTrace();
        }
        return newFile;
    }

    /**
     * 去 服务器的FTP路径下上读取文件
     *
     * @param ftpPath
     * @param fileName
     * @return String
     */
    public List<String> readConfigFileForFTP(String ftpPath, String fileName) {
        StringBuffer resultBuffer = new StringBuffer();
        List<String> strings = new ArrayList<>();
        InputStream in = null;
        log.info("开始读取绝对路径" + ftpPath + "文件!");
        String remoteFile = ftpPath + "/" + fileName;
        File newFile = new File(remoteFile);
        try {
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
//            in = ftpClient.retrieveFileStream(fileName);
            // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
            ftpClient.changeWorkingDirectory(ftpPath);
            in = ftpClient.retrieveFileStream(ftpPath+"/"+fileName);
//            FileUtils.copyToFile(in, newFile);
        } catch (FileNotFoundException e) {
            log.error("没有找到" + ftpPath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            log.error("连接FTP失败.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件读取错误。");
            e.printStackTrace();
        }
        if (in != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String data = null;
            try {
                while ((data = br.readLine()) != null) {
                    resultBuffer.append(data + "\n");
                    strings.add(data);
                    System.out.println(data);
                }
                in.close();
                ftpClient.completePendingCommand(); // 每当读完一个文件时,要执行该语句
            } catch (IOException e) {
                log.error("文件读取错误。");
                e.printStackTrace();
            }
        } else {
            log.error("in为空,不能读取。");
        }
        return strings;
    }

    //按照value的大小排序
    public static Map<String, Long> ftpsortMap(Map<String, Long> k_v) {
        List<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>(k_v.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
            @Override
            public int compare(Map.Entry<String, Long> o1,
                               Map.Entry<String, Long> o2) {
                // 升序排序
                return (int) (o2.getValue() - o1.getValue());
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry<String, Long> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    public Map<String, Long> ftpFilesAndTime(String dir) throws SftpException, IOException {
        Calendar calendar = new GregorianCalendar();
        Map<String, Long> map1 = new HashMap<>();
        //获取ftp目录下所有文件
        FTPFile[] files = ftpClient.listFiles(dir);
        //文件放入自定义集合
        for (FTPFile f : files) {
            map1.put(f.getName(), f.getTimestamp().getTimeInMillis());
        }
        return map1;
    }


    /**
     * ftp服务文件下载到指定目录
     *
     * @param ftpPath   FTP服务器中文件所在路径
     * @param localPath 下载到本地的位置
     * @param fileName  文件名称
     */
    public List<String> downloadFtpFile(String ftpPath, String localPath, String fileName) throws Exception {
        //判断文件夹是否存在, 不存在创建
        makeDirs(localPath);
        ftpClient.setControlEncoding("GBK"); // 中文支持
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(StringUtils.trim(ftpPath));
        File file = new File(localPath + "/" + fileName);
        OutputStream os = new FileOutputStream(file);
        ftpClient.retrieveFile(new String(file.getName().getBytes(), "ISO-8859-1"), os);
        List<String> strings = FileUtil.readLines(file, Charsets.UTF_8);
        ftpClient.deleteFile(localPath + "/" + fileName);
        os.close();
        return strings;
    }

    /**
     * 关闭FTP服务器
     */
    public void closeServer() {
        try {
            if (ftpClient != null) {
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            log.error("ftpl连接关闭失败,message={}", e);
        }

    }

    /**
     * 判断本地路径是否存在,不存在就创建路径
     */
    public void makeDirs(String localSavePath) {
        File localFile = new File(localSavePath);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
    }
}



// sftp读取文件
package com.zway.lcs.an.util;
import cn.hutool.core.io.FileUtil;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class SftpUtils {
    public static void main(String[] args) throws FileNotFoundException,SftpException {
        SftpUtils fiel = new SftpUtils("root","zway@123456","192.168.182.222",22);
        ChannelSftp sftp = fiel.sftp;
        Boolean login = fiel.login();
        fiel.download("/home/test","role.sql","C:\\Users\\DELL\\Desktop\\testFtp");
        ArrayList<String> strings = fiel.listFiles("/home/test");
        Map<Integer,String > maps = fiel.FilesAndTime("/home/test");
        Map<Integer, String> integerStringMap = SftpUtils.sortMap(maps);
        String lastModifiedTime = fiel.getLastModifiedTime("/home/test/nsa_info.2022-03-11-0.log");
        //测试上传功能
        File file = new File("C:\\Users\\DELL\\Desktop\\testFtp\\test.csv");
        InputStream is = new FileInputStream(file);
        fiel.upload("/home/test","","1.csv",is);
        //测试下载功能
        try {
            fiel.download("/home/test","1.csv","C:\\Users\\DELL\\Desktop\\testFtp");
            fiel.delete("C:\\Users\\DELL\\Desktop\\testFtp\\1.csv");
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //用户名
    private String username;
    //密码
    private String password;
    //ip
    private String host;
    //端口一般为22
    private int port;
    //私钥
    private String privateKey;

    ChannelSftp sftp = null;
    //通过构造方法传参
    public SftpUtils(String username, String password, String host, int port){
        this.username = username;
        this.password = password;
        this.host = host;
        this.port = port;
    }
    public SftpUtils(String username, String host, int port, String privateKey){
        this.username = username;
        this.host = host;
        this.port = port;
        this.privateKey = privateKey;
    }

    //登录,检查链接情况
    public ChannelSftp loginChannelSftp(){
        try {
            JSch jSch = new JSch();
            if(privateKey != null){
                jSch.addIdentity(privateKey);
            }
            Session session = jSch.getSession(username,host,port);
            if(password != null){
                session.setPassword(password);
            }
            session.setTimeout(100000);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            log.info("登录成功--------");
        } catch (JSchException e) {
            log.info("登录失败--------");
        }
        return  sftp;
    }


    //登录,检查链接情况
    public Boolean login(){
        boolean connected=false;
        try {
            JSch jSch = new JSch();
            if(privateKey != null){
                jSch.addIdentity(privateKey);
            }
            Session session = jSch.getSession(username,host,port);
            if(password != null){
                session.setPassword(password);
            }
            session.setTimeout(100000);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            connected=sftp.isConnected();
            log.info("登录成功--------");
        } catch (JSchException e) {
            log.info("登录失败--------");
        }
        return  connected;
    }


    //上传文件
    /**
     * @param basePath 目标路径
     * @param direcotry 目标子路径
     * @param sftpFileName 文件名称
     */
    public void  upload(String basePath, String direcotry, String sftpFileName, InputStream inputStream) throws SftpException{
        try {
            //进入到目标目录
            sftp.cd(basePath);
            sftp.cd(direcotry);
        } catch (SftpException e) {
            String[] dirs = direcotry.split("/");
            String temPath = basePath;
            for (String dir: dirs
            ) {
                if( null == dir || "".equals(dir)){ continue;};
                temPath +="/" + dir;
                try {
                    sftp.cd(temPath);
                } catch (SftpException ex) {
                    sftp.mkdir(temPath);
                    sftp.cd(temPath);
                }
            }
        }
        sftp.put(inputStream,sftpFileName);
        log.info("上传成功--------");
    }
    //删除
    /**
     * @param saveFile 下载的文件路径
     */
    public  void delete(String saveFile){
        File file = new File(saveFile);
        FileUtil.del(file);
        log.info("删除成功--------");
    }

    //下载文件
    /**
     * @param directory 下载的文件路径
     * @param downloadFile 下载的文件名
     * @param saveFileDirectory 保存的文件路径
     */
    public List<String> download(String directory, String downloadFile, String saveFileDirectory) throws SftpException, FileNotFoundException {
        File localFile = new File(saveFileDirectory);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
        if(directory != null && !"".equals(directory)){
            sftp.cd(directory);
        }
        String saveFile = saveFileDirectory + "//" + downloadFile;
        File file = new File(saveFile);
        sftp.get(downloadFile, new FileOutputStream(file));
        log.info("下载成功--------");
        return FileUtil.readLines(file, Charsets.UTF_8);
    }

    /**
     * @return File
     */
    public  File   readSFTPforFile(  ChannelSftp sftp,String remoteFile) {
        File newFile = new File(remoteFile);
        //将InputStream转File
        InputStream in = null;
        log.info("开始读取绝对路径" + remoteFile + "文件!");
        try {
            in=sftp.get(remoteFile);
            FileUtils.copyToFile(in, newFile);
        } catch (SftpException | IOException e) {
            e.printStackTrace();
            log.error("文件读取错误。");
        }
        if (in==null){
            log.error("in为空,不能读取。");
        }
            return newFile;
    }

    /**
     * 去 服务器的FTP路径下上读取文件
     *
     * @param sftpPath
     * @param fileName
     * @return String
     */
    public  List<String>   readConfigFileForsFTP(String sftpPath, String fileName) {
        StringBuffer resultBuffer = new StringBuffer();
        List<String> strings=new ArrayList<>();
        InputStream in = null;
        log.info("开始读取绝对路径" + sftpPath + "文件!");
        try {
            String remoteFile = sftpPath +"/"+ fileName;
            in=sftp.get(remoteFile);
        } catch (SftpException e) {
            e.printStackTrace();
            log.error("文件读取错误。");
        }
        if (in != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String data = null;
            try {
                while ((data = br.readLine()) != null) {
                    resultBuffer.append(data + "\n");
                    strings.add(data);
                }
                in.close();
            } catch (IOException e) {
                log.error("文件读取错误。");
                e.printStackTrace();
            }
        }else{
            log.error("in为空,不能读取。");
        }
        return strings;
    }

    /**
     *  获取文件创建时间
     * @param srcSftpFilePath
     * @return
     */
    public   String getLastModifiedTime(String srcSftpFilePath){
        try {
        SftpATTRS sftpATTRS = sftp.lstat(srcSftpFilePath);
        int mTime = sftpATTRS.getMTime();
        Date lastModified = new Date(mTime * 1000L);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(lastModified);
        return time;
    } catch (Exception e) {
        e.printStackTrace();
    }
        return null;
}

    //按照key的大小排序
    public   static  Map<Integer, String> sortMap(Map<Integer, String> k_v) {
        List<Map.Entry<Integer, String>> list = new ArrayList<Map.Entry<Integer, String>>(k_v.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
            @Override
            public int compare(Map.Entry<Integer, String> o1,
                               Map.Entry<Integer, String> o2) {
                // 升序排序
                return o2.getKey()- o1.getKey();
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry<Integer, String> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    /**
     * 获取文件传教时间和文件名称
     * @param dir
     * @return
     * @throws SftpException
     */
    public Map<Integer, String> FilesAndTime(String dir) throws SftpException{
        Map<Integer, String> map1=new HashMap<>();
        sftp.cd(dir);
        Vector<String> lss = sftp.ls("*");
        for (int i = 0; i < lss.size(); i++) {
            Object obj = lss.elementAt(i);
            if (obj instanceof ChannelSftp.LsEntry) {
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) obj;
                if (true && !entry.getAttrs().isDir()) {
                    String filename=entry.getFilename();
                    SftpATTRS sftpATTRS = sftp.lstat(dir+"/"+filename);
                    int mTime = sftpATTRS.getMTime();
                    map1.put(mTime,filename);
                }
                if (true && entry.getAttrs().isDir()) {
                    if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
                        String filename=entry.getFilename();
                        SftpATTRS sftpATTRS = sftp.lstat(dir+"/"+filename);
                        int mTime = sftpATTRS.getMTime();
                        map1.put(mTime,filename);
                    }
                }
            }
        }
        return map1;
    }



    public ArrayList<String> listFiles(String dir) throws SftpException{
        ArrayList<String> files = new ArrayList<String>();
        sftp.cd(dir);
        Vector<String> lss = sftp.ls("*");
        for (int i = 0; i < lss.size(); i++) {
            Object obj = lss.elementAt(i);
            if (obj instanceof ChannelSftp.LsEntry) {
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) obj;
                if (true && !entry.getAttrs().isDir()) {
                    files.add(entry.getFilename());
                }
                if (true && entry.getAttrs().isDir()) {
                    if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
                        files.add(entry.getFilename());
                    }
                }
            }
        }
        return files;
    }



    //登出
    public void destroy(ChannelSftp sftp){
        try {
            if (sftp != null) {
                sftp.disconnect();
            }
            log.info("ftp conn destroyed");
        } catch (Exception e) {
            log.error("ftp conn destroy failed ", e);
        }
    }


}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值