⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpprocess.java

📁 FTP上传程序
💻 JAVA
字号:
package com.aspire.xportal.ftp;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;

import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.*;

import com.aspire.xportal.ftp.config.*;
import com.aspire.xportal.ftp.util.*;

public class FtpProcess {

  public final static String TRANSFER_ASC = "ASC"; //ascii 传输模式
  public final static String TRANSFER_BIN = "BIN"; //binary传输模式

  protected FTPClient ftp;
  protected PrintWriter log;
  protected FTPConnectMode ftpConnectMode;

  public FtpProcess() {

  }

  /**
   *初始化连接,日志等
   * @throws IOException
   */
  public void initConfig() throws IOException {

    initFtpConnectMode();
    try {
      initLog();
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }

  /**
   * 设置链接模式
   */
  private void initFtpConnectMode() {
    if (FtpConf.getConnectMode().equalsIgnoreCase("active")) {
      ftpConnectMode = FTPConnectMode.ACTIVE;
    }
    else {
      ftpConnectMode = FTPConnectMode.PASV;
    }
  }

  /**
   * 初始化日志
   * @throws IOException
   */
  private void initLog() throws IOException {
    log = new PrintWriter(new FileWriter(FtpConf.getLocalLog(), true));
  }

  /**
   * ftp连接
   * @throws IOException
   * @throws FTPException
   */
  protected void connect() throws IOException, FTPException {
    ftp = new FTPClient(FtpConf.getHost(), FtpConf.getPort());
    ftp.debugResponses(true);
    ftp.setLogStream(log);
    ftp.setConnectMode(ftpConnectMode);
  }

  /**
   * ftp登陆
   * @throws IOException
   * @throws FTPException
   */

  protected void login() throws IOException, FTPException {
    ftp.login(FtpConf.getUserName(), FtpConf.getPassword());
    setType(TRANSFER_ASC);
  }

  /**
   * 设置传输类型
   * @param transferType String
   * @throws IOException
   * @throws FTPException
   */
  public void setType(String transferType) throws IOException, FTPException {
    if (transferType == null) {
      return;
    }
    if (TRANSFER_ASC.equalsIgnoreCase(transferType)) {
      ftp.setType(FTPTransferType.BINARY);
    }
    if (TRANSFER_BIN.equalsIgnoreCase(transferType)) {
      ftp.setType(FTPTransferType.ASCII);
    }
  }

  /**
   * 取得远程目录中文件列表
   * @param remoteDir String 远程目录
   * @throws IOException
   * @throws FTPException
   * @return String[]
   */
  public String[] getRemoteDirList(String remoteDir) throws IOException,
      FTPException {
    return ftp.dir(remoteDir);
  }

  /**
   * 取得本地文件列表,包括路径
   * @param localDir String
   * @param fileType String 文件类型
   * @throws Exception
   * @return String[]
   */
  public String[] getLocalDirList(String localDir, String fileType) throws
      Exception {

    return FileUtil.getLocalDirList(localDir, fileType);

  }

  /**
   * 下载远程目录指定文件
   * @param factLocalDir String  本地文件存放目录
   * @param factRemoteDir String 远程文件名称,包括路径
   * @throws Exception
   * @return boolean
   */
  public boolean transferFile(String factLocalDir, String factRemoteFile) throws
      Exception {

    try
    {
      String msg = "";
      logg(factRemoteFile);
      logg(factLocalDir);

      String[] files = getRemoteDirList(factRemoteFile);
      if (files.length == 0) {
        msg = "文件不存在:" + factRemoteFile;
        System.out.println(msg);
        return false;
      }
      mkDir(factLocalDir);

      ftp.get(factLocalDir + "/" + getFileNameByPath(factRemoteFile),
              factRemoteFile);
      logg("传输文件:" + factRemoteFile + "成功");
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 下载远程目录中的所有文件
   * @param factLocalDir String  本地文件存放目录
   * @param factRemoteDir String 远程文件存放目录
   * @throws Exception
   * @return boolean
   */
  public boolean transferDir(String factLocalDir, String factRemoteDir, String fileType) throws
      Exception {

    if(fileType == null) fileType = "";
    String msg = "";
    logg(factRemoteDir);
    logg(factLocalDir);
    try {
      String[] files = getRemoteDirList(factRemoteDir);
      if (files.length == 0) {
        msg = "文件或目录不存在:" + factRemoteDir;
        logg(msg);
        return false;
      }
      mkDir(factLocalDir);

      //ftp.chdir(factRemoteDir);

      for (int j = 0; j < files.length; j++) {
        logg(files[j]);
        String exStr = files[j].substring(files[j].length() -
                                          fileType.length());
        if(fileType.equalsIgnoreCase(exStr) || fileType.equals(""))
        {
          ftp.get(factLocalDir + "/" + getFileNameByPath(files[j]), files[j]);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 下载文件
   * @param type String 类型(sms,wap,pda)
   * @param dateStr String 日期(yyyymm)
   * @throws Exception
   * @return boolean
   */
  public boolean transferText(String type, String dateStr, String fileType) throws Exception {

    String msg = "";

    String factRemoteDir = "";
    String factLocalDir = "";

    factRemoteDir = getFactRemoteDir(type, dateStr);
    factLocalDir = getFactLocalDir(type, dateStr);
    logg(factRemoteDir);
    logg(factLocalDir);
    return transferDir(factLocalDir, factRemoteDir, fileType);
  }

  /**
   *
   * @param type String
   * @param dateStr String
   * @return String
   */
  public String getFactRemoteDir(String type, String dateStr) {
    return FtpConf.getFactRemoteDir(type, dateStr);
  }

  /**
   *
   * @param type String
   * @param dateStr String
   * @return String
   */

  public String getFactLocalDir(String type, String dateStr) {
    return FtpConf.getFactLocalDir(type, dateStr);
  }

  /**
   *
   * @param path String
   * @return String
   */
  private String getFileNameByPath(String path) {
    int pos = path.lastIndexOf("/");
    return path.substring(pos + 1);

  }

  /**
   * 按日期传输指定的文件目录
   * @param dateStr String 日期 yyyymm
   * @throws Exception
   */
  public void TransferText(String dateStr) throws Exception {

    log.println("TransferText()");

    setType(this.TRANSFER_ASC);

    String[] types = FtpConf.getTypes();

    for (int i = 0; i < types.length; i++) {
      log.println("TransferType:" + types[i]);

      if (transferText(types[i], dateStr, "")) {
        String[] list = getRemoteDirList(getFactRemoteDir(types[i],
            dateStr));
        for (int j = 0; j < list.length; j++) {
          ftp.delete(list[j]);
        }
        ftp.chdir(getFactRemoteDir(types[i], dateStr));
        ftp.chdir("..");
        ftp.rmdir(getFactRemoteDir(types[i], dateStr));
      }
    }

  }

  /**
   * 创建目录
   * @param path String
   * @throws Exception
   * @return boolean
   */
  public boolean mkDir(String path) throws Exception {
    String msg = null;
    java.io.File dir;

    dir = new java.io.File(path);
    if (dir == null) {
      msg = "错误原因:对不起,不能创建空目录!";
      throw new Exception(msg);
    }
    if (dir.isFile()) {
      msg = "错误原因:已有同名文件" + dir.getAbsolutePath() + "存在。";
      throw new Exception(msg);
    }
    if (!dir.exists()) {
      boolean result = dir.mkdirs();
      if (result == false) {
        msg = "错误原因:目录" + dir.getAbsolutePath() +
            "创建失败,原因不明!";
        throw new Exception(msg);
      }
      return true;
    }
//        else {
//            msg = "错误原因:目录" + dir.getAbsolutePath() + "已存在。";
//        }
    return true;
  }

  public void logg(String str) {
    System.out.println(str);
  }

  public void logg(String[] arrayStr) {
    for (int i = 0; i < arrayStr.length; i++) {
      System.out.println(arrayStr[i]);
    }
  }

  /**
   * 退出ftp
   */
  public void quit() {
    try {
      if (ftp != null) {
        ftp.quit();
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {

    FtpProcess ftp = new FtpProcess();
    try {
      ftp.initConfig();
      ftp.connect();
      ftp.login();
      //ftp.TransferText("200403");

      ftp.logg(ftp.getRemoteDirList("/opt/wap/report/sms/200403"));
      ftp.quit();
      //String[] arrayStr = ftp.getLocalDirList("e:\\ftp","");
      //ftp.logg(arrayStr);
    }
    catch (IOException ioe) {
      ioe.printStackTrace();
      ftp.quit();
    }
    catch (FTPException ftpe) {
      ftpe.printStackTrace();
      ftp.quit();
    }
    catch (Exception e) {
      e.printStackTrace();
      ftp.quit();
    }

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -