简介
服务端接收上传的目的是提供文件的访问服务,那么对于SpringBoot而言,可以提供文件访问的静态资源目录:
classpath:/META-INF/resources/
,classpath:/static/
,classpath:/public/
,classpath:/resources/
上传目录自定义配置
Spring boot 为我们提供了使用spring.web.resources.static-locations
配置自定义静态文件的位置。
web:
upload-path: F:\\images
server:
#端口号
port: 8888
spring:
web:
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${
web.upload-path}
- 配置
web.upload-path
为与项目代码分离的静态资源路径,即:文件上传保存根路径 - 配置
spring.web.resources.static-locations
,除了带上Spring Boot默认的静态资源路径之外,加上file:${web.upload-path}
指向外部的文件资源上传路径。该路径下的静态资源可以直接对外提供HTTP访问服务。即,在F:\\images
文件下的资源都可以通过路径直接打开。如:有一个1.jpg
图片在浏览器中直接可以访问http://localhost:8888/1.jpg
看到图片
文件上传实现
前端
<body>
<h2>单文件上传</h2>
<form action="http://127.0.0.1:8888/fileUpload/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="">
<input type="submit" value="上传">
</form>
<h2>多文件上传</h2>
<