java讀取ftp中TXT文件的案例
最近在開(kāi)發(fā)關(guān)于java讀取ftp中TXT文件,其中有些坑踩了一下,再次做個(gè)記錄
1、讀取文件時(shí)我會(huì)根據(jù)文件名稱去生成數(shù)據(jù)庫(kù)表,oracle數(shù)據(jù)庫(kù)對(duì)于表名的長(zhǎng)度是有限制的,最多30個(gè)字符
2、對(duì)于多個(gè)文件的ftp的讀取,每次獲取文件后再次回去文件的流會(huì)為空,即在循環(huán)中多次根據(jù)ftp獲取文件的流
當(dāng)出現(xiàn)這種情況時(shí),需要在循環(huán)時(shí)每次開(kāi)啟和關(guān)閉ftp的鏈接即可解決,否則在第二次獲取的時(shí)候inputsteam為null
3、讀取txt文件時(shí),如果文件中包含中文,進(jìn)行讀取時(shí)可能會(huì)出現(xiàn)亂碼,這是可設(shè)置讀取的字符集為UTF-8,如果不行,再試試
GB2312
4、java讀取TXT文件:
InputStreamReader reader = new InputStreamReader(is, 'GB2312');BufferedReader br = new BufferedReader(reader);String lineTxt = null; //每行數(shù)據(jù)int rowNum = 0;while ((lineTxt = br.readLine()) != null) {}
補(bǔ)充知識(shí):Java實(shí)現(xiàn)從FTP獲取文件下載到本地,并讀取文件中的內(nèi)容的成功方法
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
package com.aof.web.servlet;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import javax.jws.WebService;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;@WebService(endpointInterface='com.aof.web.servlet.QualityComplainServices')public class QualityComplainServicesImpl implements QualityComplainServices { //ftp對(duì)象 private FTPClient ftp; //需要連接到的ftp端的ip private String ip = '10.46.249.7'; //連接端口,默認(rèn)21 private int port = 21; //要連接到的ftp端的名字 private String name = 'DKEDI'; //要連接到的ftp端的對(duì)應(yīng)得密碼 private String pwd = 'P@ssw0rd'; //調(diào)用此方法,輸入對(duì)應(yīng)得ip,端口,要連接到的ftp端的名字,要連接到的ftp端的對(duì)應(yīng)得密碼。連接到ftp對(duì)象,并驗(yàn)證登錄進(jìn)入fto public boolean ftp1() { ftp = new FTPClient(); try {// ftp.connect(ip, port); if(!ftp.isConnected()){ ftp.connect(ip, port); } System.out.println(ftp.login(name, pwd));// ftp.setCharset(Charset.forName('UTF-8')); ftp.setControlEncoding('UTF-8'); return true; } catch (IOException e) { e.printStackTrace(); return true; } } public void disconnect() throws Exception { if (ftp.isConnected()) { ftp.disconnect(); } } // 下載文件到本地 public boolean download(FTPFile file) throws Exception { boolean result = true; // 本地文件路徑 File f = new File('E:crmFiles'); if (!f.exists()) { f.getParentFile().mkdirs(); } long lRemoteSize = file.getSize(); try {// 下載過(guò)的不在下載了 OutputStream out = new FileOutputStream(f); if (f.length() >= lRemoteSize) { System.out.println('~~~~~~~~~~~~~~~~~~~~~~~~本地已經(jīng)存在,下載中止'); out.flush(); out.close(); } boolean iss = ftp.retrieveFile(file.getName(), out); System.out.println('~~~~~~~~~~~~~~~~~~~~~~~~下載成功rn'); out.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println('~~~~~~~~~~~~~~~~~~~~~~~~下載失敗rn'); return false; } return result; } private InputStreamReader read; private BufferedReader reader; private String preRead(String filepath) throws Exception { File file = new File(filepath); String ordertype = null; if (file.isFile() && file.exists()) { try { read = new InputStreamReader(new FileInputStream(file), 'GBK'); reader = new BufferedReader(read); StringBuffer FileContent = new StringBuffer(); String temp = null; while ((temp = reader.readLine()) != null) { FileContent.append(temp); } System.out.println('訂單內(nèi)容為------------------>>>>> '+FileContent+' <<<<<------------------'); } catch (FileNotFoundException e) { System.out.println('!!!!!!!!!!!!!!!!!沒(méi)有找到合適的訂單信息!!!!!!!!!!!!!!!'); e.printStackTrace(); } finally { reader.close(); read.close();// file.delete(); } } return ordertype; } public void gmRead(String remote) throws Exception { boolean downloadResult = false; try { ftp.changeWorkingDirectory(remote); System.out.println('遠(yuǎn)程路徑為*************************'+remote); FTPFile[] files = ftp.listFiles(remote); // 通過(guò)路徑得到文件 System.out.println('文件數(shù)量為*************************'+files.length); for (int i = 0; i < files.length; i++) { FTPFile file = files[i]; if (file.isFile()) { downloadResult = this.download(file);// 下載文件 到本地讀取路徑 if (downloadResult) { String ordertype = this.preRead('E:crmFiles'); } /*//讀取文件內(nèi)容,將內(nèi)容存數(shù)據(jù)庫(kù) InputStreamReader isr = new InputStreamReader(new FileInputStream(file), 'utf-8'); BufferedReader br = new BufferedReader(isr); String lineTxt = null; while ((lineTxt = br.readLine()) != null) { lineTxt+=lineTxt; } System.out.println(lineTxt); br.close();*/ }else{ System.out.println('************* 文件不存在 ************'); } } } catch (Exception e) { e.printStackTrace(); } } @Override public String threeDAndEightDReports(String orderNum, String FTPUrl, String FileType) { //抱怨單號(hào)、FTP地址、3D/8D文件類(lèi)型 System.out.println('1-------------'+orderNum); System.out.println('2-------------'+FTPUrl); System.out.println('3-------------'+FileType); if(null != orderNum && null != FTPUrl && null != FileType){ //連接FTP boolean flag = this.ftp1(); if(flag){ try { //獲取文件、解析文件內(nèi)容,進(jìn)庫(kù)操作 this.gmRead(FTPUrl); // 關(guān)閉連接 this.disconnect(); } catch (Exception e) { e.printStackTrace(); } }else{ System.out.println('!!!!!!!!!!!!!!!!!FTP連接失敗!!!!!!!!!!!!!!!!!'); } return 'success'; }else{ return 'fail'; } } public static void main(String[] args) { QualityComplainServicesImpl q = new QualityComplainServicesImpl(); q.threeDAndEightDReports('001','/CRMINTERFACE','3D'); } }
以上這篇java讀取ftp中TXT文件的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)2. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)3. ASP.NET MVC實(shí)現(xiàn)樹(shù)形導(dǎo)航菜單4. 如何封裝一個(gè)Ajax函數(shù)5. Java 接口和抽象類(lèi)的區(qū)別詳解6. Laravel Eloquent ORM高級(jí)部分解析7. IDEA 搭建maven 安裝、下載、配置的圖文教程詳解8. jsp response.sendRedirect()用法詳解9. Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)10. PHP擴(kuò)展之URL編碼、解碼及解析——URLs
