天天看點

java循環讀取ftp伺服器上面的檔案

      公司新項目需要在ftp伺服器上讀取檔案儲存到資料庫,是以學習了一下怎麼讀取

     項目采用的是springboot架構,首先需要在pom檔案中加入comment.net jar包依賴

<!-- 讀取ftp 檔案-->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.3</version>
		</dependency>
           

   然後寫一個ftp登陸的方法,采用@Slf4j注解 來引入log日志

//傳入ftp位址,端口,登入名,密碼
	public FTPClient getFtpClient(String host, int port, String userName, String password) {
		FTPClient ftp = new FTPClient();
		try {
			ftp.connect(host, port);// 連接配接ftp
			ftp.login(userName, password);// 登陸ftp
			//判斷ftp是否登入成功
			if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
				log.info("登陸成功" + host + ":" + port);
				return ftp;

			} else {
				log.info("登陸失敗,使用者名或者密碼錯誤" + host + ":" + port);
				ftp.disconnect();//ftp關閉
				return null;
			}
		} catch (SocketException e) {
			e.printStackTrace();
			log.error("ftp位址可能錯誤");
		} catch (IOException e) {
			e.printStackTrace();
			log.error("ftp端口錯誤");
		}
		return null;

	}
           

然後寫讀取檔案的方法

//傳入ftp連接配接,和該連結下面的檔案名稱
	public String readFile(FTPClient ftp, String fileName) {
		StringBuffer sb = new StringBuffer();
		InputStream ps = null;
		try {
			ftp.setControlEncoding("UTF-8");
			ftp.changeWorkingDirectory(fileName);// 改變工作空間
			FTPFile[] ff = ftp.listFiles();//列出fileName下面所有檔案
			
			for (int i = 0; i < ff.length; i++) {
				//如果該檔案是檔案夾,調用自身方法
				if (ff[i].isDirectory()) {
					String s = readFile(ftp, ff[i].getName());
					sb.append(s);
				} else {
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
					String date=dateFormat.format(new Date());
					System.out.println("目前時間"+date);
					//讀取以當天日期命名的txt檔案
					if (ff[i].getName().matches(date+".*.txt")) {
						System.out.println(ff[i].getName());
						//以二進制流的方式讀取
						ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
						ftp.enterLocalPassiveMode();
						
						ps = ftp.retrieveFileStream(ff[i].getName());
						if (ps != null) {
							BufferedReader br = new BufferedReader(new InputStreamReader(ps));
							String data = null;
							sb.append(ff[i].getName());
							try {
								while ((data = br.readLine()) != null) {
									sb.append(data);
								}
								//關閉流
								if (br != null) {
									br.close();
								}
								ftp.completePendingCommand();

							} catch (IOException e) {

								e.printStackTrace();

							}
						}
					}
				}
			}
		} catch (IOException e) {
			log.error("檔案讀取失敗");
			e.printStackTrace();
		}

		return sb.toString();
	}
           
//關閉ftp連接配接
	public static void closeFTPClient(FTPClient ftpClient) {
		try {
			ftpClient.disconnect();
			log.info("連接配接關閉");
		} catch (IOException e) {
			log.error("關閉ftp異常...", e);
		}
	}
           

寫個main方法測試一下

public static void main(String[] args) {
		FtpRead ftp = new FtpRead();
		
		FTPClient ftc = ftp.getFtpClient("10.5.202.7", 21, "aa", "test");
		try {
			FTPFile[] ftf = ftc.listFiles();
			for(FTPFile  f:ftf){
				String s=ftp.readFile(ftc,"/"+ f.getName());
				System.out.println(s);
			}
		} catch (IOException e) {
			e.printStackTrace();

		}finally {
			
			FtpRead.closeFTPClient(ftc);
		}
	}
           

運作結果:

java循環讀取ftp伺服器上面的檔案

ftp目錄結構為:

java循環讀取ftp伺服器上面的檔案
java循環讀取ftp伺服器上面的檔案

   之前沒有負責過這一塊的任務,現在學習一下,第一次寫部落格,寫的不好請見諒哈!