springboot 静态资源访问

springboot 静态资源访问以及文件上传

默认过滤有5个位置,

1.classpath-META-resources-静态资源

2.classpath-resources-静态资源

3.classpath-static-静态资源

4.classpath-public-静态资源

5.项目目录下,

第五个一般不考虑,

优先级如图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o2BTAT3l-1618483951030)(C:%5CUsers%5Cwuyuhong%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210414114538082.png)]

用 IntelliJ IDEA 创建Spring Boot项目,就会默认创建classpath: static

静态资源一般放 在这个目录下即可。

自定义过滤:

​ 可以在application.properties中直接定义过滤规则和静态资源位置。

spring.mvc.atatic-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/static/

过滤规则为/static/**,静态资源位置为classpath:/static/

也可以java实现

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    Override
    public void addArgumentResolvers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
 }
文件上传
@RestController
public class FileUploadController {
   
    @PostMapping("/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile file){
        if(file.isEmpty()){
            //文件不存在
            return "上传失败,请选择文件";

        }
        //获取文件名
        String fileName= file.getOriginalFilename();
        //保存路径
        String filePath="C:\\download/";
        File destFile=new File(filePath+fileName);
        try {
            //文件保存
            file.transferTo(destFile);
            return filePath+fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单文件上传</title>
</head>
<body>

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFile" value="选择文件">
    <input type="submit"value="上传">
</form>

</body>
</html>
多文件上传:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单文件上传</title>
</head>
<body>

<form action="/upload" method="post" enctype="multipart/form-data">
     <input type="file" name="uploadFile" value="选择文件" >
    <input type="file" name="uploadFile"  value="选择文件" >
    <input type="file" name="uploadFile" value="选择文件" >
    <input type="file" name="uploadFile" value="选择文件" >
    <input type="submit"value="上传">
</form>

</body>
</html>

Controller

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile[] file) throws IOException {
        //保存路径
        String filePath="C:\\download/";
        //如果上传文件不为空
        if(null!=file &&file.length >0){
            for (MultipartFile file2: file) {
                    //重复上传,文件名重复,
               String fileName=file2.getOriginalFilename();
                    if(fileName.length()>0) {
                        file2.transferTo(new File(filePath + UUID.randomUUID() + "-" + fileName));
                    }

            }
            return "上传成功";
        }
       else {
            return "上传失败";
        }
    }
}

文件上传的一些设置

#是否开启文件上传支持
spring.servlet.multipart.enabled=true
#写入磁盘的阈值
spring.servlt.multipart.file-size-threshold=0
#临时保存文件位置
spring.servlet.multipart.location=C:\\download/
#单个文件的最大大小
spring.servlet.multipart.max-file-size=1MB
#多文件上传时的总大小
spring.servlet.multipart.max-request-size=10MB
#文件是否延迟解析
spring.servlet.multipart.resolve-lazily=false

ing.servlet.multipart.max-file-size=1MB
#多文件上传时的总大小
spring.servlet.multipart.max-request-size=10MB
#文件是否延迟解析
spring.servlet.multipart.resolve-lazily=false


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值