我们都知道浏览器本身支持直接显示base64编码的图片的 例如
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEAYABgAAD......" width="554" height="402" />
但是通过项目中发现前台浏览器处理这种base编码的图片,如果图片数量很多,单个图片接近1MB的话 浏览器会变得非常卡顿 页面加载特别慢
所以我改成后台解析base64编码的图片还原成图片临时存储在服务器中, 直接返回前台图片地址 这样一来,像以前一样,带链接地址的图片会异步加载 不会导致页面白屏
话不多说 贴代码
/**
* base64编码图片转图片存储并返回访问路径
* @param request
* @param model
* @throws IOException
*/
private void settingImageUrl(HttpServletRequest request, Model t) throws IOException
{
String img = t.getImg();
if (StringUtil.isEmpty(img))
{
return;
}
// “data:image/jpeg;base64,”之后的字符串才是图片,所以一定要去掉这个前缀。
img = img.replace("<img src=\"", "");
img = img.substring(0, img.indexOf("\""));
String[] split = img.split(",");
if (split.length != 2)
{
return;
}
img = split[1];
byte[] bs = ImageUtil.GenerateImage(img);
// 项目在容器中实际发布运行的根路径
String realPath = request.getSession().getServletContext().getRealPath("/") + "temp";
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis()) + ".png";
// 设置存放图片文件的路径
realPath = realPath + File.separator + trueFileName;
System.out.println("存放图片文件的路径:" + realPath);
// 转存文件到指定的路径
FileUtil.createFile(realPath, bs);
String imgUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + "temp" + "/" + trueFileName;
t.setImg(imgUrl);
System.out.println("图片文件的访问地址:" + imgUrl);
}
这样一来图片是存储在项目根目录下的temp文件夹下的 但是这样一来图片如果太多会不会占用服务器 所以可以每次访问都对图片进行清空操作
/**
* 清空图片缓存
*/
String realPath = request.getSession().getServletContext().getRealPath("/") + "temp";
File file = new File(realPath);
if (file.exists())
{
FileUtil.deleteDir(file);
}
代码中图片工具类和文件操作工具类都贴出来如下
/**
* 对Base64编码图片解码返回byte字节流
*
* @param imgStr
* @return 字节流
*/
public static byte[] GenerateImage(String imgStr)
{
if (imgStr == null) // 图像数据为空
return null;
BASE64Decoder decoder = new BASE64Decoder();
try
{
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i)
{
if (bytes[i] < 0)
{// 调整异常数据
bytes[i] += 256;
}
}
return bytes;
}
catch (Exception e)
{
return null;
}
}
/**
* 在本地地址创建文件
* @param filePath 文件 地址
* @param by 字节流
* @return
* @throws IOException
*/
public static File createFile(String filePath, byte[] by) throws IOException
{
File prjfile = new File(filePath.replace("\\", "/").replace("//", "/"));
if (!prjfile.exists())
{
File parent = prjfile.getParentFile();
if (!parent.exists())
{
parent.mkdirs();
}
// 创建新文件
prjfile.createNewFile();
}
BufferedOutputStream out = null;
try
{
//将byte数组写入 文件
out = new BufferedOutputStream(new FileOutputStream(prjfile));
out.write(by);
out.flush();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
release(out);
}
return prjfile;
}
/**
* 递归删除文件夹以及下面所有文件
* @param directoryPath
* @return
*/
public static boolean deleteDir(File file)
{
if (file == null)
{
return false;
}
if (file.isDirectory())
{
String[] fileNames = file.list();
for (String fileName : fileNames)
{
boolean success = FileUtil.deleteDir(new File(file, fileName));
if (!success)
{
return false;
}
}
}
return file.delete();
}