java springMVC 图片缓存问题

1 篇文章 0 订阅
1 篇文章 0 订阅

现在做的项目,需要将图片一次性加载到页面上,我试过从数据库里面读二进制,发现花的时间太长,基本上要十几秒才能加载出来,而且图片越多加载越慢,就想着怎么能将图片下载到本地,再去读本地的地址,这样肯定就会快很多,事实上确实如此。废话不多说,代码上来。

因为这里讲的是图片缓存,上传图片在这里就不多说了

 public void pageLoad()
    {
        String itemtype = "checkpointphoto";
        String str = "";
        String str2 = "";
        String sql = "select * from frame_attachstorage sto LEFT JOIN  frame_attachinfo info on sto.ATTACHGUID=info.ATTACHGUID where info.uploaduserguid = '"
                +userSession.getUserGuid()+"' and sto.contenttype like 'image%'";
        if(StringUtil.isNotBlank(itemtype)){
            sql += "and sto.cliengtag = '"+itemtype+"'";
        }
        List<FrameAttachStorage> attaches = service.findList(sql, FrameAttachStorage.class);
        for( int i=0;i<attaches.size();i++){
            FrameAttachStorage storage = attaches.get(i);
            String type = storage.getContentType();
            if(".png".equals(type)||".jpg".equals(type)||".jpeg".equals(type)||".gif".equals(type)||".bmp".equals(type)){
                
              //下载到本地
                FileMkdir fm = new FileMkdir();

//decideFile 是下面要调用到的方法,这个很关键
                String url = fm.decideFile(storage);
                //拼接前台内容
                if(i==0){
                    str += "<li style=\"z-index: 1;\"><img height=\"380\" width=\"650\" src=\"" + url + "\" /></li>";
                    str2 += "<li style=\"filter: alpha(opacity : 100); opacity: 1;\"><img src=\"" + url + "\" /></li>";
                }else{
                    str += "<li><img height=\"380\" width=\"650\" src=\"" + url + "\" /></li>";
                    str2 += "<li><img src=\"" + url + "\" /></li>";
                } 
            }
        }

//传给前台
        addCallbackParam("str", str);
        addCallbackParam("str2", str2);
    }

下面的这个方法是我写的公共类里面的,方便复用

public String decideFile(FrameAttachStorage storage){
        //获取当前项目路径
        String pwd = System.getProperty("user.dir");
        //查找图片路径下有没有需要加载的图片,如果没有就从数据库里面下载
        File file=new File(pwd+"/src/main/webapp/pages/images/"+storage.getAttachGuid()+".jpg");    
        if(!file.exists()) {
            try {
                BufferedImage bufferedImage = ImageIO.read(storage.getContent());
                Image image = bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT);
                BufferedImage outputImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics graphics = outputImage.getGraphics();
                graphics.drawImage(image, 0, 0, null);
                graphics.dispose();
                ImageIO.write(outputImage, "jpg", new File(pwd+"/src/main/webapp/pages/images/"+storage.getAttachGuid()+".jpg"));
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
        }  
        String url = "../../images/"+storage.getAttachGuid()+".jpg";
        return url;
    }


这样就可以直接从本地读取图片了,一般加载几十张图片两秒内都能搞定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值