隨筆-62  評論-29  文章-0  trackbacks-0
          1. import java.io.File;   
          2. import java.io.FileInputStream;   
          3. import java.io.FileOutputStream;   
          4. import java.io.IOException;   
          5. import java.io.InputStream;   
          6. import java.io.OutputStream;   
          7. import java.net.SocketException;   
          8. import java.util.ArrayList;   
          9. import java.util.Date;   
          10. import java.util.List;   
          11. import org.apache.commons.net.ftp.FTP;   
          12. import org.apache.commons.net.ftp.FTPClient;   
          13. import org.apache.commons.net.ftp.FTPFile;   
          14.   
          15. public class FtpUtil {   
          16.     private FTPClient ftpClient;   
          17.     public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;   
          18.     public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;   
          19.        
          20.     // path should not the path from root index   
          21.     // or some FTP server would go to root as '/'.   
          22.     public void connectServer(FtpConfig ftpConfig) throws SocketException,   
          23.             IOException {   
          24.         String server = ftpConfig.getServer();   
          25.         int port = ftpConfig.getPort();   
          26.         String user = ftpConfig.getUsername();   
          27.         String password = ftpConfig.getPassword();   
          28.         String location = ftpConfig.getLocation();   
          29.         connectServer(server, port, user, password, location);   
          30.     }   
          31.        
          32.        
          33.     public void connectServer(String server, int port, String user,   
          34.             String password, String path) throws SocketException, IOException {   
          35.         ftpClient = new FTPClient();   
          36.         ftpClient.connect(server, port);   
          37.         System.out.println("Connected to " + server + ".");   
          38.         System.out.println(ftpClient.getReplyCode());   
          39.         ftpClient.login(user, password);   
          40.         // Path is the sub-path of the FTP path   
          41.         if (path.length() != 0) {   
          42.             ftpClient.changeWorkingDirectory(path);   
          43.         }   
          44.     }   
          45.     //FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE   
          46.     // Set transform type   
          47.     public void setFileType(int fileType) throws IOException {   
          48.         ftpClient.setFileType(fileType);   
          49.     }   
          50.   
          51.     public void closeServer() throws IOException {   
          52.         if (ftpClient.isConnected()) {   
          53.             ftpClient.disconnect();   
          54.         }   
          55.     }   
          56.     //=======================================================================   
          57.     //==         About directory       =====   
          58.     // The following method using relative path better.   
          59.     //=======================================================================   
          60.        
          61.     public boolean changeDirectory(String path) throws IOException {   
          62.         return ftpClient.changeWorkingDirectory(path);   
          63.     }   
          64.     public boolean createDirectory(String pathName) throws IOException {   
          65.         return ftpClient.makeDirectory(pathName);   
          66.     }   
          67.     public boolean removeDirectory(String path) throws IOException {   
          68.         return ftpClient.removeDirectory(path);   
          69.     }   
          70.        
          71.     // delete all subDirectory and files.   
          72.     public boolean removeDirectory(String path, boolean isAll)   
          73.             throws IOException {   
          74.            
          75.         if (!isAll) {   
          76.             return removeDirectory(path);   
          77.         }   
          78.   
          79.         FTPFile[] ftpFileArr = ftpClient.listFiles(path);   
          80.         if (ftpFileArr == null || ftpFileArr.length == 0) {   
          81.             return removeDirectory(path);   
          82.         }   
          83.         //    
          84.         for (FTPFile ftpFile : ftpFileArr) {   
          85.             String name = ftpFile.getName();   
          86.             if (ftpFile.isDirectory()) {   
          87. System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");                
          88.                 removeDirectory(path + "/" + name, true);   
          89.             } else if (ftpFile.isFile()) {   
          90. System.out.println("* [sF]Delete file ["+path + "/" + name+"]");                           
          91.                 deleteFile(path + "/" + name);   
          92.             } else if (ftpFile.isSymbolicLink()) {   
          93.   
          94.             } else if (ftpFile.isUnknown()) {   
          95.   
          96.             }   
          97.         }   
          98.         return ftpClient.removeDirectory(path);   
          99.     }   
          100.        
          101.     // Check the path is exist; exist return true, else false.   
          102.     public boolean existDirectory(String path) throws IOException {   
          103.         boolean flag = false;   
          104.         FTPFile[] ftpFileArr = ftpClient.listFiles(path);   
          105.         for (FTPFile ftpFile : ftpFileArr) {   
          106.             if (ftpFile.isDirectory()   
          107.                     && ftpFile.getName().equalsIgnoreCase(path)) {   
          108.                 flag = true;   
          109.                 break;   
          110.             }   
          111.         }   
          112.         return flag;   
          113.     }   
          114.   
          115.     //=======================================================================   
          116.     //==         About file        =====   
          117.     // Download and Upload file using   
          118.     // ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better!   
          119.     //=======================================================================   
          120.   
          121.     // #1. list & delete operation   
          122.     // Not contains directory   
          123.     public List<String> getFileList(String path) throws IOException {   
          124.         // listFiles return contains directory and file, it's FTPFile instance   
          125.         // listNames() contains directory, so using following to filer directory.   
          126.         //String[] fileNameArr = ftpClient.listNames(path);   
          127.         FTPFile[] ftpFiles= ftpClient.listFiles(path);   
          128.            
          129.         List<String> retList = new ArrayList<String>();   
          130.         if (ftpFiles == null || ftpFiles.length == 0) {   
          131.             return retList;   
          132.         }   
          133.         for (FTPFile ftpFile : ftpFiles) {   
          134.             if (ftpFile.isFile()) {   
          135.                 retList.add(ftpFile.getName());   
          136.             }   
          137.         }   
          138.         return retList;   
          139.     }   
          140.   
          141.     public boolean deleteFile(String pathName) throws IOException {   
          142.         return ftpClient.deleteFile(pathName);   
          143.     }   
          144.   
          145.     // #2. upload to ftp server   
          146.     // InputStream <------> byte[]  simple and See API   
          147.   
          148.     public boolean uploadFile(String fileName, String newName)   
          149.             throws IOException {   
          150.         boolean flag = false;   
          151.         InputStream iStream = null;   
          152.         try {   
          153.             iStream = new FileInputStream(fileName);   
          154.             flag = ftpClient.storeFile(newName, iStream);   
          155.         } catch (IOException e) {   
          156.             flag = false;   
          157.             return flag;   
          158.         } finally {   
          159.             if (iStream != null) {   
          160.                 iStream.close();   
          161.             }   
          162.         }   
          163.         return flag;   
          164.     }   
          165.   
          166.     public boolean uploadFile(String fileName) throws IOException {   
          167.         return uploadFile(fileName, fileName);   
          168.     }   
          169.   
          170.     public boolean uploadFile(InputStream iStream, String newName)   
          171.             throws IOException {   
          172.         boolean flag = false;   
          173.         try {   
          174.             // can execute [OutputStream storeFileStream(String remote)]   
          175.             // Above method return's value is the local file stream.   
          176.             flag = ftpClient.storeFile(newName, iStream);   
          177.         } catch (IOException e) {   
          178.             flag = false;   
          179.             return flag;   
          180.         } finally {   
          181.             if (iStream != null) {   
          182.                 iStream.close();   
          183.             }   
          184.         }   
          185.         return flag;   
          186.     }   
          187.   
          188.     // #3. Down load    
          189.   
          190.     public boolean download(String remoteFileName, String localFileName)   
          191.             throws IOException {   
          192.         boolean flag = false;   
          193.         File outfile = new File(localFileName);   
          194.         OutputStream oStream = null;   
          195.         try {   
          196.             oStream = new FileOutputStream(outfile);   
          197.             flag = ftpClient.retrieveFile(remoteFileName, oStream);   
          198.         } catch (IOException e) {   
          199.             flag = false;   
          200.             return flag;   
          201.         } finally {   
          202.             oStream.close();   
          203.         }   
          204.         return flag;   
          205.     }   
          206.        
          207.     public InputStream downFile(String sourceFileName) throws IOException {   
          208.         return ftpClient.retrieveFileStream(sourceFileName);   
          209.     }  


          posted on 2008-08-28 22:43 閱讀(1130) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 唐山市| 高要市| 建湖县| 武安市| 霍山县| 金昌市| 广灵县| 保定市| 天镇县| 五大连池市| 武邑县| 诏安县| 嵊州市| 乌兰浩特市| 萍乡市| 司法| 泾川县| 伊川县| 周至县| 宜城市| 图片| 汕尾市| 偏关县| 布拖县| 巴楚县| 牡丹江市| 扶风县| 繁昌县| 荆州市| 吉木乃县| 汉阴县| 佛学| 平潭县| 三亚市| 凌源市| 新绛县| 嘉善县| 东乌珠穆沁旗| 南涧| 宝坻区| 承德县|