您的位置:首页 > 编程语言 > Java开发

SpringBoot设置虚拟路径映射绝对路径

2018-01-30 11:47 621 查看
上传图片到本地路径,得到的是一个绝对路径例如:D:\picpath\O48681516429132485.png

但是前台需要的数据是这样的 :http://localhost:8082/image/O48681516429132485.png

那么就要设置虚拟路径 /image/ = D:\picpath\ 了,下面我们就来代码实现下,作为一个负责任

的程序员,我把包也给你们复制过来了。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
* 图片绝对地址与虚拟地址映射
*/

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

//文件磁盘图片url 映射
//配置server虚拟路径,handler为前台访问的目录,locations为files相对应的本地路径
registry.addResourceHandler("/image/**").addResourceLocations("D:\\picpath\\");
}

}
是不是很简单呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: