一、第一步

        引入依赖

<!-- 阿里云OSS -->
<dependency>
   <groupId>com.aliyun.oss</groupId>
   <artifactId>aliyun-sdk-oss</artifactId>
</dependency>

二、第二步      

          application.yml

#阿里云oss服务配置
aliyun:
  oss:
    endpoint: oss-cn-hangzhou.aliyuncs.com
    accessKeyId: LTAI4G9eB**********
    accessKeySecret: zsAM2Jkr*******
    bucketName: *****

其中bucketName是 存储空间名称,注意这以下几个地方
        

三、第三步

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OssConfig {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Bean(destroyMethod = "shutdown")
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
}

四、第四步


import com.aliyun.oss.OSS;
import com.oversea.common.config.OverseaConfig;
import com.oversea.common.core.domain.AjaxResult;
import com.oversea.common.utils.file.FileUploadUtils;
import com.oversea.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.UUID;

@Service
public class OssService {

    @Autowired
    private OSS ossClient;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    /**
     * 上传文件到OSS的指定路径 /oss/uploads/oversea/YYYY/MM/DD/
     */
    public AjaxResult uploadFile(MultipartFile file) throws IOException {
        try (InputStream inputStream = file.getInputStream()) {
            // 获取当前日期
            LocalDate now = LocalDate.now();

            // 构建目标文件夹路径(格式:oss/uploads/oversea/YYYY/MM/DD/)
            String folderPath = String.format("oss/uploads/oversea/%d/%02d/%02d/",
                    now.getYear(), now.getMonthValue(), now.getDayOfMonth());

            // 确保文件夹路径以斜杠结尾(这里已经确保了)

            // 构建唯一文件名,包含指定的文件夹路径
            String fileName = folderPath + UUID.randomUUID() + "_" + file.getOriginalFilename();

            // 上传文件流到指定位置
            ossClient.putObject(bucketName, fileName, inputStream);

            // 返回文件访问链接
            String url = "https://" + bucketName + "." + endpoint + "/" + fileName;

            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", fileName.substring(fileName.lastIndexOf("/") + 1));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
    }
}

五、第五步

/**
     * 文件上传接口
     */
    @PostMapping("/uploadOss")
    public AjaxResult uploadOss(MultipartFile file) throws Exception {
        try {
            return ossService.uploadFile(file);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

六、第六步

前端若依框架组件调用,文件或者图片上传

预览

七、第七步

前端页面展示效果,预览

文件上传

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐