首页 > 编程语言 > 详细

springBoot 连接打包成jar包运行时,获取图片上传文件、前端调用图片显示

时间:2019-09-27 12:12:23      阅读:430      评论:0      收藏:0      [点我收藏+]

配置文件
在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

springBoot 连接打包成jar包运行时,获取图片上传文件、前端调用图片显示

原文:https://www.cnblogs.com/liabin/p/11596944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!