ftp上传文件、删除文件、下载文件的操作

FavFTPUtil.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.favccxx.favsoft.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
public class FavFTPUtil {
     
    /**
     * 上传文件(可供Action/Controller层使用)
     * @param hostname FTP服务器地址
     * @param port   FTP服务器端口号
     * @param username   FTP登录帐号
     * @param password   FTP登录密码
     * @param pathname   FTP服务器保存目录
     * @param fileName   上传到FTP服务器后的文件名称
     * @param inputStream 输入文件流
     * @return
     */
    public static boolean uploadFile(String hostname, int port, String username, String password, String pathname, String fileName, InputStream inputStream){
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        try {
            //连接FTP服务器
            ftpClient.connect(hostname, port);
            //登录FTP服务器
            ftpClient.login(username, password);
            //是否成功登录FTP服务器
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
             
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        catch (Exception e) {
            e.printStackTrace();
        finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.disconnect();
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
     
     
    /**
     * 上传文件(可对文件进行重命名)
     * @param hostname FTP服务器地址
     * @param port   FTP服务器端口号
     * @param username   FTP登录帐号
     * @param password   FTP登录密码
     * @param pathname   FTP服务器保存目录
     * @param filename   上传到FTP服务器后的文件名称
     * @param originfilename 待上传文件的名称(绝对地址)
     * @return
     */
    public static boolean uploadFileFromProduction(String hostname, int port, String username, String password, String pathname, String filename, String originfilename){
        boolean flag = false;
        try {
            InputStream inputStream = new FileInputStream(new File(originfilename));
            flag = uploadFile(hostname, port, username, password, pathname, filename, inputStream);
        catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
     
    /**
     * 上传文件(不可以进行文件的重命名操作)
     * @param hostname FTP服务器地址
     * @param port   FTP服务器端口号
     * @param username   FTP登录帐号
     * @param password   FTP登录密码
     * @param pathname   FTP服务器保存目录
     * @param originfilename 待上传文件的名称(绝对地址)
     * @return
     */
    public static boolean uploadFileFromProduction(String hostname, int port, String username, String password, String pathname, String originfilename){
        boolean flag = false;
        try {
            String fileName = new File(originfilename).getName();
            InputStream inputStream = new FileInputStream(new File(originfilename));
            flag = uploadFile(hostname, port, username, password, pathname, fileName, inputStream);
        catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
     
     
    /**
     * 删除文件
     * @param hostname FTP服务器地址
     * @param port   FTP服务器端口号
     * @param username   FTP登录帐号
     * @param password   FTP登录密码
     * @param pathname   FTP服务器保存目录
     * @param filename   要删除的文件名称
     * @return
     */
    public static boolean deleteFile(String hostname, int port, String username, String password, String pathname, String filename){
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        try {
            //连接FTP服务器
            ftpClient.connect(hostname, port);
            //登录FTP服务器
            ftpClient.login(username, password);
            //验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.dele(filename);
            ftpClient.logout();
            flag = true;
        catch (Exception e) {
            e.printStackTrace();
        finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.logout();
                catch (IOException e) {
                 
                }
            }
        }
        return flag;
    }
     
    /**
     * 下载文件
     * @param hostname FTP服务器地址
     * @param port   FTP服务器端口号
     * @param username   FTP登录帐号
     * @param password   FTP登录密码
     * @param pathname   FTP服务器文件目录
     * @param filename   文件名称
     * @param localpath 下载后的文件路径
     * @return
     */
    public static boolean downloadFile(String hostname, int port, String username, String password, String pathname, String filename, String localpath){
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        try {
            //连接FTP服务器
            ftpClient.connect(hostname, port);
            //登录FTP服务器
            ftpClient.login(username, password);
            //验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(replyCode)){
                return flag;
            }
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for(FTPFile file : ftpFiles){
                if(filename.equalsIgnoreCase(file.getName())){
                    File localFile = new File(localpath + "/" + file.getName());
                    OutputStream os = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(file.getName(), os);
                    os.close();
                }
            }
            ftpClient.logout();
            flag = true;
        catch (Exception e) {
            e.printStackTrace();
        finally{
            if(ftpClient.isConnected()){
                try {
                    ftpClient.logout();
                catch (IOException e) {
                 
                }
            }
        }
        return flag;
    }
 
}

  FavFTPUtilTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.favccxx.favsoft.util;
 
import junit.framework.TestCase;
 
public class FavFTPTest extends TestCase {
     
    public void testFavFTPUtil(){
        String hostname = "127.0.0.1";
        int port = 21;
        String username = "business";
        String password = "business";
        String pathname = "business/ebook"
        String filename = "big.rar"
        String originfilename = "C:\Users\Downloads\Downloads.rar";
        FavFTPUtil.uploadFileFromProduction(hostname, port, username, password, pathname, filename, originfilename);
//      String localpath = "D:/";
         
//      FavFTPUtil.downloadFile(hostname, port, username, password, pathname, filename, localpath);
    }
 
}
原文地址:https://www.cnblogs.com/likeju/p/4687216.html