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

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

編輯:關於JAVA

java應用ftp上傳文件示例分享。本站提示廣大學習愛好者:(java應用ftp上傳文件示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用ftp上傳文件示例分享正文



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