图片下载完成后,需要转成Base64码,方面浏览器的加载和预览
try {
path = path + imgcode + "/";
logger.info("开始从文件夹里:读取图片");
File files = new File(path);
File[] tempList = files.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
String fileName = tempList[i].getName();
String prefix = fileName.substring(fileName
.indexOf(".") + 1);
logger.info(fileName);
logger.info(prefix);
if (!prefix.equals("xml")) {
Map<String, Object> map = new HashMap<String, Object>();
FileInputStream inputFile;
inputFile = new FileInputStream(tempList[i]);
byte[] buffer = new byte[(int) tempList[i]
.length()]; //获取图片的大小,并构建相应的byte数组
inputFile.read(buffer); //将图片写入字节数组内
inputFile.close();
String base64 = (new Base64Encoder())
.encode(buffer); //base64加密
logger.info("读取成功一张base64:" + base64);
if (prefix.indexOf(".") < 0) {
map.put("base64", base64);
map.put("herf", tempList[i]);
listJpg.add(map);
} else {
}
}
}
}
ImageUtil.deleteDir(files);
} catch (Exception e) {
e.printStackTrace();
}