oss 图片上传和下载
常用的图片服务器
1.hdfs
2.fastdfs
3.oss
OSS
通过阿里云实现存储对象来管理文件
1.注册阿里云

3.创建bucket
4.创建公钥
5.复制公钥的id和secret
6.创建springboot项目
6.1导入jar包
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- web依赖,tomcat。dispatcherServlet xml-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<!--引入druid数据源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>joda-time</groupId>-->
<!-- <artifactId>joda-time</artifactId>-->
<!-- <version>2.10.10</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
6.2创建实体类:名字随意记录oss需要的所有的属性
package zhiyou.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "aliyun.oss.file")
public class ConstantPropertiesUtils {
//读取配置文件内容
@Value("aliyun.oss.file.endpoint")
private String endpoint;
@Value("aliyun.oss.file.keyid")
private String keyid;
@Value("aliyun.oss.file.keysecret")
private String keysecret;
@Value("aliyun.oss.file.bucketname")
private String bucketname;
}
6.3:在配置文件中创建aliyun.oss.file前缀的相关属性
aliyun.oss.file.bucketname=oss-05013
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.file.keyid=LTAI5tF8a5rYk2FN4njHPzXs
aliyun.oss.file.keysecret=zIaYBlledZJDnkZyFgRxK1gc6uNom3
6.4 创建service
package zhiyou.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSBuilder;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import zhiyou.bean.ConstantPropertiesUtils;
import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.InputStream;
import java.util.UUID;
@Service
@Slf4j
public class OssService {
@Resource
private ConstantPropertiesUtils constantPropertiesUtils;
public String uploadFileAvatar(MultipartFile file) {
//获取oss上传文件中的参数
String bucketname = constantPropertiesUtils.getBucketname();
String endpoint = constantPropertiesUtils.getEndpoint();
String keyId = constantPropertiesUtils.getKeyid();
String keysecret= constantPropertiesUtils.getKeysecret();
System.out.println("keysecret = " + keysecret);
System.out.println("keyId = " + keyId);
OSS ossClient;
InputStream inputStream;
//创建ossclient实例
try {
ossClient = new OSSClientBuilder().build(endpoint,keyId,keysecret);
//上传文件流
inputStream = file.getInputStream();
//为了是的文件可以重复的上传,每次上传的是偶需要将文件名进行修改
String filename = file.getOriginalFilename();
log.info("图片上传的名字为{},",filename);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String newFileName = uuid+filename;
//获取当前的日期,然后以日期和新的文件名组成全路径,使得oss中的文件按照日期进行分类存储
String date = new DateTime().toString("yyyy/MM/dd");
String fullFilaName = date+"/"+newFileName;
log.info("图片保存在oss的全路径为,{}",fullFilaName);
//第一个参数Bucket名称,第二个参数上传搭到oss文件路径和文件名称
ossClient.putObject(bucketname,fullFilaName,inputStream);
//关闭OSSClient
ossClient.shutdown();
System.out.println("https://"+bucketname+"."+endpoint+"/"+fullFilaName);
return "https://"+bucketname+"."+endpoint+"/"+fullFilaName;
} catch (Exception e) {
log.error("文件上传失败",e);
throw new RuntimeException("文件上传oss丢失");
}
}
public void downLoadFileAvatar(String yuan, File muDi){
String bucketName = constantPropertiesUtils.getBucketname();
String endpoint = constantPropertiesUtils.getEndpoint();
String keyId = constantPropertiesUtils.getKeyid();
String keySecret = constantPropertiesUtils.getKeysecret();
//创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint,keyId,keySecret);
// 下载Object到本地文件,并保存到指定的本地路径中。如果指定本地文件存在会覆盖,不存在则新建。
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属目对应本地路径中。
File muDiFile = new File(muDi,System.currentTimeMillis()+(yuan.substring(yuan.indexOf("."))));
ossClient.getObject(new GetObjectRequest(bucketName,yuan),muDiFile);
//关闭OSSClient
ossClient.shutdown();
}
}
6.5创建controller
package zhiyou.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zhiyou.bean.Department;
import zhiyou.service.DeptSerrvice;
@Controller
public class DepartController {
@Autowired
DeptSerrvice deptSerrvice;
@RequestMapping("/getDeptById/{id}")
@ResponseBody //返回json 数据
public Department getDeptById(@PathVariable("id") Integer id){
return deptSerrvice.getDeptById(id);
}
}
7 测试

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!