针对jar里面的图片不显示问题

做了一个html生产pdf案例。

然后把图片放到resource/static/model/img下面,生成jar包运行,发现图片不显示,

发现html里面的src必须是http域名开头的图片。

下面来说说解决的方法:

一。将图片放到web目录下,直接http路径显示

http://xxx.com/static/img/1.png

  

二。通过Resource读取资源,然后利用controller输出

@RequestMapping("/getimg")
    public void getConfig(String path) throws Exception {
        Resource resource = new ClassPathResource(path);
        InputStream is = resource.getInputStream();
        int i = is.available(); // 得到文件大小
        byte data[] = new byte[i];
        is.read(data); // 读数据
        is.close();
        response.setContentType("image/*"); // 设置返回的文件类型
        OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
        toClient.write(data); // 输出数据
        toClient.close();
    }  

src这样显示

 <img src="http://localhost:8081/api/getimg?path=/static/model/img/flightDelay.png" class="iconUrl" />

  

三,如果图片不多的话,用base64位显示

 <img class="logo" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdoAAACQCAYAAACvUKoRAAA....." />

  

猜你喜欢

转载自www.cnblogs.com/achengmu/p/10874861.html