美文网首页
显示图片

显示图片

作者: Ukuleler | 来源:发表于2019-01-16 11:29 被阅读0次

springboot部署的时候一般打成jar包,那么在想象一下图片下载,并展示的情况。下载的图片必须要服务器中间件才可以通过url访问,那么tomcat被内嵌到jar包内,无法进行展示,那么有两种方法,第一种将图片转换成流的形式,第二种是官方推荐的。第一种不做赘述,这里看一下官方推荐做法如下。
js中通过后台传过来的文件url地址,将之作为参数访问获取图片地址。

var host = location.host;
            var uurl =[[${picUrl}]];
            var tp = [[${tp}]];
            if(tp == 1){
            var xmlhttp;
            xmlhttp=new XMLHttpRequest();
            xmlhttp.open("GET","http://"+host+"/updateImgss/"+uurl,true);
            xmlhttp.responseType = "blob";
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState==4){
                    if (xmlhttp.status == 200) {
                        var blob = this.response;
                        var img = document.createElement("img");
                        img.style.width=60+"px";
                        img.style.height=60+"px";
                        img.onload = function(e) {
                        window.URL.revokeObjectURL(img.src);
                        };
                        img.src = window.URL.createObjectURL(blob);
                        $("#iii").append(img);
                    }
                }

            }
            xmlhttp.send();

controller如下

@Autowired
    public LoginController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

 @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/updateImgss/{filename:.+}")
    public  ResponseEntity<?>  getFile(@PathVariable String filename ) {
        try {
            String a = Paths.get(ROOT, filename).toString();
            Resource r = resourceLoader.getResource("file:"+a);
            return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(ROOT, filename).toString()));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

相关文章

  • 图片显示

    //标签按钮 @property(nonatomic,strong)UILabel *titleLabel; //...

  • 显示图片

    springboot部署的时候一般打成jar包,那么在想象一下图片下载,并展示的情况。下载的图片必须要服务器中间件...

  • 显示图片

    import pygame

  • 21_显示YUV图片&视频

    一、显示YUV图片 显示 YUV 图片和显示 BMP 图片的大致流程是一样的。显示 BMP 图片我们可以直接获取到...

  • UI基础控件- UIImageView

    UIImageView 功能:显示图片 常见属性:image:显示的图片animationImages:显示的动画...

  • YYImage/YYWebImage

    YYImage YYKit的图像框架 显示普通动画类型图片 显示帧动画 显示精灵图片精灵图片? 判断图片格式 YY...

  • July 27-day12-Python初识pygame

    显示字体 显示结果: 显示图片 显示结果: 显示图形 显示结果:

  • 【GeekBand】UIImage显示图片

    UIImage 通过属性显示assets中的图片 通过代码显示图片 显示图片最直接的方式就是使用Image Vie...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • flutter 显示本地默认

    简单的flutter 显示网络图片之前显示本地默认图片

网友评论

      本文标题:显示图片

      本文链接:https://www.haomeiwen.com/subject/ydopdqtx.html