JAVA读取FTP文件并转为字符串
来源:互联网 发布:淘宝店主自拍文胸 编辑:程序博客网 时间:2024/05/17 08:17
从FTP服务器读取文件,并以字符串形式输出内容
其中包含了FTP读文件与文件转字符串两方面,需要的朋友自己拆分功能
本代码依赖commons-net-3.4.jar,请自行度娘下载
ReadFile.java
package com.crm.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.SocketException;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;/** * @author DingJiaCheng * 读取文件,转为字符串 * */public class ReadFile {public static String read(String path,String ftpuser,String ftppwd) {if(path.contains("ftp:") || path.contains("FTP:")){String ftppath = path.substring(19,43); //暂时写死String localPath="C:\\Program Files\\hwlogstemp\\";int reply;FTPClient ftp = new FTPClient();try {ftp.connect(path.substring(6,18)); //暂时写死//2.登录服务器 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftp.login(ftpuser, ftppwd); //3.判断登陆是否成功 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect();}ftp.changeWorkingDirectory(ftppath);// 转移到FTP服务器目录 //5.遍历下载的目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { //解决中文乱码问题,两次解码 byte[] bytes=ff.getName().getBytes("iso-8859-1"); String fn=new String(bytes,"utf8"); if(fn.contains("json")){ //6.写操作,将其写入到本地文件中 File localFile = new File(localPath + "temp.json"); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout();path = localPath + "temp.json"; } catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if (ftp.isConnected()){try {ftp.disconnect();} catch (IOException e) {}}} }BufferedReader reader = null;String laststr = "";try {FileInputStream fileInputStream = new FileInputStream(path);InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "gb2312");reader = new BufferedReader(inputStreamReader);String tempString = null;while ((tempString = reader.readLine()) != null) {laststr += tempString;}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}return laststr;}public static void main(String[] args) {String path = "ftp://123.123.123.123/abcde/defghi.txt";String res = read(path,"username","password");System.out.println(read(path));}
0 0
- JAVA读取FTP文件并转为字符串
- 读取文件转为java字符串
- java读取ftp文件,并获取文件内容
- Java读取远程文件[Http,ftp],并保存
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- java 生成xml并转为字符串
- java读取ftp文件下载
- JS中将字符串转为XML并读取对象的值
- JS中将字符串转为XML并读取对象值实例
- Javascript将字符串转为XML并读取对象值
- python 读取csv文档hex字符串 转为int并存储
- 将图像转为TFRecord文件并读取TFRecord文件
- 将图像转为TFRecord文件并读取TFRecord文件
- 将图像转为TFRecord文件并读取TFRecord文件
- Python读取mat文件,并转为csv文件
- Java读取文件内容并转换为字符串
- 用通俗易懂的大白话讲解Map/Reduce原理
- 使用高德地图API时出现错误为:10定位服务启动失败的解决方法
- unity terrain export to obj
- Linux学习3之shell的if大小比对使用
- 如何将源码编译生成RPM包
- JAVA读取FTP文件并转为字符串
- Eclipse代码提示功能失效
- Go语言以匿名组合的方式实现继承
- Android开发性能优化大总结
- SAX
- 【OpenCL】Coalescing Memory Accesses
- 使用rosserial把Arduino作为ROS一个Node遇到的一个问题
- JAVA 多态 由浅及深介绍
- linux之shell学习:tr的使用