springBoot 连接打包成jar包运行时,获取图片上传文件、前端调用图片显示
配置文件
在application.properties中进行配置
web.upload-path=d:/myfile/upload
web.front-path=d:/myfile/front
spring.resources.static-locations=file:${web.upload-path},file:${web.front-path}
application.yml配置方式
web:
upload-path: d:/myfile/upload
front-path: d:/myfile/front
spring:
resources:
static-locations: file:${web.upload-path},file:${web.front-path}
上传代码封装
上传部分代码
@Value("${web.upload-path}")
private String filePath; //获取上传地址
String filePath=this.filePath+"/test/";
try {
this.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
System.out.println("文件上传失败");
}
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
//PrintUtil.println("filePath="+filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
盘符图片地址 d:/myfile/upload/test/fileName
前段网页地址 src="test/fileName" http://127.0.0.1/test/fileName