本文實例為大家分享了文件上傳到ftp服務工具類,供大家參考,具體內容如下
直接引用此java工具類就好
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
147
148
149
150
151
|
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; /** * ftp上傳下載工具類 * <p>Title: FtpUtil</p> * <p>Description: </p> * <p>Company: www.itcast.com</p> * @author 入云龍 * @date 2015年7月29日下午8:11:51 * @version 1.0 */ public class FtpUtil { /** * Description: 向FTP服務器上傳文件 * @param host FTP服務器hostname * @param port FTP服務器端口 * @param username FTP登錄賬號 * @param password FTP登錄密碼 * @param basePath FTP服務器基礎目錄 * @param filePath FTP服務器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑為basePath+filePath * @param filename 上傳到FTP服務器上的文件名 * @param input 輸入流 * @return 成功返回true,否則返回false */ public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false ; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 連接FTP服務器 // 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器 ftp.login(username, password); // 登錄 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //切換到上傳目錄 if (!ftp.changeWorkingDirectory(basePath+filePath)) { //如果目錄不存在創建目錄 String[] dirs = filePath.split( "/" ); String tempPath = basePath; for (String dir : dirs) { if ( null == dir || "" .equals(dir)) continue ; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } //設置上傳文件的類型為二進制類型 ftp.setFileType(FTP.BINARY_FILE_TYPE); //上傳文件 if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true ; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 從FTP服務器下載文件 * @param host FTP服務器hostname * @param port FTP服務器端口 * @param username FTP登錄賬號 * @param password FTP登錄密碼 * @param remotePath FTP服務器上的相對路徑 * @param fileName 要下載的文件名 * @param localPath 下載后保存到本地的路徑 * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false ; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器 ftp.login(username, password); // 登錄 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath); // 轉移到FTP服務器目錄 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true ; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static void main(String[] args) { try { FileInputStream in= new FileInputStream( new File( "D:\\temp\\image\\gaigeming.jpg" )); boolean flag = uploadFile( "192.168.25.133" , 21 , "ftpuser" , "ftpuser" , "/home/ftpuser/www/images" , "/2015/01/21" , "gaigeming.jpg" , in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。