程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java使用ftp上傳文件示例分享

java使用ftp上傳文件示例分享

編輯:JAVA編程入門知識

代碼如下:

import java.io.ByteArrayInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.SocketException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.apache.commons.io.IOUtils; 
import org.apache.commons.net.ftp.FTPClient; 

/**
 * class name:FTPFileTransmit <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2011-8-9
 */ 
public class FTPFileTransmit { 

    private String ftpPath; 
    private String ftpName; 
    private String ftpPassword; 
    private String ftpServerIP; 

    public FTPFileTransmit() { 
        this.ftpPath = "xxx/xxx/"; 
        this.ftpName = "name"; 
        this.ftpPassword = "pass"; 
        this.ftpServerIP = "192.168.0.xx"; 
    } 

     
    /**
     * Method name: saveInFTP <BR>
     * Description: 把文件存儲在FTP上 <BR>
     * Remark: <BR>
     * @param FolderName            示例"xxx/xxx/"
     * @param FileName              示例"thefilename"
     * @param data                  byte[]數組
     * @return  boolean<BR>
     */ 
    public boolean saveInFTP (String FolderName, String FileName, byte[] data) { 
        boolean flag = false; 

        // 創建FTP客戶端  
        FTPClient ftpClient = new FTPClient(); 
        // 輸入流用於讀取文件  
//      FileInputStream fis = null;  
        ByteArrayInputStream bis = null; 

        try { 
            // 如果FolderName 和 FileName都不符合基本要求, 那麼就沒有必要進行ftp操作  
            if (FolderName != null 
                    && FolderName.compareTo("") != 0 
                    && FileName != null 
                    && FileName.compareTo("") != 0) { 

                // 建立FTP連接  
                ftpClient.connect(this.ftpServerIP); 

                // 如果登錄成功後, 才進行創建輸入流  
                if (ftpClient.login(this.ftpName, this.ftpPassword)) { 
//                  File srcClientFile = new File("C:/ParseXML.xml");  

                    // 實例化輸入流  
//                  fis = new FileInputStream(srcClientFile);  

                    if (ftpClient.changeWorkingDirectory(FolderName)) { 
                        // 將byte[]寫入到輸入流中, 實例化  
                        bis = new ByteArrayInputStream(data); 

                        // 設置緩沖  
                        ftpClient.setBufferSize(1024); 

                        // 設置文件類型(二進制類型)  
                        if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) { 
                            flag = ftpClient.storeFile(FileName, bis); 
                        } 
                    } 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 關閉輸入流  
                IOUtils.closeQuietly(bis); 
                // 關閉連接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

    /**
     * Method name: getFromFTP <BR>
     * Description: 從FTP上讀取文件 <BR>
     * Remark: <BR>
     * @return  boolean<BR>
     */ 
    public boolean getFromFTP () { 
        boolean flag = false; 

        // 創建FTP客戶端  
        FTPClient ftpClient = new FTPClient(); 
        // 輸出流用於輸出文件  
        FileOutputStream fos = null; 

        try { 
            // 建立FTP連接  
            ftpClient.connect(this.ftpServerIP); 
            // 如果登錄成功後, 才進行創建輸出流  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 
                // FTP文件  
                String distinationFile = "/name/xxx/xxx/xxx文件"; 
                // 實例化輸出流  
                fos = new FileOutputStream("C:/ParseXML_InFTP.xml"); 

                // 設置緩沖  
                ftpClient.setBufferSize(1024); 

                // 設置文件類型(二進制類型)  
                if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) { 
                    ftpClient.retrieveFile(distinationFile, fos); 
                    flag = true; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 關閉輸出流  
                IOUtils.closeQuietly(fos); 
                // 關閉連接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

    public boolean createDirectory() { 
        boolean flag = false; 

        // 創建FTP客戶端  
        FTPClient ftpClient = new FTPClient(); 

        try { 
            // 建立FTP連接  
            ftpClient.connect(this.ftpServerIP); 
            // 如果登錄成功後, 才進行操作  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 

                // 切換文件路徑, 到FTP上的"NNDD3"文件夾下  
                if (this.ftpPath != null && this.ftpPath.compareTo("") != 0 
                        && ftpClient.changeWorkingDirectory(this.ftpPath)) { 
                    SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd"); 
                    String time = f.format(new Date()); 

                    String FolderName = time + "_ReTransmit"; 
                    ftpClient.makeDirectory(FolderName); 
                    flag = true; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (IOException e) { 
            e.printStackTrace(); 
            flag = false; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            flag = false; 
        } finally { 
            try { 
                // 關閉連接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return flag; 
    } 

    public String[] getAllFolderNames () { 
        // 創建FTP客戶端  
        FTPClient ftpClient = new FTPClient(); 

        try { 
            // 建立FTP連接  
            ftpClient.connect(this.ftpServerIP); 

            // 如果登錄成功後, 才進行操作  
            if (ftpClient.login(this.ftpName, this.ftpPassword)) { 

                // 切換文件路徑, 到FTP上的"NNDD3"文件夾下  
                if (this.ftpPath != null && this.ftpPath.compareTo("") != 0 
                        && ftpClient.changeWorkingDirectory(this.ftpPath)) { 
                    // 將當前時間減去2天, 刪除的是前兩天的數據包  
                    String time = minusTime(); 

                    String[] allNames = ftpClient.listNames(); 

                    String[] temp = new String[allNames.length]; 
                    // 初始化數組  
                    for (int j = 0; j < allNames.length; j ++) { 
                        temp[j] = ""; 
                    } 

                    // 找出要刪除文件夾的名稱  
                    for (int i = 0; i < allNames.length; i ++) { 
                        if (allNames[i].substring(0, 8).compareTo(time) <= 0) { 
                            temp[i] = allNames[i]; 
                        } 
                    } 

                    return temp; 
                } 
            } 
        } catch (SocketException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                // 關閉連接  
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 

        return null; 
    } 

    /**
     * 
     * Method name: minusTime <BR>
     * Description: 獲取錢兩天的時間,如2011-8-1的前兩天就是2011-7-30 <BR>
     * Remark: <BR>
     * @return  String<BR>
     */ 
    private String minusTime() { 
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");    
        Date d = new Date(); 
        String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));    
        return timeMinus2; 
    } 

    public static void main(String[] args) { 
        FTPFileTransmit ftpFileTransmit = new FTPFileTransmit(); 
        ftpFileTransmit.deleteFoldersInFTP(); 

//      boolean flag = ftpFileTransmit.createDirectory();  
//      if (flag) {  
//          System.out.println("****** FTP文件夾創建成功 ******");  
//      }  

//      String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";  
//      byte[] data = new byte[1024];  
//      for (int i = 0; i < data.length; i ++) {  
//          data[i] = 'a';  
//      }  
//      boolean flag = ftpFileTransmit.saveInFTP(FolderName, "2011080912345678" ,data);  
//      if (flag) {  
//          System.out.println("****** FTP文件夾創建成功 ******");  
//      }  
    } 
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved