2014/08/28

[Java] 向FTP上傳下載文件圖片




1. 下載apache-commons-io.jar

2. 將jar檔加入至project

3. FTPUploader.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
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
 
public class FTPUploader{
    /**
     * @param port 窗口(可默認)
     * @param url FTP hostname
     * @param username FTP登入帳號
     * @param password FTP登入密碼
     */
    int port = 21;
    String url = FTP的hostname;
    String username = FTP登入帳號;
    String password = FTP登入密碼;
    FTPClient ftp = new FTPClient();
    public FTPUpload(){
        try{
            ftp.connect(url, port);//連接FTP服務器
            //如果採用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
        }catch (Exception e){
            System.out.println(e);
        }
    }
    /**
     * @param path FTP上的保存目錄
     * @param filename 文件名稱
     * @param input 輸入流
     */
    public boolean uploadFile(String path, String filename, InputStream input) {
         
        boolean success = false;
        try {
            int reply;
            ftp.login(username, password);//登錄
            reply = ftp.getReplyCode();
            ftp.makeDirectory(path);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            //ftp.setFileType(FTP.BINARY_FILE_TYPE);如果是圖片
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);        
             
            input.close();
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;//成功返回true
    }
     
    /**
     * @param remoteFileName 文件在ftp上的路徑
     * @param localSavePath 下載下來的文件存放路徑
     */
    public void downloadFile(String remoteFileName,String localSavePath) {
        FileOutputStream fos = null;
 
        try {
                ftp.connect(url, port);
                ftp.login(username, password);
                File tmp = new File(localSavePath);
                if (!tmp.exists())
                    tmp.createNewFile();
            fos = new FileOutputStream(localSavePath);
 
            ftp.setBufferSize(1024);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.retrieveFile(remoteFileName, fos);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客戶端出錯!", e);
        } finally {
            IOUtils.closeQuietly(fos);
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace(); //關閉連結異常
            }
        }
    }
}


4. 如何使用

(1)upload

?
1
2
3
4
File imgFile = new File(上傳檔案的路徑);
FileInputStream fis = new FileInputStream(imgFile);
FTPUploader uploader = new FTPUploader();
boolean flag = uploader.uploadFile(上傳至ftp的哪個目錄, imgFile.getName(), fis);

(2)download

?
1
2
FTPUploader downloader = new FTPUploader();
downloader.downloadFile(欲檔案在FTP上的絕對路徑(含檔名), 要下載到哪(含檔名));