在此之前,在项目中加上FTP的架包
第一步:配置FTP服务器的相关配置
FtpConfig.java 实体类(配置类)
1 package com.sxd.ftp;View Code
2
3 public class FtpConfig {
4 //主机ip
5 private String FtpHost = "192.168.18.252";
6 //端口号
7 private int FtpPort = 21;
8 //ftp用户名
9 private String FtpUser = "ftp";
10 //ftp密码
11 private String FtpPassword = "agenbiology";
12 //ftp中的目录 这里先指定的根目录
13 private String FtpPath = "/";
14
15
16
17 public String getFtpHost() {
18 return FtpHost;
19 }
20 public void setFtpHost(String ftpHost) {
21 FtpHost = ftpHost;
22 }
23 public int getFtpPort() {
24 return FtpPort;
25 }
26 public void setFtpPort(int ftpPort) {
27 FtpPort = ftpPort;
28 }
29 public String getFtpUser() {
30 return FtpUser;
31 }
32 public void setFtpUser(String ftpUser) {
33 FtpUser = ftpUser;
34 }
35 public String getFtpPassword() {
36 return FtpPassword;
37 }
38 public void setFtpPassword(String ftpPassword) {
39 FtpPassword = ftpPassword;
40 }
41 public String getFtpPath() {
42 return FtpPath;
43 }
44 public void setFtpPath(String ftpPath) {
45 FtpPath = ftpPath;
46 }
47
48
49
50 }
第二步:
FtpUtils.java 实现简单的上传下载
1 package com.sxd.ftp;View Code
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9
10 import org.apache.commons.net.ftp.FTPClient;
11 import org.junit.Test;
12
13
14
15 public class FtpUtils {
16
17 /**
18 * 获取FTP连接
19 * @return
20 */
21 public FTPClient getFTPClient() {
22 FtpConfig config = new FtpConfig();
23 FTPClient ftpClient = new FTPClient();
24 boolean result = true;
25 try {
26 //连接FTP服务器
27 ftpClient.connect(config.getFtpHost(), config.getFtpPort());
28 //如果连接
29 if (ftpClient.isConnected()) {
30 //提供用户名/密码登录FTP服务器
31 boolean flag = ftpClient.login(config.getFtpUser(), config.getFtpPassword());
32 //如果登录成功
33 if (flag) {
34 //设置编码类型为UTF-8
35 ftpClient.setControlEncoding("UTF-8");
36 //设置文件类型为二进制文件类型
37 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
38 } else {
39 result = false;
40 }
41 } else {
42 result = false;
43 }
44 //成功连接并 登陆成功 返回连接
45 if (result) {
46 return ftpClient;
47 } else {
48 return null;
49 }
50 } catch (Exception e) {
51 e.printStackTrace();
52 return null;
53 }
54 }
55
56
57
58 /**
59 *
60 * @param localPath本地路径
61 * @param fileName文件名
62 * @param newPath上传至FTP的新位置
63 * @return
64 */
65 public boolean testUpload(String localPath,String fileName,String newPath) {
66 boolean result = true;
67 FileInputStream in = null;
68 FTPClient ftpClient = getFTPClient();
69 if (null == ftpClient) {
70 System.out.println("FTP服务器未连接成功!!!");
71 return false;
72 }
73 try {
74 //当前上传的文件
75 File file = new File(localPath+fileName);
76 in = new FileInputStream(file);
77 //上传至FTP服务器的新位置
78 ftpClient.changeWorkingDirectory(newPath);
79 //上传
80 ftpClient.storeFile(fileName, in);
81 System.out.println(ftpClient.printWorkingDirectory());
82 return result;
83 } catch (IOException e) {
84 e.printStackTrace();
85 return false;
86 } finally {
87 close(in, null, ftpClient);
88 }
89 }
90
91 /**
92 * FTP下载
93 * @param fileName 文件名
94 * @param localPath
95 * @param remotePath
96 * @return
97 */
98 public boolean testDownload(String localPath,String fileName, String remotePath) {
99 boolean result = true;
100 FileOutputStream out = null;
101 FTPClient ftpClient = getFTPClient();
102 if (null == ftpClient) {
103 System.out.println("FTP服务器未连接成功!!!");
104 return false;
105 }
106 try {
107 //要写到本地的位置
108 File file = new File(localPath + fileName);
109 out = new FileOutputStream(file);
110 //文件存储在FTP的位置
111 ftpClient.changeWorkingDirectory(remotePath);
112 //下载文件
113 ftpClient.retrieveFile(fileName, out);
114 System.out.println(ftpClient.printWorkingDirectory());
115 return result;
116 } catch (IOException e) {
117 e.printStackTrace();
118 return false;
119 } finally {
120 close(null, out, ftpClient);
121 }
122 }
123
124
125 /**
126 * 关闭 输入流或输出流
127 * @param in
128 * @param out
129 * @param ftpClient
130 */
131 public static void close(InputStream in, OutputStream out,FTPClient ftpClient) {
132 if (null != in) {
133 try {
134 in.close();
135 } catch (IOException e) {
136 e.printStackTrace();
137 System.out.println("输入流关闭失败");
138 }
139 }
140 if (null != out) {
141 try {
142 out.close();
143 } catch (IOException e) {
144 e.printStackTrace();
145 System.out.println("输出流关闭失败");
146 }
147 }
148 if (null != ftpClient) {
149 try {
150 ftpClient.logout();
151 ftpClient.disconnect();
152 } catch (IOException e) {
153 e.printStackTrace();
154 System.out.println("Ftp服务关闭失败!");
155 }
156 }
157 }
158
159
160 @Test
161 public void test(){
162 FtpConfig config = new FtpConfig();
163 //上传文件
164 // testUpload("D:/","new4.txt" , config.getFtpPath());
165 //下载
166 testDownload("D:/develop/", "new4.txt", config.getFtpPath());
167
168
169
170 }
171 }