1. 程式人生 > >利用FtpClient實現上傳下載及獲得檔案目錄

利用FtpClient實現上傳下載及獲得檔案目錄

sun程式碼中有個FtpClient,雖然沒有把它用做公開的工具包,但我們也還是可以拿它來利用一下.
 
 
 


Java程式碼  
1./** 
2. * FTP檔案上傳與下載 
3. * notice: 
4. * 之所以每次都要連線一次ftp是讓它的目錄重新返回到相對的根目錄, 
5. * 如果複用上次的FtpClient則可能它當前在FTP的目錄不是我們想要的 
6. * 目錄,所以在FTP上傳下載檔案時,最好每次都重新登入一下FTP 
7. * @author lgh 
8. */  
9.public class FTPClient {  
10.  
11.    private FtpClient ftpClient;  
12.    private String ip;  
13.    private int port;  
14.    private String username;  
15.    private String password;  
16.  
17.    public FTPClient() {  
18.    }  
19.  
20.    public FTPClient(String ip, int port, String username, String password) {  
21.        this.ip = ip;  
22.        this.port = port;  
23.        this.username = username;  
24.        this.password = password;  
25.    }  
26.  
27.    /** 
28.     * 需要備份的檔案 
29.     * @param list 
30.     * @return 
31.     */  
32.    private List needBackFile(List list, String relativeName) {  
33.        List fileNames = new ArrayList();  
34.        for (int i = 0; i < list.size(); i++) {  
35.            String temp = (String) list.get(i);  
36.            if (temp.indexOf(relativeName) > 0) {  
37.                fileNames.add(temp);  
38.            }  
39.        }  
40.        return fileNames;  
41.    }  
42.  
43.    public static void main(String[] args) {  
44.        FTPClient client = new FTPClient(".....", 21, "...", "....");  
45.  
46.        try {  
47.//            client.downloadFile("CRM/ccbcrm/", "D://", "CRMClientLog.log", "CRMClientLog.log");  
48.//            client.uploadFile("", "D://", "CRMClientLog.log");  
49.            List list = client.getList("csrtestftp/網路/", false);  
50.        } catch (Exception ex) {  
51.            Logger.getLogger(FTPClient.class.getName()).log(Level.SEVERE, null, ex);  
52.        }  
53.    }  
54.  
55.    /** 
56.     * 關閉FTP連線  
57.     * @throws java.lang.Exception 
58.     */  
59.    public void closeServer() throws Exception {  
60.        ftpClient.closeServer();  
61.    }  
62.  
63.    /** 
64.     * 連線ftp伺服器 
65.     * @param ip 
66.     * @param port 
67.     * @param user 
68.     * @param pwd 
69.     * @return 
70.     * @throws Exception 
71.     */  
72.    public boolean connectServer(String ip, int port, String user, String pwd)  
73.            throws Exception {  
74.        boolean isSuccess = false;  
75.        try {  
76.            ftpClient = new FtpClient();  
77.            ftpClient.openServer(ip, port);  
78.            ftpClient.login(user, pwd);  
79.            isSuccess = true;  
80.        } catch (Exception ex) {  
81.            ex.printStackTrace();  
82.        }  
83.        return isSuccess;  
84.    }  
85.  
86.    /** 
87.     * 獲得遠端下的目錄 
88.     * @param remotePath 遠端目錄 
89.     * @param fullPath 是否需要完整路徑 
90.     * @return 
91.     */  
92.    public List getList(String remotePath, boolean fullPath) {  
93.        List list = new ArrayList();  
94.        try {  
95.            if (connectServer(ip, port, username, password)) {  
96.                BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));  
97.                String temp = ftpClient.getResponseString();  
98.                System.out.println(temp);  
99.                String readLine = null;  
100.                int lastIndex;  
101.                if ((lastIndex = remotePath.lastIndexOf("/")) > 0||(lastIndex = remotePath.lastIndexOf("//")) > 0  
102.                    ||(lastIndex = remotePath.lastIndexOf("\\")) > 0||(lastIndex = remotePath.lastIndexOf(File.separator)) > 0) {  
103.                    remotePath = remotePath.substring(0, lastIndex);  
104.                }   //去掉remotePath的字尾,可能是'/',也有可能是其他符號  
105.                while ((readLine = br.readLine()) != null) {  
106.  
107.                    if (!fullPath) {  
108.                        list.add(readLine.substring(remotePath.length() + 1, readLine.length()));  
109.                        System.out.println(readLine.substring(remotePath.length() + 1, readLine.length()));  
110.                    } else {  
111.                        list.add(readLine);  
112.                        System.out.println(readLine);  
113.                    }  
114.                }  
115.                ftpClient.closeServer();  
116.            }  
117.        } catch (Exception ex) {  
118.            ex.printStackTrace();  
119.        }  
120.        return list;  
121.    }  
122.  
123.    /** 
124.     * 下載檔案 
125.     * @param remotePath 
126.     * @param localPath 
127.     * @param filename 
128.     * @throws Exception 
129.     */  
130.    public void downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {  
131.        try {  
132.            if (connectServer(ip, port, username, password)) {  
133.                if (remotePath.length() != 0) {  
134.                    ftpClient.cd(remotePath);  
135.                }  
136.                ftpClient.binary();  
137.                TelnetInputStream is = ftpClient.get(remoteFileName);  
138.                File fp = new File(localPath);  
139.                if (!fp.exists()) {  
140.                    fp.mkdirs();  
141.                }  
142.                File file_out = new File(localPath + File.separator + localFileName);  
143.                FileOutputStream os = new FileOutputStream(file_out);  
144.                byte[] bytes = new byte[1024];  
145.                int readBye;  
146.                while ((readBye = is.read(bytes)) != -1) {  
147.                    os.write(bytes, 0, readBye);  
148.                }  
149.                is.close();  
150.                os.close();  
151.                ftpClient.closeServer();  
152.            }  
153.        } catch (Exception ex) {  
154.            ex.printStackTrace();  
155.        }  
156.    }  
157.  
158.    /** 
159.     * 上傳檔案 
160.     * @param remotePath 
161.     * @param localPath 
162.     * @param filename 
163.     * @throws Exception 
164.     */  
165.    public void uploadFile(String remotePath, String localPath, String filename) throws Exception {  
166.        try {  
167.            if (connectServer(ip, port, username, password)) {  
168.                if (remotePath.length() != 0) {  
169.                    ftpClient.cd(remotePath);  
170.                }  
171.                ftpClient.binary();  
172.                TelnetOutputStream os = ftpClient.put(filename);  
173.                File file_in = new File(localPath + File.separator + filename);  
174.                FileInputStream is = new FileInputStream(file_in);  
175.                byte[] bytes = new byte[1024];  
176.                int readBye;  
177.                while ((readBye = is.read(bytes)) != -1) {  
178.                    os.write(bytes, 0, readBye);  
179.                }  
180.                is.close();  
181.                os.close();  
182.                ftpClient.closeServer();  
183.            }  
184.        } catch (Exception ex) {  
185.            ex.printStackTrace();  
186.        }  
187.    }  
188.  
189.    /** 
190.     * @return the ip 
191.     */  
192.    public String getIp() {  
193.        return ip;  
194.    }  
195.  
196.    /** 
197.     * @param ip the ip to set 
198.     */  
199.    public void setIp(String ip) {  
200.        this.ip = ip;  
201.    }  
202.  
203.    /** 
204.     * @return the port 
205.     */  
206.    public int getPort() {  
207.        return port;  
208.    }  
209.  
210.    /** 
211.     * @param port the port to set 
212.     */  
213.    public void setPort(int port) {  
214.        this.port = port;  
215.    }  
216.  
217.    /** 
218.     * @return the username 
219.     */  
220.    public String getUsername() {  
221.        return username;  
222.    }  
223.  
224.    /** 
225.     * @param username the username to set 
226.     */  
227.    public void setUsername(String username) {  
228.        this.username = username;  
229.    }  
230.  
231.    /** 
232.     * @return the password 
233.     */  
234.    public String getPassword() {  
235.        return password;  
236.    }  
237.  
238.    /** 
239.     * @param password the password to set 
240.     */  
241.    public void setPassword(String password) {  
242.        this.password = password;  
243.    }  
244.}