zoukankan      html  css  js  c++  java
  • springboot 项目中读取资源文件内容 如图片、文档文件

    1 问题描述:在 springboot 项目中有时候会需要读取一些资源文件,例如 office的 docx 文档或者 png、jpg的图片。在多模块项目中资源文件需要放到启动项目的 Resources 文件夹

    示例代码如下:

    InputStream pngInStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("img.png");
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int n;
            while ((n = pngInStream.read(buffer)) != -1) {
                out.write(buffer,0,n);
            }
            System.out.println(out.toByteArray());

    可以将 Resources 目录下的 img.png 图片读取出来,存放到 out 对象中。

    在应用中涉及到 io 操作时最好将数据转换成 io 流,提高运算速度,可以到内存中运算。ByteArrayOutputStream, ByteArrayInputStream

    不要使用 InputStream.avaiable() 方法,在不同系统中读到的数据可能不一样

  • 相关阅读:
    5G名词术语
    什么是IPv6
    如何用SecureCRT 登录eNSP模拟器里的设备
    numpy-排序
    numpy-tile 数组复制
    经纬度计算距离与方位角
    numpy-添加操作大全
    高效编程之 concurrent.future
    linux命令 集合
    PostgreSQL-表空间
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/10539894.html
Copyright © 2011-2022 走看看