最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器

 更新時(shí)間:2018年06月10日 13:56:32   作者:多巴胺二次元式  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

以前做的一個(gè)項(xiàng)目,用到了文件上傳下載至ftp服務(wù)器,現(xiàn)在對(duì)其進(jìn)行一下復(fù)習(xí),比較簡單,一下就能看明白。
環(huán)境:首先,先安裝ftp服務(wù)器,我是在win8本地用IIS配置的, 百度一下就可以找到安裝文檔。

1.在你的項(xiàng)目目錄下建立ftp配置文件,目錄如下圖

01 ftpconfig.properties:

ftpIp=10.73.222.29
ftpPort=21
ftpUser=WP
ftpPwd=04143114wp
ftpRemotePath=d://share 

02 讀取ftpconfig.properties中的具體內(nèi)容的類:

 package com.java.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author wangpei
 * @version 創(chuàng)建時(shí)間: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("關(guān)閉流失敗..");
          e.printStackTrace();
        }
      }

    }

  }

  public String getIp() {// 獲取ftp服務(wù)器的ip地址
    return properties.getProperty("ftpIp");

  }

  public String getPort() {// 獲取ftp服務(wù)器的端口
    return properties.getProperty("ftpPort");

  }

  public String getUser() {// 獲取ftp登錄用戶名
    return properties.getProperty("ftpUser");

  }

  public String getPwd() {// 獲取ftp服務(wù)器的登錄密碼
    return properties.getProperty("ftpPwd");

  }

  public String getRemotePath() {// 獲取ftp服務(wù)器的存放文件的目錄
    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 創(chuàng)建時(shí)間:2017年5月6日 下午6:39:03 
 * 文件上傳下載業(yè)務(wù)邏輯接口層
 */
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 文件上傳下載的接口實(shí)現(xiàn)類

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 創(chuàng)建時(shí)間: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出錯(cuò)");
      logout(client);// 退出/斷開FTP服務(wù)器鏈接
      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);// 創(chuàng)建FTP服務(wù)器目錄
            client.changeWorkingDirectory(onepath);// 改變FTP服務(wù)器目錄
          } 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 {
      // 設(shè)置被動(dòng)模式
      ftpClient.enterLocalPassiveMode();
      // 設(shè)置以二進(jìn)制流的方式傳輸
      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
      // 設(shè)置編輯格式
      ftpClient.setControlEncoding("utf-8");

      remotePath = remotePath.substring(0,
          remotePath.lastIndexOf(fileName));
      fs = ftpClient.listFiles(remotePath);// 遞歸目標(biāo)目錄
      for (FTPFile ff : fs) {
        if (ff.getName().equals(fileName)) {// 查找目標(biāo)文件
          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服務(wù)器鏈接
    }

  }

}

代碼很好理解,看一遍應(yīng)該就可以理解,在這兒就不具體分析了,主要看代碼中的注釋。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

银川市| 新余市| 井研县| 宣汉县| 九龙城区| 科尔| 遂昌县| 高安市| 连江县| 朝阳市| 西乌珠穆沁旗| 翁牛特旗| 绥芬河市| 柏乡县| 秦皇岛市| 九龙城区| 河源市| 太湖县| 奎屯市| 新乐市| 洪湖市| 恭城| 肥东县| 灵宝市| 丹巴县| 博白县| 梅河口市| 吉林市| 南康市| 宝山区| 阳原县| 西华县| 巫溪县| 宁乡县| 奉节县| 渭南市| 盐边县| 阿拉善左旗| 江陵县| 双牌县| 青岛市|