赞
踩
目录
方式2:@ConfigurationProperties方式
输入员工信息(暂不考虑头像),点击“保存”按钮时,要提交到服务端,把员工信息保存到数据库
分析:
EmpController:
- @PostMapping
- public Result addEmp(@RequestBody Emp emp){
- empService.addEmp(emp);
- return Result.success();
- }
EmpService:
void addEmp(Emp emp);
EmpServiceImpl:
- @Override
- public void addEmp(Emp emp) {
- emp.setCreateTime(LocalDateTime.now());
- emp.setUpdateTime(LocalDateTime.now());
- empMapper.insert(emp);
- }
EmpMapper:
- @Insert("INSERT INTO emp (username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
- "VALUES (#{username}, #{password}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
- void insert(Emp emp);
文件上传,是指将本地图片、视频、音频等文件上传到服务器,供其他用户浏览或下载的过程
文件上传在项目中应用非常广泛,我们经常发微博、发微信朋友圈都用到了文件上传功能
客户端要把文件上传到服务端,需要满足以下三要素,否则仅仅是提交文件名,而不会提交文件内容:
使用表单方式提交,且表单提交方式是POST
表单的enctype
属性要设置为multipart/form-data
表单里要有文件选择框,且文件选择框必须有name属性:<input type="file" name="表单参数名">
示例:在工程的resources下的static目录里创建文件demo.html
,内容如下
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>文件上传-示例</title>
- </head>
- <body>
- <!--
- 1. 使用表单方式提交数据,且表单提交方式为post
- 2. 表单form标签的enctype属性要设置为multipart/form-data
- -->
- <form action="/file/upload" method="post" enctype="multipart/form-data">
- <input type="text" name="name"><br>
- <!-- 3. 表单里要有文件选择框,且必须有name属性 -->
- <input type="file" name="avatar"><br>
- <button>提交</button>
- </form>
- </body>
- </html>

在Controller里接收文件数据:
需要使用MultipartFile
类型的参数,MultipartFile
对象的常用方法:
getOriginalFilename()
:获取原始文件名(客户端上传的文件名称)
getSize()
:获取文件的大小,单位是byte字节
getInputStream()
:获取字节输入流对象,用于获取文件数据
transferTo(File dest)
:把文件数据存储到本地磁盘上
默认允许上传的文件大小为1M,可以通过修改配置文件来调整限制
服务端接收文件,代码示例:
- package com.itheima.controller;
-
- import com.itheima.pojo.Result;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
-
- @Slf4j
- @RestController
- @RequestMapping("/file")
- public class FileController {
-
- /**
- * 客户端提交了表单数据,一个是name,一个是avatar
- * 在方法上直接加形参,可以接收表单数据:
- * name是字符串,使用String类型
- * avatar是文件,使用MultipartFile类型
- */
- @PostMapping("/upload")
- public Result upload(String name, MultipartFile avatar){
-
- return Result.success();
- }
- }

修改配置文件application.properties或者yaml或yml
- #设置:一次请求最大允许提交20MB。默认10MB
- spring.servlet.multipart.max-request-size=20MB
- #设置:一个文件最大允许10MB。默认1MB
- spring.servlet.multipart.max-file-size=10MB
所谓本地存储文件,是客户端把文件上传到服务器之后,服务器把文件直接存储到自己的磁盘里。
好处:简单
缺点:
本地磁盘空间有限,不能存储太多文件;且不方便扩容
本地存储文件,没有备份,一旦磁盘出现问题,就会丢失文件
维护管理不方便
示例代码:
- @Slf4j
- @RestController
- @RequestMapping("/file")
- public class FileController {
-
- @PostMapping("/upload")
- public Result upload(String name, MultipartFile avatar) throws IOException {
- log.info("name={}", name);
- //重命名文件
- // 1. 获取原始文件名。比如:100.jpg
- String filename = avatar.getOriginalFilename();
- // 2. 截取得到后缀名。比如:.jpg
- String suffix = filename.substring(filename.lastIndexOf("."));
- // 3. 重新生成新的文件名。比如:2ceea81e-2a4d-4709-8332-367ea4e34e23.jpg
- filename = UUID.randomUUID().toString() + suffix;
- //保存文件到本地磁盘
- avatar.transferTo(Paths.get("E:\\" + filename));
- log.info("文件已保存到:E:/{}", filename);
- return Result.success();
- }
- }

阿里云对象存储 OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,提供最高可达 99.995 % 的服务可用性。多种存储类型供选择,全面优化存储成本。
(1)打开https://www.aliyun.com/ ,申请阿里云账号并完成实名认证。
(2)充值 (可以不用做)
(3)开通OSS
登录阿里云官网。 点击右上角的控制台。
将鼠标移至产品,找到并单击对象存储OSS,打开OSS产品详情页面。在OSS产品详情页中的单击立即开通。
开通服务后,在OSS产品详情页面单击管理控制台直接进入OSS管理控制台界面。您也可以单击位于官网首页右上方菜单栏的控制台,进入阿里云管理控制台首页,然后单击左侧的对象存储OSS菜单进入OSS管理控制台界面。
(4)创建存储空间
新建Bucket,命名为 hmleadnews ,读写权限为 ==公共读==(不然别人没办法上传)
3. OSS快速入门
(1)创建测试工程,引入依赖
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-sdk-oss</artifactId>
- <version>3.15.1</version>
- </dependency>
(2)新建类和main方法
- import org.junit.jupiter.api.Test;
- import com.aliyun.oss.ClientException;
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import com.aliyun.oss.OSSException;
- import java.io.FileInputStream;
- import java.io.InputStream;
-
- public class AliOssTest {
-
- @Test
- public void testOss(){
- // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
- String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
- // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
- String accessKeyId = "---------------------";
- String accessKeySecret = "-----------------------";
- // 填写Bucket名称,例如examplebucket。
- String bucketName = "-----------";
- // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
- String objectName = "0001.jpg";
- // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
- // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
- String filePath= "C:\\Users\\Administrator\\Pictures\\Saved Pictures\\10.jpg";
-
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
-
- try {
- InputStream inputStream = new FileInputStream(filePath);
- // 创建PutObject请求。
- ossClient.putObject(bucketName, objectName, inputStream);
- } catch (OSSException oe) {
- System.out.println("Caught an OSSException, which means your request made it to OSS, "
- + "but was rejected with an error response for some reason.");
- System.out.println("Error Message:" + oe.getErrorMessage());
- System.out.println("Error Code:" + oe.getErrorCode());
- System.out.println("Request ID:" + oe.getRequestId());
- System.out.println("Host ID:" + oe.getHostId());
- } catch (Exception ce) {
- System.out.println("Caught an ClientException, which means the client encountered "
- + "a serious internal problem while trying to communicate with OSS, "
- + "such as not being able to access the network.");
- System.out.println("Error Message:" + ce.getMessage());
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- }

4. 获取AccessKeyId
阿里云OSS文档:OSS Java SDK 兼容性和示例代码_对象存储(OSS)-阿里云帮助中心
阿里云SDK介绍:安装OSS Java SDK_对象存储(OSS)-阿里云帮助中心
上传文件示例代码:如何使用JavaSDK简单上传文件_对象存储(OSS)-阿里云帮助中心
在类上添加@Component
修改配置参数endpoind、accessKeyId、accessKeySecret、bucket名称等,改成自己的
如果要上传文件时:
只要使用@Autowired
注入AliOSSUtils对象,调用它的upload方法就可以了
工具类:
- package com.itheima.util;
-
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.util.UUID;
-
- /**
- * 阿里云 OSS 工具类
- */
- @Component
- public class AliOSSUtils {
-
- @Value("${aliyun.endpoint}")
- private String endpoint;
- @Value("${aliyun.accessKeyId}")
- private String accessKeyId;
- @Value("${aliyun.accessKeySecret}")
- private String accessKeySecret;
- @Value("${aliyun.bucketName}")
- private String bucketName;
-
- /**
- * 实现上传图片到OSS
- */
- public String upload(MultipartFile file) throws IOException {
- // 获取上传的文件的输入流
- InputStream inputStream = file.getInputStream();
-
- // 避免文件覆盖
- String originalFilename = file.getOriginalFilename();
- String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
-
- //上传文件到 OSS
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- ossClient.putObject(bucketName, fileName, inputStream);
-
- //文件访问路径
- String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
- // 关闭ossClient
- ossClient.shutdown();
- return url;// 把上传到oss的路径返回
- }
- }

controller:
- package com.itheima.controller;
-
- import com.itheima.pojo.Result;
- import com.itheima.util.AliOSSUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
- import java.io.IOException;
-
-
- @Slf4j
- @RestController
- public class UploadController {
- @Autowired
- private AliOSSUtils ossUtils;
-
- @PostMapping("/upload")
- public Result upload(MultipartFile image) throws IOException {
- String url = ossUtils.upload(image);
- return Result.success(url);
- }
- }

EmpController:
- @GetMapping("/{id}")
- public Result queryEmpById(@PathVariable("id") Integer id){
- Emp emp = empService.queryEmpById(id);
- return Result.success(emp);
- }
EmpService:
Emp queryEmpById(Integer id);
EmpServiceImpl:
- @Override
- public Emp queryEmpById(Integer id) {
- return empMapper.selectById(id);
- }
EmpMapper:
- @Select("select * from emp where id = #{id}")
- Emp selectById(Integer id);
EmpController:
- @PutMapping
- public Result updateEmp(@RequestBody Emp emp){
- empService.updateEmpById(emp);
- return Result.success();
- }
EmpService:
void updateEmpById(Emp emp);
EmpServiceImpl:
- @Override
- public void updateEmpById(Emp emp) {
- empMapper.updateById(emp);
- }
EmpMapper:
void updateById(Emp emp);
EmpMapper.xml:
- <update id="updateById">
- UPDATE emp
- <set>
- <if test="username!=null and username.length()>0">username = #{username},</if>
- <if test="password!=null and password.length()>0">password = #{password},</if>
- <if test="name!=null and name.length()>0">name = #{name},</if>
- <if test="gender!=null">gender = #{gender},</if>
- <if test="image!=null and image.length()>0">image = #{image},</if>
- <if test="job!=null">job = #{job},</if>
- <if test="entrydate!=null">entrydate = #{entrydate},</if>
- <if test="deptId!=null">dept_id = #{deptId},</if>
- <if test="updateTime!=null">update_time = #{updateTime}</if>
- </set>
- WHERE id = #{id};
- </update>
SpringBoot工程支持的配置文件格式有:
application.properties
优点:格式简单,key=value
,所有配置参数都是平级的关系
缺点:不方便表示不同层级的配置参数
application.yml
或者application.yaml
优点:格式简单,key: value
,还能表示配置参数之间的层级
无论是哪种配置文件,都可以用于配置参数。包括SpringBoot本身的参数,或者是自定义的参数。而SpringBoot工程会自动读取配置文件,我们可以直接获取配置文件里的参数值
使用properties配置参数
- aliyun.endpoint=https://oss-cn-beijing.aliyuncs.com
- aliyun.accessKeyId=LTAI5tG3TbA9HLs22KEtimyB
- aliyun.accessKeySecret=4avUxhaO5KCTl5pqpta3AdU98mT9um
- aliyun.bucketName=itheima-liuyp
获取参数值
无论是在application.properties里,还是在application.yaml里配置了参数,如果我们的程序代码中需要获取这些参数该怎么做呢?
首先:并不需要我们加载配置文件,SpringBoot会帮我们加载配置文件,把所有配置参数存储起来
然后:我们需要什么参数,直接使用注解注入参数值即可。常用两种方式
@Value方式
@ConfigurationProperties方式
@Value
方式作用:在==任意bean对象==里,都可以使用@Value
,注入某个参数值
用法:在bean对象里增加成员变量,在成员变量上增加注解@Value("${参数名}")
使用:如果只需要获取少量几个参数值,就使用@Value("${参数名}")
;如果要读取大量参数,建议使用方式2
- package com.itheima.controller;
-
- import com.itheima.pojo.Result;
- import com.itheima.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
-
- @RestController
- public class DemoConfigController {
-
- /** 把“AA”直接注入赋值给变量aa */
- @Value("AA")
- private String aa;
-
- /** 取名称为“aliyun.endpoint”的参数值,注入赋值给当前成员变量endpoint */
- @Value("${aliyun.endpoint}")
- private String endpoint;
-
- /**取名称为“aliyun.accessKeyId”的参数值,注入赋值给当前成员变量*/
- @Value("${aliyun.accessKeyId}")
- private String accessKey;
-
- @GetMapping("/config1")
- public Result config1(){
- return Result.success(accessKey);
- }
- }

@ConfigurationProperties
方式作用:读取一批配置参数,封装到一个JavaBean对象里。当需要使用参数值的时候,只要得到这个JavaBean对象即可
用法:
准备一个JavaBean类,用于封装一批参数值
类上加@ConfigurationProperties + @Component
使用时用@Autowired注入这个JavaBean对象
优点:可以一次性读取一批配置参数值,使用时让代码更简洁
示例:
- package com.itheima.pojo;
-
- import lombok.Data;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- /**
- * 1. 类里要有private成员变量,@Data提供get和set方法
- * 2. 类上要加@ConfigurationProperties(prefix="配置参数名的前缀")
- * prefix + 成员变量名:要对应一个配置参数名
- * 3. 类上加@Component,让Spring帮我们生成类的对象放到容器里
- */
- @Data
- @Component
- @ConfigurationProperties(prefix = "aliyun")
- public class AliyunProperties {
- private String endpoint;
- private String accessKeyId;
- private String accessKeySecret;
- private String bucketName;
- }

使用时:
- package com.itheima.controller;
-
- import com.itheima.pojo.AliyunProperties;
- import com.itheima.pojo.Result;
- import com.itheima.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
-
- @RestController
- public class DemoConfigController {
-
- @Autowired
- private AliyunProperties aliyunProperties;
-
- @GetMapping("/config1")
- public Result config1(){
- return Result.success(aliyunProperties);
- }
- }

- @RestController
- @RequestMapping("/books")
- public class BookController{
- @Autowired
- private Environment env;
-
- @GetMapping("/{id}")
- public String getById(@PathVariable Integer id){
- sout(env.getProperty("lesson"));
- sout(env.getPeoperty("enterprise.name"));
- sout(env.getProperty("enterprise.subject[0]"));
- return "hello ,spring boot! ";
-
- }
- }
YAML(/ˈjæməl/
,尾音类似camel骆驼) 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写。它是一个可读性高,用来表达数据序列化的格式。
YAML可以简单表达清单、散列表,标量等数据形态。它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲。
YAML 的配置文件后缀为 .yml
或者.yaml
使用key和value配置参数值,key和value之间要有 :空格
进行分隔。如果使用:
分隔,是语法错误
相同层级的key-value,前边要有相同数量的空格。几个空格无所谓,但是必须有相同数量的空格
yaml的高级语法,可以让我们配置数组或集合、Map结构的数据,语法如下:
纯量:不可再拆分的基本值。包括:字符串,布尔,整数,浮点数,null,时间,日期
数组:以-空格
开头再跟上值,表示数组里的一个元素
对象:使用key: value
格式形式,注意冒号后边有一个空格
- username: zhangsan
- user:
- name: 张三 #相当于properties里user.name=张三
- gender: 男 #相当于properties里user.gender=男
- age: 20 #相当于properties里user.age=20
- girls: #girls是一个集合或数组,集合里现在有3个元素值
- - 小丽
- - 小美
- - 小坤
- info:
- email: zhangsan@163.com
- phone: 13800138000
和刚刚读取properties文件里的参数,是完全相同的
@Value("${参数名}")
,读取某一个参数
@ConfigurationPropperties
把一批参数封装成一个JavaBean对象
封装全部数据到Environment对象(一般用在框架里)
- aliyun:
- endpoint: https://oss-cn-beijing.aliyuncs.com
- accessKeyId: LTAI5tG3TbA9HLs22KEtimyB
- accessKeySecret: 4avUxhaO5KCTl5pqpta3AdU98mT9um
- bucketName: itheima-liuyp
- package com.itheima.util;
-
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.util.UUID;
-
- /**
- * 阿里云 OSS 工具类
- */
- @Component
- public class AliOSSUtils {
- @Value("${aliyun.endpoint}")
- private String endpoint;
- @Value("${aliyun.accessKeyId}")
- private String accessKeyId;
- @Value("${aliyun.accessKeySecret}")
- private String accessKeySecret;
- @Value("${aliyun.bucketName}")
- private String bucketName;
-
- /**
- * 实现上传图片到OSS
- */
- public String upload(MultipartFile file) throws IOException {
- // 获取上传的文件的输入流
- InputStream inputStream = file.getInputStream();
-
- // 避免文件覆盖
- String originalFilename = file.getOriginalFilename();
- String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
-
- //上传文件到 OSS
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- ossClient.putObject(bucketName, fileName, inputStream);
-
- //文件访问路径
- String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
- // 关闭ossClient
- ossClient.shutdown();
- return url;// 把上传到oss的路径返回
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。