您的位置:首页 > 文旅 > 旅游 > 山西新闻头条最新消息_dw网页设计免费下载_网络科技公司网站建设_邀请推广app

山西新闻头条最新消息_dw网页设计免费下载_网络科技公司网站建设_邀请推广app

2025/9/1 15:09:25 来源:https://blog.csdn.net/u014001523/article/details/145949310  浏览:    关键词:山西新闻头条最新消息_dw网页设计免费下载_网络科技公司网站建设_邀请推广app
山西新闻头条最新消息_dw网页设计免费下载_网络科技公司网站建设_邀请推广app

一、第一步

        引入依赖

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

二、第二步      

          application.yml

#阿里云oss服务配置
aliyun:oss:endpoint: oss-cn-hangzhou.aliyuncs.comaccessKeyId: 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 {@Autowiredprivate 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());}}

六、第六步

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

预览

七、第七步

前端页面展示效果,预览

文件上传

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com