SpringBoot2.0入门(详细文档)二

文章目录

http其他请求

  • POST/uri 创建
  • DELETE/uri/xxx 删除
  • PUT/uri/xxx 更新或者创建
  • GET/uri/xxx 查看

看似地址一样其实请求方式不一样

SpringBoot2.x目录文件结构

简介:讲解SpringBoot目录文件结构和官网推荐的目录规范

  1. 目录结构
    src/main/java:存放代码
    ser/main/resources
    static:存放静态文件,比如css,js,image,(访问方式http://localhost:8080/js/main.js)
    templates:存储静态模板页面jsp,html,tpl
    config:存放配置文件,application.properties
    resources:
  2. 引入依赖Thymeleaf,如果要访问templates文件夹下的资源,需要引入
    以下依赖,进行页面的转发方式访问
java 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

注意:如果不引入这个依赖包,html文件应该放在默认加载文件夹里面

比如resources,static,public这几个文件夹,才可以访问

  1. 同个文件的加载顺序,静态资源文件
    在SpringBoot中默认情况下,一共有5个位置可以:
    1. classpath:/META-INF/resources/
    2. classpath:/resources/
    3. classpath:/static/
    4. classpath:/public/
    5. /

SpringBoot默认会挨个从

META/resources>resources>static>public 里面找是否存在相应的资源,如果有则直接返回

所以访问这些目录下的资源时,在路径中可以体现上面的几个路径

  1. 默认配置
    1. 官网地址:https://springdoc.cn/spring-boot-application-configuration-tutorial/
    2. 自己配置
      1. 在resources目录下创建,application.properties
      2. 重写静态资源加载: spring.resources.static-locations = classpath:/META-
        INF/resources/,classpath:/resources/,classpath:/staticl,classpath:/public/
  2. 静态资源文件存储在CDN,他可以加快静态资源的访问速度。
    在大型的公司中可能会实现静态资源和java代码的分离。把静态资源的加载放到另外一个工程中使用。

SpringBoot2.x文件上传实战

简介:讲解HTML页面文件上传和后端处理实战

  1. 讲解SpringBoot文件上传MultipartFile file,源自SpringMVC
    1. 静态页面直接,不需要进行controller转发:localhost:8080/index.html
      注意点:
      如果想要直接访问html页面,则需要把html放在SpringBoot默认加载的文件夹下面
    2. MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用的FileOutStream方便和高效)
      a. 在static下面创建一个upload.html,页面内容如下
java 复制代码
<form action="/upload" method="post"  enctype="multipart/form-data" >
    文件:<input type="file"  name="head_img">
    姓名:<input name="username" >
    <input type="submit" value="上传">
</form>
复制代码
		b. 控制类中的代码如下
		upload.html中的文件name要和@RequestParam("head_img")一致
JAVA 复制代码
@RequestMapping("/upload")
@ResponseBody
public Object upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest req){
    // 动态获取文件的真实路径,用于保存上传的文件
    String filepath =  "D:/test/";
    System.out.println(filepath);
    // 获取普通表单字段
    String username =  req.getParameter("username");
    System.out.println("用户名:"+username);

    // 获取文件的全名
    String filename =  file.getOriginalFilename();
    System.out.println("文件全名:"+filename);

    String suffixName =  filename.substring(filename.indexOf("."));
    System.out.println("后缀名:"+suffixName);

    filename = UUID.randomUUID() +  suffixName;
    File newFile = new  File(filepath+filename);
    try {
        file.transferTo(newFile);
    } catch (IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return filename;

}
复制代码
c.SpringBoot的默认上传文件大小为1M,如果要上传大于1M的文件需要在application.properties文件中设置:
java 复制代码
#单个文件大小和单次请求文件大小
spring.servlet.multipart.max-file-size=10Mb
spring.servlet.multipart.max-request-size=100Mb

文件上传和访问需要指定磁盘路径

这时候的文件是无法上传成功的:因为我们无法在jar中上传和访问一文件

  1. 解决方法如下:
    1. application.properties中增加下面配置
java 复制代码
#声明一个要上传的文件路径
web.images-path=D:/test
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.images-path}
相关推荐
直奔標竿2 小时前
Java开发者AI转型第二十五课!Spring AI 个人知识库实战(四)——RAG来源追溯落地,拒绝AI幻觉
java·开发语言·人工智能·spring boot·后端·spring
敖正炀3 小时前
WebFlux 深度:Reactor 线程模型、背压与错误处理
spring boot
BING_Algorithm3 小时前
一文搞定 AOP 所有核心知识点
spring boot·后端·spring
勿忘初心12214 小时前
【Java实战】SpringBoot 集成 freemarker 导出 Word 模板
java·spring boot·freemarker·模板引擎·word导出·后端实战
绿草在线4 小时前
SpringBoot项目实战:从零搭建高效开发环境
java·spring boot·后端
空中海6 小时前
Spring Boot Kafka 项目 Demo:订单事件系统 专家知识、源码阅读路线与面试题
spring boot·kafka·linq
默 语1 天前
基于 Spring Boot 3 + LangChain4j 快速构建企业级 AI 应用实战
人工智能·spring boot·后端
薪火铺子1 天前
SpringBoot WebServer启动与监听器原理深度解析
spring boot·后端·tomcat
KmSH8umpK1 天前
SpringBoot 分布式锁实战:从单机锁到Redis分布式锁全覆盖,解决超卖、重复下单、幂等并发问题
spring boot·redis·分布式
jay神1 天前
基于团队模式的C程序设计课程辅助教学管理系统
java·spring boot·vue·web开发·管理系统