ftp文件下载或上传没数据问题,ftp的工具类

ftp文件下载或上传没数据问题

开发当中遇到了一个通过ftp同步文件的任务,突然遇到了文件上传或下载失败的问题,由于我的文件要求按照字段给定的多目录上传,在网上也查看了很多帖子没有人提到这个问题,以下是我通过实验得出的结论。

ftp这个家伙比较坑,不管是文件上传还是下载都不支持多目录进行,也就是所必须要先进入到文件所在的目录下然后才能执行上传或下载的操作,不然会报错或者是上传下载失败。废话不多说直接上代码

Scala 版本的

import java.io.{File, FileInputStream, FileOutputStream, IOException}
import org.apache.commons.net.ftp.{FTPClient,FTPReply}
object FtpUtils {
  var ftp: FTPClient = null
  var rootPath:String = null
  /**
   * 登录到ftp 
   */ 
  def ftpLogin(ip: String, port: Int, name: String, pwd: String) = {
    ftp = new FTPClient()
    ftp.enterLocalPassiveMode()
    //验证登录
    try {
      ftp.connect(ip, port);
      ftp.setControlEncoding("UTF-8")
      val reply: Int = ftp.getReplyCode
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect()
        println("FTP服务器连接失败")
      }
      rootPath = ftp.printWorkingDirectory()
      ftp
    } catch {
      case e: IOException => e.printStackTrace()
        ftp
    }
  }

  /**
   * 本地数据上传到ftp
   */
  def putFile(file: String, path: String, ftp: FTPClient) = {
    var isStore: Boolean = false
    try {
      changeWorkDir(file, ftp)
      // 如果分割符是"\" 这里要用File.separator*2
      val filename: String = file.split(File.separator).last
      isStore = ftp.storeFile(filename, new FileInputStream(new File(path)))
      ftp.changeWorkingDirectory(rootPath)
      isStore
    } catch {
      case e: IOException => e.printStackTrace()
        isStore
    }
  }

/**
 * 从ftp 下载数据到本地目录下
 */
  def getFile(infile: String, outFile: String, ftp: FTPClient) = {
    var isLoad: Boolean = false
    try {
      ftp.changeWorkingDirectory(rootPath)
      changeWorkDir(infile,ftp)
      val file: File = new File(outFile)
      if(file.getParentFile!=null){
        file.getParentFile.getAbsoluteFile.mkdirs()
      }
      val out: FileOutputStream = new FileOutputStream(outFile)
      isLoad = ftp.retrieveFile(infile.split(File.separator).last,out)
      ftp.changeWorkingDirectory(rootPath)
      isLoad
    } catch {
      case e: IOException => e.printStackTrace()
        isLoad
    }
  }
  /**
   * 这里将工作将工作目录移动到最下即的目录
   */
  def changeWorkDir(file: String, ftp: FTPClient) = {
    val files: Array[String] = file.split(File.separator)
    for (i <- 0 until files.length - 1) {
      ftp.makeDirectory(files(i))
      ftp.changeWorkingDirectory(files(i))
    }
  }
}

Java版本的

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FtpUtils {
    FTPClient ftp = new FTPClient();
    String rootPath;
    
    public FTPClient ftpLogin(String ip, int port, String name, String pwd) throws IOException {
        ftp.enterLocalPassiveMode();
        ftp.connect(ip, port);
        ftp.setControlEncoding("UTF-8");
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.out.println("FTP服务器连接失败");
        }
        rootPath = ftp.printWorkingDirectory();
        return ftp;
    }

    public Boolean getFile(String infile, String outFile, FTPClient ftp) throws IOException {
        ftp.changeWorkingDirectory(rootPath);
        changeWorkDir(infile, ftp);
        File file = new File(outFile);
        if (file.getParentFile() != null) {
            file.getParentFile().getAbsoluteFile().mkdirs();
        }
        FileOutputStream out = new FileOutputStream(outFile);
        String[] paths = infile.split(File.separator);
        boolean isLoaded = ftp.retrieveFile(paths[paths.length - 1], out);
        ftp.changeWorkingDirectory(rootPath);
        return isLoaded;
    }

    public boolean putFile(String file, String path, FTPClient ftp) throws IOException {
        changeWorkDir(file, ftp);
        // 如果分割符是"\" 这里要两个 File.separator
        String[] paths = file.split(File.separator);
        String filename = paths[paths.length - 1];
        boolean  isStored = ftp.storeFile(filename, new FileInputStream(new File(path)));
        ftp.changeWorkingDirectory(rootPath);
        return isStored;
    }

    public void changeWorkDir(String file, FTPClient ftp) throws IOException {
        String[] paths = file.split(File.separator);
        for (String path : paths) {
            ftp.makeDirectory(path);
            ftp.changeWorkingDirectory(path);
        }
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrSun329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值