将本地图片(h5页面)映射到页面访问地址

1、application.yml中配置本地图片存储物理地址

###########################################################################################
#文件映射路径
imagesPath: file:/D:/img/picture/

一般项目中要将盘符去掉写出:file:/img/picture/

2.本地文件映射配置

//                         _ooOoo_  
//                        o8888888o  
//                        88" . "88  
//                        (| -_- |)  
//                         O\ = /O  
//                     ____/`---'\____  
//                   .   ' \\| |// `.  
//                    / \\||| : |||// \  
//                  / _||||| -:- |||||- \  
//                    | | \\\ - /// | |  
//                  | \_| ''\---/'' | |  
//                   \ .-\__ `-` ___/-. /  
//                ___`. .' /--.--\ `. . __  
//             ."" '< `.___\_<|>_/___.' >'"".  
//            | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//              \ \ `-. \_ __\ /__ _/ .-` / /  
//      ======`-.____`-.___\_____/___.-`____.-'======  
//                         `=---='  
//
//      .............................................  
//               佛祖保佑             永无BUG 
package com.example.demo.controller;

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.WebMvcConfigurationSupport;

/**
 * @version Revision 1.0.0
 * @创建日期:2020年3月2日 @功能说明: @begin
 * @修改记录:
 * @修改后版本 修改人 修改内容
 * @2020年3月2日 liuyu 创建
 * @end 本地映射配置
 */
@Configuration
public class FileConfig extends WebMvcConfigurationSupport {
    @Value("${imagesPath}")
    private String mImagesPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (mImagesPath.equals("") || mImagesPath.equals("${imagesPath}")) {
            String imagesPath = FileConfig.class.getClassLoader().getResource("").getPath();
            if (imagesPath.indexOf(".jar") > 0) {
                imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
            } else if (imagesPath.indexOf("classes") > 0) {
                imagesPath = "file:" + imagesPath.substring(0, imagesPath.indexOf("classes"));
            }
            imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"));
            mImagesPath = imagesPath;
        }
        System.out.println("imagesPath=" + mImagesPath);
        registry.addResourceHandler("/**").addResourceLocations(mImagesPath);
        super.addResourceHandlers(registry);
    }

}

3.本地文件路径结构

在这里插入图片描述

4.启动服务之后访问http://127.0.0.1:8080/chick.jpg

在这里插入图片描述转载链接:https://blog.csdn.net/xm526489770/article/details/83544217

原文是继承WebMvcConfigurerAdapter,但是这个类在新的spring版本中被弃用了,所以我改用WebMvcConfigurationSupport,当然也可以以实现的方式使用WebMvcConfigurer

@Configuration
public class FileConfig implements WebMvcConfigurer {
    @Value("${imagesPath}")
    private String mImagesPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (mImagesPath.equals("") || mImagesPath.equals("${imagesPath}")) {
            String imagesPath = FileConfig.class.getClassLoader().getResource("").getPath();
            if (imagesPath.indexOf(".jar") > 0) {
                imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
            } else if (imagesPath.indexOf("classes") > 0) {
                imagesPath = "file:" + imagesPath.substring(0, imagesPath.indexOf("classes"));
            }
            imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"));
            mImagesPath = imagesPath;
        }
        System.out.println("imagesPath=" + mImagesPath);
        registry.addResourceHandler("/**").addResourceLocations(mImagesPath);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

}

注意:继承WebMvcConfigurationSupport后自动配置不生效

demo下载:https://download.csdn.net/download/weixin_43085797/12376856

还可以用来生成二维码下载app

将apk文件放入文件夹中
在这里插入图片描述然后将地址放到草料二维码生成器生成二维码就行了
在这里插入图片描述然后就可以通过手机扫描二维码下载APP了(这种方法是不用经过应用市场下载的),前提是手机得跟电脑在同一个局域网中,IP地址互相能访问到
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值