菜鸟之路Day38一一Web开发综合案例(三)
时间:2025.6.1
作者:blue
文章内容学习自黑马程序员BV1m84y1w7Tb
1.文件上传
1.1内容介绍
文件上传,是指将本地图片、视频、音频等文件上传到服务器,供其他用户浏览或下载的过程。
文件上传在项目中应用非常广泛,我们经常发微博、发微信朋友圈都用到了文件上传功能。
文件上传前端三要素
前端与后端数据交互
1.2阿里云OSS
阿里云对象存储0ss(0bject storage Service),是一款海量、安全、低成本、高可靠的云存储服务。使用OSS,您可以通过网络随时存储和调用包括文本、图片、音频和视频等在内的各种文件。
准备流程
Bucket:存储空间是用户用于存储对象(Object,就是文件)的容器,所有的对象都必须隶属于某个存储空间
SDK:软件开发工具包,包括辅助软件开发的依赖(jar包),代码示例等,都可以叫做SDK
开通对象存储服务OSS
创建bucket
获取AccessKey(密钥)
参照官方SDK编写入门程序
pom文件中引入相关依赖
<!--阿里云OSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.4</version>
</dependency>
<!--Java9以上-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
1.3阿里云OSS集成
UploadController
@RestController
public class UploadController {
@Autowired
private AliOSSUtils aliOSSUtils;
@PostMapping("/upload")
public Result upload(MultipartFile image) throws IOException {
//调用阿里云OSS工具类,将上传上来的文件存入阿里云
String url = aliOSSUtils.upload(image);
//将图片上传后的url返回,用于浏览器回显展示
return Result.success(url);
}
}
如果在此图片没有显示,可能是访问权限的问题,将阻止公共访问关闭,然后将读写权限修改为公共读
2.修改员工
很明显修改员工着部分操作是分两步进行的,首先要将员工的信息查询出来,回显到页面上,然后再对员工的信息进行修改
2.1查询回显
Controller
@GetMapping("/emps/{id}")
public Result getById(@PathVariable("id") Integer id) {
log.info("根据id查询员工信息");
Emp emp = empService.getById(id);
return Result.success(emp);
}
EmpService
@Override
public Emp getById(Integer id) {
Emp emp = empMapper.getById(id);
return emp;
}
Mapper
@Select("select * from emp where id = #{id}")
Emp getById(Integer id);
2.2修改员工
Controller
@PutMapping("/emps")
public Result update(@RequestBody Emp emp) {
log.info("修改员工信息");
empService.update(emp);
return Result.success();
}
EmpService
@Override
public void update(Emp emp) {
empMapper.update(emp);
}
Mapper
<!--更新员工-->
<update id="update">
update emp
<set>
<if test="username != null and username != ''" >
username=#{
username},
</if>
<if test="password != null and password != ''">
password=#{
password},
</if>
<if test="name != null and username != ''">
name=#{
name},
</if>
<if test="gender != null">
gender=#{
gender},
</if>
<if test="image != null">
image=#{
image},
</if>
<if test="job != null">
job = #{
job},
</if>
<if test="entryDate != null">
entry_date=#{
entryDate},
</if>
<if test="updateTime != null">
update_time=#{
updateTime},
</if>
<if test="deptId != null">
dept_id=#{
deptId}
</if>
</set>
where id=#{
id}
</update>
3.配置文件
3.1参数配置化
我们可以将springboot项目中原本硬编码的参数的值,抽取到配置文件中,然后再通过@Value注解进行映射,这样就解决了参数硬编码的问题
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.bucketName}")
private String bucketName;
3.2yml配置文件
基本语法
大小写敏感
数值前边必须有空格,作为分隔符
使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格(idea中会自动将Tab转换为空格)缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
表示注释,从这个字符一直到行尾,都会被解析器忽略
yml数据格式
对象/Map集合:
user:
name: zhangsan
age: 18
password: 123456
数组/List/Set集合:
hobby:
- java
- game
- sport
3.3@ConfigurationProperties与@Value
这两个注解的相同点是:都是用来注入外部配置的属性的
这两个注解的不同点是:
@Value注解只能一个一个的进行外部属性的注入
@ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中
原本我们的代码,用@Value进行外部属性注入十分的繁琐,我们可以将这些变量抽象到一个类中,然后在类中使用@ConfigurationProperties注解批量注入,再将类交由IOC容器管理,然后再在原来的工具类中,注入我们自定义类的bean对象,达到解耦的效果。
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.bucketName}")
private String bucketName;
抽象到实体类
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
工具类中依赖注入
@Autowired
AliOSSProperties Aliossproperties;
获取bean对象中的值
public String upload(MultipartFile file) throws IOException {
//获取bean中的变量
String endpoint = Aliossproperties.getEndpoint();
String bucketName = Aliossproperties.getBucketName();
String accessKeyId = Aliossproperties.getAccessKeyId();
String accessKeySecret = Aliossproperties.getAccessKeySecret();
......
}