码迷,mamicode.com
首页 > 编程语言 > 详细

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

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

标签:write   tar   private   this   puts   src   cep   sources   打包   

配置文件
在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包运行时,获取图片上传文件、前端调用图片显示

标签:write   tar   private   this   puts   src   cep   sources   打包   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!