黔西南建设厅网站,服务器和网站的关系,互联网行业发展,宁波住房和城乡建设网站最近在开发关于java读取ftp中TXT文件#xff0c;其中有些坑踩了一下#xff0c;再次做个记录1、读取文件时我会根据文件名称去生成数据库表#xff0c;oracle数据库对于表名的长度是有限制的#xff0c;最多30个字符2、对于多个文件的ftp的读取#xff0c;每次获取文件后再…最近在开发关于java读取ftp中TXT文件其中有些坑踩了一下再次做个记录1、读取文件时我会根据文件名称去生成数据库表oracle数据库对于表名的长度是有限制的最多30个字符2、对于多个文件的ftp的读取每次获取文件后再次回去文件的流会为空即在循环中多次根据ftp获取文件的流当出现这种情况时需要在循环时每次开启和关闭ftp的链接即可解决否则在第二次获取的时候inputsteam为null3、读取txt文件时如果文件中包含中文进行读取时可能会出现乱码这是可设置读取的字符集为UTF-8如果不行再试试GB23124、java读取TXT文件InputStreamReader reader new InputStreamReader(is, GB2312);BufferedReader br new BufferedReader(reader);String lineTxt null; //每行数据int rowNum 0;while ((lineTxt br.readLine()) ! null) {}补充知识Java实现从FTP获取文件下载到本地并读取文件中的内容的成功方法我就废话不多说了大家还是直接看代码吧~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(endpointInterfacecom.aof.web.servlet.QualityComplainServices)public class QualityComplainServicesImpl implements QualityComplainServices {//ftp对象private FTPClient ftp;//需要连接到的ftp端的ipprivate String ip 10.46.249.7;//连接端口默认21private int port 21;//要连接到的ftp端的名字private String name DKEDI;//要连接到的ftp端的对应得密码private String pwd Pssw0rd;//调用此方法输入对应得ip端口要连接到的ftp端的名字要连接到的ftp端的对应得密码。连接到ftp对象并验证登录进入ftopublic 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 {// 下载过的不在下载了OutputStream out new FileOutputStream(f);if (f.length() lRemoteSize) {System.out.println(~~~~~~~~~~~~~~~~~~~~~~~~本地已经存在,下载中止);out.flush();out.close();}boolean iss ftp.retrieveFile(file.getName(), out);System.out.println(~~~~~~~~~~~~~~~~~~~~~~~~下载成功);out.close();} catch (Exception ex) {ex.printStackTrace();System.out.println(~~~~~~~~~~~~~~~~~~~~~~~~下载失败);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(订单内容为------------------ FileContent } catch (FileNotFoundException e) {System.out.println(没有找到合适的订单信息);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(远程路径为*************************remote);FTPFile[] files ftp.listFiles(remote); // 通过路径得到文件System.out.println(文件数量为*************************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\);}/*//读取文件内容将内容存数据库InputStreamReader isr new InputStreamReader(new FileInputStream(file), utf-8);BufferedReader br new BufferedReader(isr);String lineTxt null;while ((lineTxt br.readLine()) ! null) {lineTxtlineTxt;}System.out.println(lineTxt);br.close();*/}else{System.out.println(************* 文件不存在 ************);}}} catch (Exception e) {e.printStackTrace();}}Overridepublic String threeDAndEightDReports(String orderNum, String FTPUrl, String FileType) {//抱怨单号、FTP地址、3D/8D文件类型System.out.println(1-------------orderNum);System.out.println(2-------------FTPUrl);System.out.println(3-------------FileType);if(null ! orderNum null ! FTPUrl null ! FileType){//连接FTPboolean flag this.ftp1();if(flag){try {//获取文件、解析文件内容进库操作this.gmRead(FTPUrl);// 关闭连接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文件的案例就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持云海天教程。原文链接https://blog.csdn.net/zhang06105586/article/details/105410803