读取远程服务器上的图片在页面上展示

开发环境

  • JDK:java8
  • IDE:IDEA
  • 项目框架:SpringBoot

项目配置

  1. application.yml配置
server:
  port: 6389
  ssl:
    key-store: .keystore
    key-store-type: JKS
    key-alias: alias
    key-store-password: xxxxxxxx
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/xxxxxxx?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: XXXXXX
    password: XXXXXX
#    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
logging:
  path: /opt/modules/logs/qrcode/qrcode.log
file:
  imageWidth: 80  #二维码logo的宽度
  imageHeight: 80  #二维码logo的高度
  QRCODE_SIZE: 400  #二维码尺寸
  logoImagePath: /opt/modules/qrcode/logo/logo.png  #二维码logo路径
#  logoImagePath: D:/data/qrcode/logo.png  #二维码logo路径
  qrFilePath: /opt/modules/qrcode/  #生成二维码路径
#  qrFilePath: D:/data/qrcode/  #生成二维码路径
  imagesPath: file:/opt/modules/qrcode/  # 二维码图片映射路径
#  imagesPath: file:D:/data/qrcode/  # 二维码图片映射路径
  imageReturnPath: /images/**
  1. 静态资源映射(图片)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Tomthy on 2018/11/21.
 */
@Configuration
public class WebMvcConf extends WebMvcConfigurerAdapter {

    @Value("${file.imageReturnPath}")
    private String imageReturnPath;
    @Value("${file.imagesPath}")
    private String imagesPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //imageReturnPath:映射前缀,imagesPath:图片映射路径
        registry.addResourceHandler(imageReturnPath).addResourceLocations(imagesPath);
    }
}
  1. 本地图片地址


    image.png
  2. url请求查询


    image.png

服务器与本地使用情况相同

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。