社区
Java EE
帖子详情
FTPClient的循环读取问题!!
wh5700057
2013-11-19 02:11:46
for (int i = 0; i < fileNameList.size(); i++) {
InputStream in = m_ftpClient.retrieveFileStream(fileNameList.get(i));
System.out.println(in);
}
上方的代码,循环第一个文件时InputStream是有值,继续循环之后的文件InputStream就全为null了。不知道为什么,大神有遇到过吗?
...全文
134
回复
打赏
收藏
FTPClient的循环读取问题!!
for (int i = 0; i < fileNameList.size(); i++) { InputStream in = m_ftpClient.retrieveFileStream(fileNameList.get(i)); System.out.println(in); } 上方的代码,循环第一个文件时InputStream是有值,继续循环之后的文件InputStream就全为null了。不知道为什么,大神有遇到过吗?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
ftp
上传文本、移动文件、删除文件
public class
FTP
Util { private
FTP
Client
ftp
Client
=null; private boolean result = false; private FileInputStream fis; String
ftp
Host = "10.16.111.111"; String port = 21 String
ftp
UserName = "
ftp
user11; String
ftp
Password = "1234561"; /** * 登录服务器 * @param
ftp
Info * @return * @throws IOException */ public
FTP
Client
login() throws IOException {
ftp
Client
= new
FTP
Client
();
ftp
Client
.connect(
ftp
Host); boolean login =
ftp
Client
.login(
ftp
UserName,
ftp
Password); int reply =
ftp
Client
.getReplyCode(); if (!
FTP
Reply.isPositiveCompletion(reply)) {
ftp
Client
.disconnect(); } if(login){ System.out.println("
ftp
连接成功!"); }else{ System.out.println("
ftp
连接失败!"); } //
ftp
Client
.setControlEncoding("GBK"); return
ftp
Client
; } /** * 字符串作为文件上传指定目录 下 * @param content 源字符串 * @param uploadDir 上传目录 * @param
ftp
FileName 上传文件名称 * @throws Exception */ public void
ftp
UploadByText(String content ,String uploadDir,String
ftp
FileName) throws Exception{ try {
ftp
Client
= this.login(); //创建目录 createDir(
ftp
Client
,uploadDir); // 设置上传目录 这个也应该用配置文件
读取
ftp
Client
.changeWorkingDirectory(uploadDir);
ftp
Client
.setBufferSize(1024);
ftp
Client
.setControlEncoding("GBK"); // 设置文件类型(二进制)
ftp
Client
.setFileType(
FTP
Client
.BINARY_FILE_TYPE); String fileName = new String(
ftp
FileName.getBytes("GBK"),"iso-8859-1"); OutputStream os =
ftp
Client
.storeFileStream(fileName); byte[] bytes = content.getBytes(); os.write(bytes); os.flush(); os.close(); } catch (Exception e) {
ftp
Client
.disconnect();
ftp
Client
= null; e.printStackTrace(); throw e; }finally{
ftp
Client
.disconnect();
ftp
Client
= null; } } /** * 移动文件 * @param
ftp
Info * @return * @throws Exception */ public boolean moveFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.moveFile(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath(),
ftp
Info.getFilePath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.disconnect();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 删除文件 * @param
ftp
Info * @return * @throws Exception */ public boolean deleteFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.deleteByFolder(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.disconnect();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 实现文件的移动,这里做的是一个文件夹下的所有内容移动到新的文件, * 如果要做指定文件移动,加个判断判断文件名 * 如果不需要移动,只是需要文件重命名,可以使用
ftp
.rename(oleName,newName) * @param
ftp
* @param oldPath * @param newPath * @return */ public boolean moveFile(
FTP
Client
ftp
,String oldPath,String newPath){ boolean flag = false; try {
ftp
.changeWorkingDirectory(oldPath);
ftp
.enterLocalPassiveMode(); //获取文件数组
FTP
File[] files =
ftp
.listFiles(); //新文件夹不存在则创建 if(!
ftp
.changeWorkingDirectory(newPath)){
ftp
.makeDirectory(newPath); } //回到原有工作目录
ftp
.changeWorkingDirectory(oldPath); for (
FTP
File file : files) { if(file.isDirectory()) { moveFile(
ftp
,oldPath+file.getName()+"/" ,newPath+file.getName()+"/"); }else{ //转存目录 flag =
ftp
.rename(oldPath+new String(file.getName().getBytes("GBK"),"ISO-8859-1"), newPath+"/"+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } if(flag){ System.out.println(file.getName()+"移动成功"); }else{ System.out.println(file.getName()+"移动失败"); } }
ftp
.removeDirectory(new String(oldPath.getBytes("GBK"),"ISO-8859-1")); } catch (Exception e) { e.printStackTrace(); System.out.println("移动文件失败"); } return flag; } /** * 删除
FTP
上指定文件夹下文件及其子文件方法,添加了对中文目录的支持 * @param
ftp
FTP
Client
对象 * @param
Ftp
Folder 需要删除的文件夹 * @return */ public boolean deleteByFolder(
FTP
Client
ftp
,String
Ftp
Folder){ boolean flag = false; try {
ftp
.changeWorkingDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1"));
ftp
.enterLocalPassiveMode();
FTP
File[] files =
ftp
.listFiles(); for (
FTP
File file : files) { //判断为文件则删除 if(file.isFile()){
ftp
.deleteFile(
Ftp
Folder+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } //判断是文件夹 if(file.isDirectory()){ String childPath =
Ftp
Folder +file.getName()+ "/"; //递归删除子文件夹 deleteByFolder(
ftp
,childPath); } } //
循环
完成后删除文件夹 flag =
ftp
.removeDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1")); if(flag){ System.out.println(
Ftp
Folder+"文件夹删除成功"); }else{ System.out.println(
Ftp
Folder+"文件夹删除成功"); } } catch (Exception e) { e.printStackTrace(); System.out.println("删除失败"); } return flag; } /** * 创建目录 * @param createpath * @param s
ftp
*/ public void createDir(
FTP
Client
ftp
Client
,String createpath) throws Exception { try { if(
ftp
Client
.changeWorkingDirectory(createpath)) { return; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if(!
ftp
Client
.changeWorkingDirectory(filePath.toString())) {
ftp
Client
.makeDirectory(filePath.toString());
ftp
Client
.changeWorkingDirectory(filePath.toString()); } }
ftp
Client
.changeWorkingDirectory(createpath); }catch (Exception e) { e.printStackTrace(); throw new Exception("创建路径错误:" + createpath); } } }
ftp
工具类,修复了获取输入流为空
包含文件上传,文件删除,文件列表,查询当天文件类表方法; 已经填坑,第一个上传的里面获取指定路径下输入流集合会有空对象;此坑已填
java
ftp
读写文件_
FTP
Client
连续
读取
文件
最近在使用
FTP
Client
连续
读取
ftp
上的多个文件内容时,遇到了两个
问题
:1. 在for
循环
中,
FTP
Client
只能
读取
到第一个文件内容,
读取
第二个时遇到NPE
问题
。2. 遇到程序锁死。下面就遇到
问题
进行还原分析,并提供解决办法:现在使用
FTP
Client
读取
ftp
服务器/aaa/bbb/目录下a.txt,b.txt两个文件
问题
1:通过Debug发现在
读取
第一个文件前
FTP
Client
的_r...
java
循环
读取
ftp
服务器上面的文件
公司新项目需要在
ftp
服务器上
读取
文件保存到数据库,所以学习了一下怎么
读取
项目采用的是springboot框架,首先需要在pom文件中加入comment.net jar包依赖 commons-net commons-net 3.3 然后写一个
ftp
登陆的方法,采用@Slf4j注解 来引入log日志 //传入
ftp
地址,端口,登
java
ftp
文件
读取
_
FTP
Client
从
ftp
上
读取
文件夹能读出来,但是读文件读不出来
代码如下:String remoteHost = snDataSynchronizationParam.get("RemoteHost");//
FTP
地址String port = snDataSynchronizationParam.get("RemotePort");// 端口String remotePath = snDataSynchronizationParam.get("Remote...
Java EE
67,513
社区成员
225,878
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章