java retrievefilestream_使用FTP的retrieveFileStream在循环中取多个文件报NULL

今天需要将FTP某个文件夹下的图片做批量的展示,准备使用BASE64进行编码在前台直接显示

在使用ftpClient.listFiles()方法将所有的文件名取出后准备在循环中得到inputstream流,但是再操作中发现,一个图片正常显示,但是多个的话就只有第一个可以取到流,其他的均为空。试了网上的很多方法然并卵。

最后发现要将每一个循环都当做一个FTP通讯要将所有的IO,FTPClient均在循环中进行关闭

下边是涉事的代码

InputStream inputStream=null;

BufferedInputStream bufferedInputStream=null;

ByteArrayOutputStream byteArrayOutputStream=null;

BufferedOutputStream bufferedOutputStream=null;

List list=new ArrayList();

Map map=null;

try {

String diretory=FTPDIR+rightId;

List ftpFiles=fileCenFTPService.achieveFolderListByDir(diretory);

String fileName=null;

if(ftpFiles!=null && ftpFiles.size()>0){

for(int i=0;i

FTPFile ftpFile = ftpFiles.get(i);

map=new HashMap();

fileName = ftpFile.getName();

inputStream=fileCenFTPService.fileInputStreamDownload(diretory+"/"+fileName);

bufferedInputStream = new BufferedInputStream(inputStream);

byteArrayOutputStream =new ByteArrayOutputStream();

bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);

byte[] buffer=new byte[1024];

int len=bufferedInputStream.read(buffer);

while(len!=-1){

bufferedOutputStream.write(buffer, 0, len);

len=bufferedInputStream.read(buffer);

}

bufferedOutputStream.flush();

byte[] bytes=byteArrayOutputStream.toByteArray();

BASE64Encoder encoder = new BASE64Encoder();

String base64String=encoder.encode(bytes).trim();

map.put("fileName", fileName);

map.put("content", base64String);

list.add(map);

bufferedOutputStream.close();

byteArrayOutputStream.close();

inputStream.close();

fileCenFTPService.disconnect();

}

里边主要的两个方法是封装好的

@Override

public List achieveFolderListByDir(String directory) throws IOException {

try {

FTPClient ftpClient = achieveFTPClient();

ftpClient.setControlEncoding("GBK");//设置编码,否则中文名是乱码

// System.out.println(directory);

// if(!ftpClient.changeWorkingDirectory(directory)){

// ftpClient.makeDirectory(directory);

// }

//解决中文路径的问题

if(ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"ISO-8859-1"))) {

FTPFile[] ftpFileArr = ftpClient.listFiles();

// System.out.println(ftpFileArr.length);

List folderList = new ArrayList();

for(FTPFile file : ftpFileArr){

// System.out.println(file.getName());

folderList.add(file);

}

return folderList;

} else {

return null;

}

} catch (Exception er) {

throw new IOException("获取文件列表失败 : " + er.getMessage());

} finally {

disconnect();

}

}

@Override

public InputStream fileInputStreamDownload(String remoteName) throws IOException {

InputStream is = null;

try {

FTPClient ftpClient = achieveFTPClient();

is = ftpClient.retrieveFileStream(new String(remoteName.getBytes("GBK"),"ISO-8859-1"));

} catch (IOException er) {

throw new IOException("下载文件流失败 : " + er.getMessage());

}

return is;

}

public void disconnect() throws IOException {

try {

FTPClient ftpClient = achieveFTPClient();

if (ftpClient.isConnected()) {

ftpClient.disconnect();

ftpClient = null;

}

} catch (IOException e) {

throw new IOException("Could not disconnect from server.", e);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值