欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现文件上传下载至ftp服务器

程序员文章站 2023-12-17 21:46:46
以前做的一个项目,用到了文件上传下载至ftp服务器,现在对其进行一下复习,比较简单,一下就能看明白。 环境:首先,先安装ftp服务器,我是在win8本地用iis配置的,...

以前做的一个项目,用到了文件上传下载至ftp服务器,现在对其进行一下复习,比较简单,一下就能看明白。
环境:首先,先安装ftp服务器,我是在win8本地用iis配置的, 百度一下就可以找到安装文档。

1.在你的项目目录下建立ftp配置文件,目录如下图

java实现文件上传下载至ftp服务器

01 ftpconfig.properties:

ftpip=10.73.222.29
ftpport=21
ftpuser=wp
ftppwd=04143114wp
ftpremotepath=d://share 

02 读取ftpconfig.properties中的具体内容的类:

 package com.java.core.util;

import java.io.ioexception;
import java.io.inputstream;
import java.util.properties;

/**
 * @author wangpei
 * @version 创建时间:2017年5月6日 下午9:42:40 读取ftp文件的配置文件
 */
public class readftpproperties {
  private inputstream is;
  private properties properties;

  public readftpproperties() {
    is = this.getclass().getresourceasstream("/ftpconfig.properties");// 将配置文件读入输入流中
    properties = new properties();
    try {
      properties.load(is);
    } catch (ioexception e) {
      system.out.println("配置文件不存在..");
      e.printstacktrace();
    } finally {

      if (null != is) {

        try {
          is.close();
        } catch (ioexception e) {
          system.out.println("关闭流失败..");
          e.printstacktrace();
        }
      }

    }

  }

  public string getip() {// 获取ftp服务器的ip地址
    return properties.getproperty("ftpip");

  }

  public string getport() {// 获取ftp服务器的端口
    return properties.getproperty("ftpport");

  }

  public string getuser() {// 获取ftp登录用户名
    return properties.getproperty("ftpuser");

  }

  public string getpwd() {// 获取ftp服务器的登录密码
    return properties.getproperty("ftppwd");

  }

  public string getremotepath() {// 获取ftp服务器的存放文件的目录
    return properties.getproperty("ftpremotepath");

  }

}

03 文件上传下载的接口类

package com.java.web.service;

import java.io.inputstream;
import org.apache.commons.net.ftp.ftpclient;
import com.java.core.util.readftpproperties;


/**
 * @author wangpei
 * @version 创建时间:2017年5月6日 下午6:39:03 
 * 文件上传下载业务逻辑接口层
 */
public interface ftpservice {
  /*
   * 登录至ftp
   */
  public boolean loginftp(ftpclient client, readftpproperties rfp);

  /*
   * 退出ftp
   */
  public boolean logout(ftpclient client);//

  /*
   * 上传文件到remotepath,其在ftp上的名字为inputstream
   */
  public boolean uploadfile(ftpclient client, string remotepath,
      string filenewname, inputstream inputstream, readftpproperties rfp);

  /*
   * 从目录remotepath,下载文件filename
   */
  public inputstream downfilebyftp(ftpclient client, string remotepath,
      string filename);

  /*
   * 删除ftp上的目录为pathname的文件
   */
  public boolean delfile(ftpclient client, string pathname);

}

04 文件上传下载的接口实现类

package com.java.web.service.serviceimpl;

import java.io.ioexception;
import java.io.inputstream;
import java.io.unsupportedencodingexception;
import java.net.socketexception;
import org.apache.commons.net.ftp.ftp;
import org.apache.commons.net.ftp.ftpclient;
import org.apache.commons.net.ftp.ftpfile;
import com.java.core.util.readftpproperties;
import com.java.web.service.ftpservice;

/**
 * @author wangpei
 * @version 创建时间:2017年5月6日 下午10:02:28 类说明
 */
public class ftpserviceimpl implements ftpservice {

  public boolean loginftp(ftpclient client, readftpproperties rfp) {
    string ftpip = rfp.getip();
    string ftpport = rfp.getport();
    string ftpuser = rfp.getuser();
    string ftppwd = rfp.getpwd();
    // string fgtpremotepath = rfp.getremotepath();
    boolean b = false;

    try {
      client.connect(ftpip, integer.parseint(ftpport));
    } catch (numberformatexception e) {
      system.out.println("无法连接到ftp");
      return false;
    } catch (socketexception e) {
      system.out.println("无法连接到ftp");
      return false;
    } catch (ioexception e) {
      system.out.println("无法连接到ftp");
      return false;
    }
    client.setcontrolencoding("uft-8");
    try {
      b = client.login(ftpuser, ftppwd);
    } catch (ioexception e) {
      system.out.println("登录ftp出错");
      logout(client);// 退出/断开ftp服务器链接
      return false;
    }
    return b;

  }

  public boolean logout(ftpclient client) {
    boolean b = false;

    try {
      b = client.logout();// 退出登录
      client.disconnect();// 断开连接
    } catch (ioexception e) {
      return false;
    }
    return b;

  }

  public boolean uploadfile(ftpclient client, string remotepath,
      string filenewname, inputstream inputstream, readftpproperties rfp) {
    boolean b = false;
    try {
      client.setfiletype(ftpclient.binary_file_type);
      client.enterlocalpassivemode();
      if (remotepath != null && !"".equals(remotepath.trim())) {
        string[] pathes = remotepath.split("/");
        for (string onepath : pathes) {
          if (onepath == null || "".equals(onepath.trim())) {
            continue;
          }

          onepath = new string(onepath.getbytes("utf-8"),
              "iso-8859-1");
          system.out.println("onepath=" + onepath);
          if (!client.changeworkingdirectory(onepath)) {
            client.makedirectory(onepath);// 创建ftp服务器目录
            client.changeworkingdirectory(onepath);// 改变ftp服务器目录
          } else {
            system.out.println("文件单路径");
          }
        }
      }
      b = client.storefile(new string(filenewname.getbytes("utf-8"),
          "iso-8859-1"), inputstream);
    } catch (unsupportedencodingexception e) {
      return false;
    } catch (ioexception e) {
      return false;
    }
    return b;
  }

  public inputstream downfilebyftp(ftpclient ftpclient, string remotepath,
      string filename) {

    ftpfile[] fs;
    inputstream is = null;
    try {
      // 设置被动模式
      ftpclient.enterlocalpassivemode();
      // 设置以二进制流的方式传输
      ftpclient.setfiletype(ftp.binary_file_type);
      // 设置编辑格式
      ftpclient.setcontrolencoding("utf-8");

      remotepath = remotepath.substring(0,
          remotepath.lastindexof(filename));
      fs = ftpclient.listfiles(remotepath);// 递归目标目录
      for (ftpfile ff : fs) {
        if (ff.getname().equals(filename)) {// 查找目标文件
          is = ftpclient.retrievefilestream(new string(
              (remotepath + filename).getbytes("utf-8"),
              "iso-8859-1"));
          break;
        }
      }

    } catch (ioexception e) {

      e.printstacktrace();
    }
    return is;

  }

  public boolean delfile(ftpclient ftpclient, string pathname) {
    boolean b = false;

    try {
      b = ftpclient.deletefile(pathname);

      return b;
    } catch (exception e) {
      return false;
    } finally {
      logout(ftpclient);// 退出/断开ftp服务器链接
    }

  }

}

代码很好理解,看一遍应该就可以理解,在这儿就不具体分析了,主要看代码中的注释。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: