激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - Java實(shí)現(xiàn)FTP文件的上傳和下載功能的實(shí)例代碼

Java實(shí)現(xiàn)FTP文件的上傳和下載功能的實(shí)例代碼

2020-07-07 12:39一個(gè)弱者想變強(qiáng) JAVA教程

FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡(jiǎn)稱(chēng),而中文簡(jiǎn)稱(chēng)為“文傳協(xié)議”。接下來(lái)通過(guò)本文給大家實(shí)例講解Java實(shí)現(xiàn)FTP文件的上傳和下載功能,需要的的朋友一起看看吧

FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡(jiǎn)稱(chēng),而中文簡(jiǎn)稱(chēng)為“文傳協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時(shí),它也是一個(gè)應(yīng)用程序(Application)。基于不同的操作系統(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。在FTP的使用當(dāng)中,用戶(hù)經(jīng)常遇到兩個(gè)概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠(yuǎn)程主機(jī)拷貝文件至自己的計(jì)算機(jī)上;"上傳"文件就是將文件從自己的計(jì)算機(jī)中拷貝至遠(yuǎn)程主機(jī)上。用Internet語(yǔ)言來(lái)說(shuō),用戶(hù)可通過(guò)客戶(hù)機(jī)程序向(從)遠(yuǎn)程主機(jī)上傳(下載)文件。

  首先下載了Serv-U將自己的電腦設(shè)置為了FTP文件服務(wù)器,方便操作。

1.FTP文件的下載(從FTP服務(wù)器下載到本機(jī))

?
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpApche {
private static FTPClient ftpClient = new FTPClient();
private static String encoding = System.getProperty("file.encoding");
/**
* Description: 從FTP服務(wù)器下載文件
*
* @Version1.0
* @param url
* FTP服務(wù)器hostname
* @param port
* FTP服務(wù)器端口
* @param username
* FTP登錄賬號(hào)
* @param password
* FTP登錄密碼
* @param remotePath
* FTP服務(wù)器上的相對(duì)路徑
* @param fileName
* 要下載的文件名
* @param localPath
* 下載后保存到本地的路徑
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean result = false;
try {
int reply;
ftpClient.setControlEncoding(encoding);
/*
* 為了上傳和下載中文文件,有些地方建議使用以下兩句代替
* new String(remotePath.getBytes(encoding),"iso-8859-1")轉(zhuǎn)碼。
* 經(jīng)過(guò)測(cè)試,通不過(guò)。
*/
// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
// conf.setServerLanguageCode("zh");
ftpClient.connect(url, port);
// 如果采用默認(rèn)端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
ftpClient.login(username, password);// 登錄
// 設(shè)置文件傳輸類(lèi)型為二進(jìn)制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 獲取ftp登錄應(yīng)答代碼
reply = ftpClient.getReplyCode();
// 驗(yàn)證是否登陸成功
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
return result;
}
// 轉(zhuǎn)移到FTP服務(wù)器目錄至指定的目錄下
ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
// 獲取文件列表
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), is);
is.close();
}
}
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* 將FTP服務(wù)器上文件下載到本地
*
*/
public void testDownFile() {
try {
boolean flag = downFile("10.0.0.102", 21, "admin",
"123456", "/", "ip.txt", "E:/");
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FtpApche fa = new FtpApche();
fa.testDownFile();
}
}

2.FTP文件的上傳(從本機(jī)上傳到FTP服務(wù)器)

?
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
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPTest_05 {
private FTPClient ftp;
/**
*
* @param path 上傳到ftp服務(wù)器哪個(gè)路徑下
* @param addr 地址
* @param port 端口號(hào)
* @param username 用戶(hù)名
* @param password 密碼
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上傳的文件或文件夾
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0;i < files.length;i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
public static void main(String[] args) throws Exception{
FTPTest_05 t = new FTPTest_05();
boolean connFlag = t.connect("/", "10.0.0.105", 21, "ls", "123456");
System.out.println("connFlag : " + connFlag);
File file = new File("D:\\test02");//本機(jī)被傳文件的地址
System.out.println("file : " + file);
t.upload(file);
System.out.println("upload : " + "ok");
}
}

以上所述是小編給大家介紹的Java實(shí)現(xiàn)FTP文件的上傳和下載功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/winorgohome/archive/2016/11/22/6088013.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲午夜精品视频 | 国产a一级片 | 免费国产网站 | 国产三级三级三级三级 | 91精品久久香蕉国产线看观看 | 一区二区网 | 成人三级视频在线观看 | 久久男人天堂 | 中文字幕天堂在线 | 草草在线观看 | 99久久自偷自偷国产精品不卡 | 久久成人国产精品 | 久久久久久久.comav | 依人九九宗合九九九 | 黄色18网站 | 久久精品女人天堂av | 日韩精品 | 亚洲综合一区在线观看 | 久久影院国产精品 | 国产精品久久99精品毛片三a | 黄色一级片在线免费观看 | 国产精品久久久久久久久岛 | 男人午夜小视频 | 91精品国啪老师啪 | 日韩精品一区二 | sese在线视频 | 欧美黑大粗硬毛片视频 | 色3344| 久久免费毛片 | 欧美成人精品一区二区 | 一日本道久久久精品国产 | 色播视频在线播放 | 懂色av懂色aⅴ精彩av | 免费观看高清视频网站 | 亚洲精华液久久含羞草 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 91专区在线观看 | 久久久久久久免费视频 | sm高h视频 | 国产91在线播放九色 | 日本久久精品视频 |