乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java讀取FTP上的txt文件

       青_春 2016-06-22

      package com.etwin.gateway.shopping.utils;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.net.SocketException;
      import java.text.ParseException;
      import java.util.ArrayList;
      import java.util.List;

      import org.apache.commons.net.ftp.FTPClient;
      import org.apache.commons.net.ftp.FTPFile;

      /**
       * @describe 讀取FTP上的文件
       * @auto li.wang
       * @date 2013-11-18 下午4:07:34
       */
      public class FtpUtils {

       private FTPClient ftpClient;
       private String fileName, strencoding;
       private String ip = "113.108.96.3";        // 服務(wù)器IP地址
       private String userName = "test";        // 用戶名
       private String userPwd = "test";        // 密碼
       private int port = 21;      // 端口號
       private String path = "/SZX348/OPFQ/";        // 讀取文件的存放目錄

       /**
        * init ftp servere
        */
       public FtpUtils() {
        this.reSet();
       }

       public void reSet() {
        // 以當前系統(tǒng)時間拼接文件名
        fileName = "20131112114850793835861000010161141169.txt";
        strencoding = "UTF-8";
        this.connectServer(ip, port, userName, userPwd, path);
       }

       /**
        * @param ip
        * @param port
        * @param userName
        * @param userPwd
        * @param path
        * @throws SocketException
        * @throws IOException function:連接到服務(wù)器
        */
       public void connectServer(String ip, int port, String userName, String userPwd, String path) {
        ftpClient = new FTPClient();
        try {
         // 連接
         ftpClient.connect(ip, port);
         // 登錄
         ftpClient.login(userName, userPwd);
         if (path != null && path.length() > 0) {
          // 跳轉(zhuǎn)到指定目錄
          ftpClient.changeWorkingDirectory(path);
         }
        } catch (SocketException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }

       /**
        * @throws IOException function:關(guān)閉連接
        */
       public void closeServer() {
        if (ftpClient.isConnected()) {
         try {
          ftpClient.logout();
          ftpClient.disconnect();
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       }

       /**
        * @param path
        * @return function:讀取指定目錄下的文件名
        * @throws IOException
        */
       public List<String> getFileList(String path) {
        List<String> fileLists = new ArrayList<String>();
        // 獲得指定目錄下所有文件名
        FTPFile[] ftpFiles = null;
        try {
         ftpFiles = ftpClient.listFiles(path);
        } catch (IOException e) {
         e.printStackTrace();
        }
        for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
         FTPFile file = ftpFiles[i];
         if (file.isFile()) {
          fileLists.add(file.getName());
         }
        }
        return fileLists;
       }

       /**
        * @param fileName
        * @return function:從服務(wù)器上讀取指定的文件
        * @throws ParseException
        * @throws IOException
        */
       public String readFile() throws ParseException {
        InputStream ins = null;
        StringBuilder builder = null;
        try {
         // 從服務(wù)器上讀取指定的文件
         ins = ftpClient.retrieveFileStream(fileName);
         BufferedReader reader = new BufferedReader(new InputStreamReader(ins, strencoding));
         String line;
         builder = new StringBuilder(150);
         while ((line = reader.readLine()) != null) {
          System.out.println(line);
          builder.append(line);
         }
         reader.close();
         if (ins != null) {
          ins.close();
         }
         // 主動調(diào)用一次getReply()把接下來的226消費掉. 這樣做是可以解決這個返回null問題
         ftpClient.getReply();
        } catch (IOException e) {
         e.printStackTrace();
        }
        return builder.toString();
       }

       /**
        * @param fileName function:刪除文件
        */
       public void deleteFile(String fileName) {
        try {
         ftpClient.deleteFile(fileName);
        } catch (IOException e) {
         e.printStackTrace();
        }
       }

       /**
        * @param args
        * @throws ParseException
        */
       public static void main(String[] args) throws ParseException {
        FtpUtils ftp = new FtpUtils();
        String str = ftp.readFile();
        System.out.println(str);
       }
      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多