package com.example.config;
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.WebMvcConfigurer;
//运行时加载
@Configuration
public class UploadFilePathConfig implements WebMvcConfigurer {
@Value("${file.staticAccessPath}")//这里是把我们的配置文件中的值注入进去
private String staticAccessPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
//映射的作用:物理地址与逻辑地址的映射
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+uploadFolder);
}
}
前端:
upload(e){
//获取文件对象
console.log(e)
let file=e.target.files[0];
//创建表单对象
let form=new FormData();
form.append('file',file);
axios.post('http://localhost:8085/file/upload',form,{
headers:{'Content-Type':'multipart/form-data'}
}).then((res)=>{
console.log(file.name);
this.url='/image/'+file.name;
this.tu='/image/'+file.name;
this.x='/image/'+file.name;
alert(res.data);
})
}