
- files:
- upload:
- path: D:/SpringBootItem/springboot/files/
其中有用到hutool工具依赖,如下在pom.xml中添加依赖,也可以选择不添加,自己修改下Controller中的代码即可
-
-
-
cn.hutool -
hutool-all -
5.7.20 -
- package com.example.springboot.controller;
-
- /**
- * mybatis代码生成器配置
- */
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.util.IdUtil;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.io.File;
- import java.io.IOException;
-
- import com.example.springboot.service.IUploadLogService;
-
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
- /**
- *
- * 上传文件
- */
- @RestController
- @RequestMapping("/file")
- public class UploadLogController {
-
- /**
- * 上传到本地磁盘地址【从application.yml中获取预设置地址参数】
- */
- @Value("${files.upload.path}")
- private String fileUploadPath;
-
- @PostMapping("/upload")
- public String upload (@RequestParam MultipartFile file) throws IOException {
-
- // 获取文件参数
- String originalFilename = file.getOriginalFilename();
- String type = FileUtil.extName(originalFilename);
- long size = file.getSize();
-
- File uploadFiletest = new File(fileUploadPath);
-
- // 判断是否不存在该目录,如果是,新建一个该目录
- if (!uploadFiletest.exists()) {
- uploadFiletest.mkdirs();
- }
-
- // 定义一个文件的唯一标识码
- String uuid = IdUtil.fastSimpleUUID();
-
- // 重新拼接
- File uploadFile = new File(fileUploadPath + uuid + "." + type);
-
- // 进行存储到磁盘
- file.transferTo(uploadFile);
-
- return "1";
- }
-
- }

-
-
-
com.aliyun.oss -
aliyun-sdk-oss -
3.15.1 -
如何使用STS以及签名URL临时授权访问OSS资源(Java)_对象存储 OSS-阿里云帮助中心 (aliyun.com)
- package com.example.springboot.utils;
-
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import org.springframework.beans.factory.annotation.Value;
-
- import java.io.ByteArrayInputStream;
-
- /**
- * 阿里云OSS工具类
- */
- public class AliOssUtil {
-
- // endpoint填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou.aliyuncs.com。
- public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com";
- // 阿里云账号RAM用户进行API访问或日常运维(账号)。
- public static final String accessKeyId = "************************";
- // 阿里云账号RAM用户进行API访问或日常运维(密钥)。
- public static final String accessKeySecret = "***************************";
- // bucket名
- public static String bucket = "bucketName";
-
-
- // 连接阿里云OSS
- public static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
-
- /**
- * 阿里云OSS上传文件
- * @param data
- * @param url
- * @return
- */
- public static String uploadFile(byte[] data, String url) {
- try{
- ossClient.putObject(bucket, url, new ByteArrayInputStream(data));
- return "true";
- } catch (Exception e) {
- System.out.println(e);
- return "false";
- }
- }
-
- /**
- * 阿里云OSS删除文件
- * @param url
- * @return
- */
- public static String delFile(String url) {
- try{
- ossClient.deleteObject(bucket, url);
- return "true";
- } catch (Exception e) {
- System.out.println(e);
- return "false";
- }
- }
- }
- package com.example.springboot.controller;
-
- /**
- * mybatis代码生成器配置
- */
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.util.IdUtil;
- import com.aliyun.oss.common.utils.IOUtils;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.example.springboot.entity.UploadLog;
- import com.example.springboot.utils.AliOssUtil;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.List;
-
- import com.example.springboot.service.IUploadLogService;
-
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
-
- /**
- *
- * 上传日志 前端控制器
- *
- *
- * @author Sca_jie
- * @since 2023-09-13
- */
- @RestController
- @RequestMapping("/file")
- public class UploadLogController {
-
- /**
- * 自定义controller生成模板
- */
- @Resource
- private IUploadLogService uploadLogService;
-
- /**
- * 上传到本地磁盘地址【从application.yml中获取预设置地址参数】
- */
- @Value("${files.upload.path}")
- private String fileUploadPath;
-
-
- /**
- * 上传文件
- * @param file 文件(form-data类型,参数名file)
- * @return
- * @throws IOException
- */
- @PostMapping("/upload")
- public String upload (@RequestParam MultipartFile file) throws IOException {
-
- // 获取文件参数
- String originalFilename = file.getOriginalFilename();
- String type = FileUtil.extName(originalFilename);
- long size = file.getSize();
-
- File uploadFiletest = new File(fileUploadPath);
-
- // 判断是否不存在该目录,如果是,新建一个该目录
- if (!uploadFiletest.exists()) {
- uploadFiletest.mkdirs();
- }
-
- // 定义一个文件的唯一标识码
- String uuid = IdUtil.fastSimpleUUID();
-
- // 重新拼接
- File uploadFile = new File(fileUploadPath + uuid + "." + type);
-
- // 进行存储到磁盘
- file.transferTo(uploadFile);
-
- // 读取文件
- FileInputStream fileInputStream = new FileInputStream(fileUploadPath + uuid + "." + type);
- byte[] data = IOUtils.readStreamAsByteArray(fileInputStream);
-
- // 上传阿里云OSS
- String res = AliOssUtil.uploadFile(data, "upload/2023-09/" + uuid + "." + type);
-
- return res;
- }
-
- /**
- * 删除阿里云OSS文件
- * @param url
- * @return
- */
- @PostMapping("/delfile")
- public String delFile (@RequestParam String url) {
- System.out.println(url);
- String res = AliOssUtil.delFile(url);
- return res;
- }
-
- }

