java读取ftp中TXT文件的案例
作者:zhang06105586 发布时间:2022-07-07 20:10:11
标签:java,ftp,TXT
最近在开发关于java读取ftp中TXT文件,其中有些坑踩了一下,再次做个记录
1、读取文件时我会根据文件名称去生成数据库表,oracle数据库对于表名的长度是有限制的,最多30个字符
2、对于多个文件的ftp的读取,每次获取文件后再次回去文件的流会为空,即在循环中多次根据ftp获取文件的流
当出现这种情况时,需要在循环时每次开启和关闭ftp的链接即可解决,否则在第二次获取的时候inputsteam为null
3、读取txt文件时,如果文件中包含中文,进行读取时可能会出现乱码,这是可设置读取的字符集为UTF-8,如果不行,再试试
GB2312
4、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(endpointInterface="com.aof.web.servlet.QualityComplainServices")
public class QualityComplainServicesImpl implements QualityComplainServices {
//ftp对象
private FTPClient ftp;
//需要连接到的ftp端的ip
private String ip = "10.46.249.7";
//连接端口,默认21
private int port = 21;
//要连接到的ftp端的名字
private String name = "DKEDI";
//要连接到的ftp端的对应得密码
private String pwd = "P@ssw0rd";
//调用此方法,输入对应得ip,端口,要连接到的ftp端的名字,要连接到的ftp端的对应得密码。连接到ftp对象,并验证登录进入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 {// 下载过的不在下载了
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("~~~~~~~~~~~~~~~~~~~~~~~~下载成功\r\n");
out.close();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~下载失败\r\n");
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) {
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) {
//抱怨单号、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){
//连接FTP
boolean 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");
}
}
来源:https://blog.csdn.net/zhang06105586/article/details/105410803


猜你喜欢
- 前言任何一个服务如果没有监控,那就是两眼一抹黑,无法知道当前服务的运行情况,也就无法对可能出现的异常状况进行很好的处理,所以对任意一个服务来
- 本文以Java代码为例介绍如何实现将彩色PDF文件转为灰度(黑白)的PDF文件,即:将PDF文档里面的彩色图片或者文字等通过调用PdfGra
- 创蓝253: https://www.253.com/#region 获取手机验证码(创蓝253) /// <summar
- 本文实例为大家分享了PopupWindow+RecyclerView实现上下滑动框功能的具体代码,供大家参考,具体内容如下1.新建一个适配器
- 前言坚持是一件比较难的事,坚持并不是自欺欺人的一种自我麻痹和安慰,也不是做给被人的,我觉得,坚持的本质并没有带着过多的功利主义,如果满是功利
- 欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demo
- 将Fragment与Layout结合使用,一般都是主Activity以frame填充Activity的方式交互管理Fragment :1.由
- 本文实例讲述了C#实现将记事本中的代码编译成可执行文件的方法,运行环境为VS2012,具体方法如下:1、在记事本中编写如下代码:using
- goto在Java中是一个保留字,但在语言中并没有用到它;Java没有goto。但是,Java也能完成一些类似于跳转的操作,主要是依靠:标签
- Android Notification的多种用法总结我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏
- Android动画 实现开关按钮动画(属性动画之平移动画),最近做项目,根据项目需求,有一个这样的功能,实现类似开关的动画效果,经过自己琢磨
- C语言是一种高级编程语言,其最重要的特点之一是它允许程序员使用函数来组织代码。函数是一组相关的指令的集合,可以在程序中多次调用。在 C语言中
- 前言想必大家对c语言的动态内存分配并不陌生,忘了的小伙伴也可以看看我的这篇文章C语言动态内存分配c语言的动态内存分配由于有些地方用起来比较麻
- 最近没做项目,重新整理了一个最完整的Mybatis Generator(简称MBG)的最完整配置文件,带详解,再也不用去看EN的User G
- sql中like通配符模糊匹配问题针对oracle数据库:将查询条件通过功能类处理/** * Desc
- 原因分析使用ajax从前台页面传输数据到后台controller控制器的时候,出现中文乱码其实乱码问题出现的原因,就是由于默认的tomcat
- springboot 取消starter的自动注入starer是spring boot中一个很重要的概念,starter相当于一个模块,它能
- 在使用spring提供的JpaTemplate进行查询时,如果数据量超过100 条,查询效率就会明显降低。由于开始时使用JPA内部的双向关联
- 本文实例为大家分享了android计算器实现加减乘除的具体代码,供大家参考,具体内容如 * :以下计算器只注重实现功能,不考虑其他BUG,只有
- 配置多个别名 typeAliasesPackage<property name="typeAliasesPackage&qu