java sftp 多文件上传下载

该代码示例展示了如何使用Java进行SFTP多文件上传和下载的优化操作,包括从本地文件夹上传到服务器文件夹以及从服务器下载到本地。主要涉及FileInputStream、ChannelSftp接口和FileUtils工具类的方法。
摘要由CSDN通过智能技术生成

java sftp 多文件上传下载 优化单文件上传下载

多文件上传:

  • fromPath:本地文件夹的路径
  • upToPath:服务器目标文件夹的路径
    public int uploadFiles(String fromPath, String upToPath) {
        //多文件上传
        fromPath = judgePath(fromPath);
        upToPath = judgePath(upToPath);
        FileInputStream in = null;
        String fileName = null;
        String originPath = null;
        File folder = new File(fromPath);
        File[] listOfFiles = folder.listFiles();
        System.out.println(listOfFiles.length);
        try {
            if (listOfFiles == null){
                return 0;
            }
            for (File file :listOfFiles) {
                fileName = file.getName();
                if (fileName.equals(".") || fileName.equals("..")){
                    continue;
                }
                System.out.println(fileName);
                originPath = fromPath + fileName;
                System.out.println(originPath);
                in = new FileInputStream(new File(originPath));
                channelSftp.put(in,upToPath + fileName);
            }
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return listOfFiles.length;
    }

多文件下载:

  • downloadPath:服务器要下载的文件夹路径
  • toPath:下载到本地文件夹的路径
    public int downloadFiles(String downlownPath, String toPath) {
        //多文件下载
        downlownPath = judgePath(downlownPath);
        toPath = judgePath(toPath);
        Vector files = null;
        InputStream download = null;
        ChannelSftp.LsEntry lsEntry = null;
        File file1 = null;
        String fileName = null;
        String destPath = null;
        try {
            files = channelSftp.ls(downlownPath);
            System.out.println(files.size()-2);
            if (files == null){
                return 0;
            }
            for (Object file :files) {
                lsEntry = (ChannelSftp.LsEntry)file;
                fileName = lsEntry.getFilename();
                if (fileName.equals(".") || fileName.equals("..")){
                    continue;
                }
                System.out.println(fileName);
                download = channelSftp.get(downlownPath + fileName);
                destPath = toPath + fileName;
                System.out.println(destPath);
                //把文件保存到本地
                file1 = new File(destPath);
                FileUtils.copyInputStreamToFile(download, file1);
            }
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            download.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return files.size()-2;
    }

sftp工具类代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值