今天需要将FTP某个文件夹下的图片做批量的展示,准备使用BASE64进行编码在前台直接显示
在使用ftpClient.listFiles()
方法将所有的文件名取出后准备在循环中得到inputstream
流,但是再操作中发现,一个图片正常显示,但是多个的话就只有第一个可以取到流,其他的均为空。试了网上的很多方法然并卵。
最后发现要将每一个循环都当做一个FTP通讯要将所有的IO
,FTPClient
均在循环中进行关闭
下边是涉事的代码
InputStream inputStream=null;
BufferedInputStream bufferedInputStream=null;
ByteArrayOutputStream byteArrayOutputStream=null;
BufferedOutputStream bufferedOutputStream=null;
List<Object> list=new ArrayList<Object>();
Map<String, String> map=null;
try {
String diretory=FTPDIR+rightId;
List<FTPFile> ftpFiles=fileCenFTPService.achieveFolderListByDir(diretory);
String fileName=null;
if(ftpFiles!=null && ftpFiles.size()>0){
for(int i=0;i<ftpFiles.size();i++){
FTPFile ftpFile = ftpFiles.get(i);
map=new HashMap<String, String>();
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<FTPFile> 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<FTPFile> folderList = new ArrayList<FTPFile>();
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);
}
}