小雨点点789 发表于 2015-5-28 14:02:05

FTP 上传下载

Code
1 package com;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import org.apache.commons.net.ftp.FTPClient;
10 import org.apache.commons.net.ftp.FTPFile;
11 import org.apache.commons.net.ftp.FTPReply;
12
13 public class Upload {
14
15   /**
16      * Description: 向FTP服务器上传文件
17      *
18      * @param url FTP服务器hostname
19      * @param port FTP服务器端口
20      * @param username FTP登录账号
21      * @param password FTP登录密码
22      * @param path FTP服务器保存目录
23      * @param filename 上传到FTP服务器上的文件名
24      * @param input 输入流
25      * @return 成功返回true,否则返回false
26      */
27   public boolean uploadFile(String url, int port, String username,
28             String password, String path, String filename, InputStream input) {
29         // 初始表示上传失败
30         boolean success = false;
31         // 创建FTPClient对象
32         FTPClient ftp = new FTPClient();
33         try {
34             int reply;
35             // 连接FTP服务器
36             // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
37             ftp.connect(url, port);
38             // 登录ftp
39             ftp.login(username, password);
40             // 看返回的值是不是230,如果是,表示登陆成功
41             reply = ftp.getReplyCode();
42             // 以2开头的返回值就会为真
43             if (!FTPReply.isPositiveCompletion(reply)) {
44               ftp.disconnect();
45               return success;
46             }
47             // 转到指定上传目录
48             ftp.changeWorkingDirectory(path);
49             // 将上传文件存储到指定目录
50             ftp.storeFile(filename, input);
51             // 关闭输入流
52             input.close();
53             // 退出ftp
54             ftp.logout();
55             // 表示上传成功
56             success = true;
57         } catch (IOException e) {
58             e.printStackTrace();
59         } finally {
60             if (ftp.isConnected()) {
61               try {
62                     ftp.disconnect();
63               } catch (IOException ioe) {
64               }
65             }
66         }
67         return success;
68   }
69
70   /**
71      * Description: 从FTP服务器下载文件
72      *
73      * @param url FTP服务器hostname
74      * @param port FTP服务器端口
75      * @param username FTP登录账号
76      * @param password FTP登录密码
77      * @param remotePath FTP服务器上的相对路径
78      * @param fileName 要下载的文件名
79      * @param localPath 下载后保存到本地的路径
80      * @return
81      */
82   public boolean downFile(String url, int port, String username,
83             String password, String remotePath, String fileName,
84             String localPath) {
85         // 初始表示下载失败
86         boolean success = false;
87         // 创建FTPClient对象
88         FTPClient ftp = new FTPClient();
89         try {
90             int reply;
91             // 连接FTP服务器
92             // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
93             ftp.connect(url, port);
94             // 登录ftp
95             ftp.login(username, password);
96             reply = ftp.getReplyCode();
97             if (!FTPReply.isPositiveCompletion(reply)) {
98               ftp.disconnect();
99               return success;
100             } // 转到指定下载目录
101             ftp.changeWorkingDirectory(remotePath);
102             // 列出该目录下所有文件
103             FTPFile[] fs = ftp.listFiles();
104             // 遍历所有文件,找到指定的文件
105             for (FTPFile ff : fs) {
106               if (ff.getName().equals(fileName)) {
107                     // 根据绝对路径初始化文件
108                     File localFile = new File(localPath + "/" + ff.getName());
109                     // 输出流
110                     OutputStream is = new FileOutputStream(localFile);
111                     // 下载文件
112                     ftp.retrieveFile(ff.getName(), is);
113                     is.close();
114               }
115             }
116             // 退出ftp
117             ftp.logout();
118             // 下载成功
119             success = true;
120         } catch (IOException e) {
121             e.printStackTrace();
122         } finally {
123             if (ftp.isConnected()) {
124               try {
125                     ftp.disconnect();
126               } catch (IOException ioe) {
127               }
128             }
129         }
130         return success;
131   }
132 }
133
页: [1]
查看完整版本: FTP 上传下载