在项目中,将图片等文件资源上传到阿里云的OSS,减少服务器压力。
- 项目中导入阿里云的SDK
| <dependency> |
| <groupId>com.aliyun.oss</groupId> |
| <artifactId>aliyun-sdk-oss</artifactId> |
| <version>3.10.2</version> |
| </dependency> |
- 为自己的OSS创建访问的AccessKey,有了AccessKey程序就可以访问账户资源的API了。


这就是AccessKey


然后将AcessKey的key和secret、OSS的区域节点以及bucket名称配置到配置文件中,
| alioss: |
| accessKeyId: LTAI5xxxxxxxxxxxxFUDMoy |
| accessKeySecret: KxxxxxxxxxxxxxxxxxI |
| endpoint: oss-cn-qingdao.aliyuncs.com |
| bucketName: kkbank |
- 封装工具类,去读取配置文件中的AccessKey、endpoint、bucketName,注意配置文件中的名称要与属性名保持一致,负责无法为属性赋值。
| |
| @Data |
| @Slf4j |
| @Component |
| @ConfigurationProperties(prefix = "alioss") |
| public class AliOssUtil { |
| |
| private String endpoint; |
| private String accessKeyId; |
| private String accessKeySecret; |
| private String bucketName; |
| |
| |
| |
| |
| |
| |
| |
| |
| public String upload(String fileName, InputStream inputStream) { |
| |
| |
| OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| |
| |
| String objectName = "images/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date()) |
| + "/" + System.currentTimeMillis() +"-"+fileName; |
| try { |
| |
| ObjectMetadata metadata = new ObjectMetadata(); |
| |
| metadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf(".")))); |
| |
| |
| ossClient.putObject(bucketName, objectName, inputStream,metadata); |
| } catch (OSSException oe) { |
| System.out.println("Error Message:" + oe.getErrorMessage()); |
| } catch (ClientException ce) { |
| System.out.println("Error Message:" + ce.getMessage()); |
| } finally { |
| if (ossClient != null) { |
| |
| ossClient.shutdown(); |
| } |
| } |
| |
| |
| StringBuilder stringBuilder = new StringBuilder("https://"); |
| stringBuilder |
| .append(bucketName) |
| .append(".") |
| .append(endpoint) |
| .append("/") |
| .append(objectName); |
| |
| log.info("文件上传到:{}", stringBuilder.toString()); |
| |
| return stringBuilder.toString(); |
| } |
| |
| |
| |
| |
| |
| |
| private String getContentType(String filenameExtension) { |
| if (filenameExtension.equalsIgnoreCase(".bmp")) { |
| return "image/bmp"; |
| } |
| if (filenameExtension.equalsIgnoreCase(".gif")) { |
| return "image/gif"; |
| } |
| if (filenameExtension.equalsIgnoreCase(".jpeg") || |
| filenameExtension.equalsIgnoreCase(".jpg") || |
| filenameExtension.equalsIgnoreCase(".png")) { |
| return "image/jpg"; |
| } |
| return "image/jpg"; |
| } |
| } |
- 在需要的地方调用即可,利用SpringMVC中的MultiPartFile来接受文件流。
| @RestController |
| @RequestMapping("/admin/common") |
| @Slf4j |
| public class CommonController { |
| |
| @Autowired |
| private AliOssUtil aliOssUtil; |
| |
| |
| @PostMapping("/upload") |
| public Result upload(MultipartFile file) throws IOException { |
| String path = aliOssUtil.upload(file.getOriginalFilename(), file.getInputStream()); |
| log.info("图片上传之后的访问路径是{}", path); |
| return Result.success(path); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律