springboot静态资源访问,和⽂件上传,以及路径问题springboot 静态资源访问:
这是springboot 默认的静态资源访问路径访问顺序依次从前到后(localhost:8080/bb.jpg)
inputtypefile不上传文件
⾃定义静态资源访问路径(localhost:8080/bb.jpg)
# 静态⽂件请求匹配⽅式(只要是请求路径配到到了就访问下⾯配置的默认静态资源路径)
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源⽬录多个使⽤逗号分隔
//⾃定义不在项⽬下的路径(⽐如: c:/upload2)  通过localhost:8080/bb.jpg 也能访问记得加配置
# 静态⽂件请求匹配⽅式(只要是请求路径配到到了就访问下⾯配置的默认静态资源路径)
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源⽬录多个使⽤逗号分隔
INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/upload/,classpath:/ c:/upload2
springboot  实现多⽂件上传
对于上传路径问题可以通过上⾯讲的⾃定义路径来进⾏配置:下载到电脑的某个位置然后进⾏访问和上⾯的配置⼀模⼀样只是classpath=>file web.upload-path=/Users/jack/Desktop
下⾯贴代码:(⽂件下载到tomcate下)
html:
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
⽂件:<input type="file" name="head_img"/>
姓名:<input type="text" name="name"/>
<input type="submit" value="上传"/>
</form>
</body>
下载⼯具类:
/**
* 提取上传⽅法为公共⽅法
* @param uploadDir 上传⽂件⽬录
* @param file 上传对象
* @throws Exception
*/
private void executeUpload(String uploadDir,MultipartFile file) throws Exception
{
//⽂件后缀名
String suffix = OriginalFilename().OriginalFilename().lastIndexOf("."));
//上传⽂件名
String filename = UUID.randomUUID() + suffix;
/
/服务器端保存的⽂件对象
File serverFile = new File(uploadDir + filename);
//将上传的⽂件写⼊到服务器端⽂件内
}
controller:
@RequestMapping(value = "/uploads",method = RequestMethod.POST)
public @ResponseBody String uploads(HttpServletRequest request,MultipartFile[] file)
{
try {
//上传⽬录地址
/
/ 随意  String uploadDir = C:/img/;
String URL("classpath:").getPath()+"/static/up/";
System.out.println(uploadDir);
//如果⽬录不存在,⾃动创建⽂件夹
File dir = new File(uploadDir);
if(!ists())
{
dir.mkdir();
}
//遍历⽂件数组执⾏上传
for (int i =0;i<file.length;i++) {
if(file[i] != null) {
//调⽤上传⽅法
executeUpload(uploadDir, file[i]);
}
}
}catch (Exception e)
{
//打印错误堆栈信息
e.printStackTrace();
return "上传失败";
}
return "上传成功";
}
然后⽂件下载路径就到了tomcate 下。
需要配置
web.upload-path=/C:/img/