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
0
投稿
猜你喜欢
- 本文实例讲述了C#使用Dispose模式实现手动对资源的释放。分享给大家供大家参考。具体实现方法如下://单一类的实现class MyCla
- 1、Java版package com.lyz.utils.common; import java.io.UnsupportedEncodin
- 这篇文章主要介绍了java获取当前时间的四种方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- #include<iostream>using namespace std;//非递归求解所有的子集void fun(int a
- MD5加密在我们的程序中,不管是什么,都会有安全问题,今天就说的是MD5加密的方法MD5是哈希算法,也就是 从明文A到密文B很容易,但是从密
- C# 自带的HttpWebRequest效率太低,对于自组HTTP封包不好操作。在写超级SQL注入工具时,研究了很长一段时间如何使用Sock
- ProxyFactory是创建代理类的工厂接口,其中的setProperties方法用来对工厂进行属性设置,但是mybatis内置的两个实现
- 服务提供者@GetMapping("/{id}") public void queryJobInfoLogD
- 本文实例讲述了Java基于递归和循环两种方式实现未知维度集合的笛卡尔积。分享给大家供大家参考,具体如下:什么是笛卡尔积?在数学中,两个集合X
- 简介本文用示例介绍java的Period的用法。Duration和Period说明Duration类通过秒和纳秒相结合来描述一个时间量,最高
- 配置文件请看上篇Java实现redis https://www.jb51.net/article/190922.htm下面测试redis的集
- 开始 在本文中,我将展示如何使用各种不同的 Java 技术构建一些简单的 Comet 风格的 Web 应
- 问答小剧场 以下会产生信息丢失的类型转换是( ) A.float a=10;
- 本文实例为大家分享了java字符串和数字转换工具的具体代码,供大家参考,具体内容如下package com.test.util;/** *
- Object类型是所有类型的基类,其下面有ValueType类型。什么结构啊,枚举啊,都继承ValueType,这些都是值类型。其他的什么类
- 目录一、什么是Spring二、什么是IOC三、快速搭建框架环境四、spring之依赖注入五、详解Spring框架的IOC之注解方式七、Spr
- 游标查询(scroll)简介scroll 查询 可以用来对 Elasticsearch 有效地执行大批量的文档查询,而又不用付出深度分页那种
- (1)实际应用BeanUtils.copyProperties(赋值目标对象,模板源对象);我们都知道当有两个对象AB,属性名称一样的情况下
- MyBatis 获取子类的属性这里有个model类:基类public class user { pu
- 动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而