org.springframework.boot
spring-boot-starter-thymeleaf
com.aliyun.oss
aliyun-sdk-oss
2.8.3
commons-fileupload
commons-fileupload
1.3.1
#### **3. 添加配置信息**
我们可以在阿里云后台的如下位置,查看自己阿里云账号的AccessKey信息。

在这里复制自己的AccessKey ID与Secret信息,然后粘贴到自己的配置文件中。

请在application.properties或application.yml文件中添加如下格式的配置信息,重要的是把自己的AccessKey ID与Secret信息粘贴进来。
bucketName: “yiyige”
accessKeyId: “自己阿里云的accessKey”
accessKeySecret: “自己阿里云的accessKey”
#OSS对应的区域
endpoint: “http://oss-cn-hangzhou.aliyuncs.com”
filehost: “images”
#### **4. 创建配置信息类**
为了方便调用,我们封装一个配置信息类,后面就可以直接通过Get/Set方法来调用这些配置信息了。
package com.yyg.boot.config;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
- 把配置文件中的配置信息读取到该类中.
*/
@Data
@Configuration
public class OssConfiguration {
@Value(“${endpoint}”)
private String endPoint;
@Value(“${accessKeyId}”)
private String accessKeyId;
@Value(“${accessKeySecret}”)
private String accessKeySecret;
@Value(“${filehost}”)
private String fileHost;
@Value(“${bucketName}”)
private String bucketName;
}
#### **5. 封装阿里云文件上传工具类**
然后我这里封装了一个文件上传工具类,用于实现文件上传功能。
package com.yyg.boot.util;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.yyg.boot.config.OssConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
- 封装文件上传方法
*/
@Component
public class AliyunOssUtil {
@Autowired
private OssConfiguration config;
public String upload(File file) {
if (file == null) {
return null;
}
String endPoint = config.getEndPoint();
String keyId = config.getAccessKeyId();
String keySecret = config.getAccessKeySecret();
String bucketName = config.getBucketName();
String fileHost = config.getFileHost();
//定义子文件的格式
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
String dateStr = format.format(new Date());
//阿里云文件上传客户端
OSSClient client = new OSSClient(endPoint, keyId, keySecret);
try {
//判断桶是否存在
if (!client.doesBucketExist(bucketName)) {
//创建桶
client.createBucket(bucketName);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
//设置访问权限为公共读
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//发起创建桶的请求
client.createBucket(createBucketRequest);
}
//当桶存在时,进行文件上传
//设置文件路径和名称
String fileUrl = fileHost + “/” + (dateStr + “/” + UUID.randomUUID().toString().replace(“-”, “”) + “-” + file.getName());
PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl, file));
client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
//文件上传成功后,返回当前文件的路径
if (result != null) {
return fileUrl;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
client.shutdown();
}
}
return null;
}
}
#### **6. 编写Controller接口**
接着再创建一个Controller文件上传接口,上传我们的文件到阿里云中。
package com.yyg.boot.web;
import com.yyg.boot.util.AliyunOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
@Controller
public class OssController {
@Autowired
private AliyunOssUtil ossUtil;
@GetMapping(“/”)
public String showUploadFile() {
return “upLoad”;
}
@PostMapping(“/uploadFile”)
public String upload(@RequestParam(“file”) MultipartFile file) {
try {
if (file != null) {
String fileName = file.getOriginalFilename();
if (!“”.equals(fileName.trim())) {
File newFile = new File(fileName);
FileOutputStream os = new FileOutputStream(newFile);
os.write(file.getBytes());
os.close();
//把file里的内容复制到奥newFile中
file.transferTo(newFile);
String upload = ossUtil.upload(newFile);
//图片回显地址:
//http://yiyige.oss-cn-hangzhou.aliyuncs.com/images/2019-10-21/6c964702b67d4eeb920e7f1f4358189b-dishu.jpg
System.out.println(“path=” + upload);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return “success”;
}
}
#### **7. 编写文件上传页面**
编写一个文件上传页面,请参考之前的章节实现。
基于OSS的上传文件页面
8. 文件上传成功界面
为了看得文件上传的结果,我这里编写了一个文件上传成功后要跳转到的界面。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8"/>
<title>【文件上传成功页面】</title>
</head>
<body>
<div align="center">
<h5>上传成功</h5>
</div>
</body>
</html>
9. 编写入口类
最后
最后,强调几点:
- 1. 一定要谨慎对待写在简历上的东西,一定要对简历上的东西非常熟悉。因为一般情况下,面试官都是会根据你的简历来问的; 能有一个上得了台面的项目也非常重要,这很可能是面试官会大量发问的地方,所以在面试之前好好回顾一下自己所做的项目;
- 2. 和面试官聊基础知识比如设计模式的使用、多线程的使用等等,可以结合具体的项目场景或者是自己在平时是如何使用的;
- 3. 注意自己开源的Github项目,面试官可能会挖你的Github项目提问;
我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!
以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目。
面试答案
- 3. 注意自己开源的Github项目,面试官可能会挖你的Github项目提问;
我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!
以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目。
面试答案
[外链图片转存中…(img-O1JrTaMD-1714756285519)]
[外链图片转存中…(img-ImX9cdHn-1714756285519)]
[外链图片转存中…(img-V7XZGhOT-1714756285519)]