一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語(yǔ)言|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器

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

2021-05-08 11:05多巴胺二次元式 Java教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳下載至ftp服務(wù)器的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

01 ftpconfig.properties:

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

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 文件上傳下載的接口類(lèi)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)類(lèi)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 類(lèi)說(shuō)明
 */
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("無(wú)法連接到ftp");
      return false;
    } catch (socketexception e) {
      system.out.println("無(wú)法連接到ftp");
      return false;
    } catch (ioexception e) {
      system.out.println("無(wú)法連接到ftp");
      return false;
    }
    client.setcontrolencoding("uft-8");
    try {
      b = client.login(ftpuser, ftppwd);
    } catch (ioexception e) {
      system.out.println("登錄ftp出錯(cuò)");
      logout(client);// 退出/斷開(kāi)ftp服務(wù)器鏈接
      return false;
    }
    return b;
 
  }
 
  public boolean logout(ftpclient client) {
    boolean b = false;
 
    try {
      b = client.logout();// 退出登錄
      client.disconnect();// 斷開(kāi)連接
    } 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);// 退出/斷開(kāi)ftp服務(wù)器鏈接
    }
 
  }
 
}

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/wangpei555/article/details/71305603

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草视频国产 | 美女张开腿让我了一夜 | 扒开女人下面 | 国产免费一区二区三区免费视频 | 亚洲精品永久免费 | 国产91成人精品亚洲精品 | 欧美日韩精品在线观看 | 1769在线观看 | 狠狠狠地啪香蕉 | 黑人巨荃大战乌克兰美女 | 国内精品露脸在线视频播放 | 亲爱的客栈第二季免费观看完整版 | 免费成年网站 | 麻豆最新地址 | 盲井在线 | 日日视频 | 1024免费福利永久观看网站 | 天天色天天色天天色 | 成人影院免费看 | 国产一区在线看 | 亚洲国产AV一区二区三区四区 | 欧美vpswindows | 精品国产91久久久久 | 色多多视频在线 | 天天综合天天色 | 久久精品动漫网一区二区 | 女女性恋爱免费 | 天天综合五月天 | 国产一区二区三区毛片 | 桥本有菜ssni-677在线观看 | 免费国产高清精品一区在线 | 国产嫩草视频 | 香港三级系列在线播放 | 国色天香论坛社区在线视频 | 国产成人精品一区二区不卡 | 午夜亚洲WWW湿好爽 午夜想想爱午夜剧场 | 污污的动态图合集 | 91短视频版高清在线观看免费 | 欧美精品亚洲精品日韩1818 | 免费看1级伦理 | 精品久久免费视频 |