如何从服务器链接访问本地驱动器文件
问题描述:
我正在试图在本地驱动器上保存图像的春季启动和存储数据库中的图像的链接,但我面临通过链接访问图像的问题。如何从服务器链接访问本地驱动器文件
中的文件存储在F:\ImageData\ReportImage\20171218
如何使用像localhost:8085/ImageData/ReportImage/20171218/sample.jpg
链接在本地驱动器接入图像,我也尝试application.property
设置server.servlet-path=F:\ImageData
和server.context-path=F:\ImageData
但问题没有解决允许。
如果我保存图像在src/main/resources/static/sample.jpg
可以很容易地访问,但我想从一些本地驱动器访问。
请有人帮我解决我的问题。
谢谢。
答
你可以尝试到资源处理程序添加到Spring配置:
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/images/**")
.addResourceLocations(new File("F:\ImageData").toURI().toString());
}
}
有了这个配置,你将能够获得通过URL http://<host>/images/<path>
图像,这将尝试通过路径从磁盘获取图像F:\ImageData\<path>
有关此可能性的更多信息,您可以找到here
感谢您的回复,但我正在休息不是mvc ..有什么方法可以在'WebSecurityConfigurerAdapter'或'appli中设置'addResourceHandlers' cation.property' –
试试这个代码,它会与其他人一起工作 – kurt
它只映射URL'/ images/**'到你的机器的文件夹中 – kurt