Springboot 阿里云OSS对象存储
OSSController
package com.wzb.OSSController20241009;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.wzb.pojo20241009.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
@RestController
public class OSSController {
@PostMapping("/OSSUpload")
public Result upload(String username, Integer age, MultipartFile image) throws IOException {
// 获取原文件名
String originalFileName = image.getOriginalFilename();
// 获取原文件的扩展名
String externalFileName = ".txt";
if (originalFileName != null) {
// 从最后一个.开始进行字符串切割,就可以成功得到原文件的扩展名
externalFileName = originalFileName.substring(originalFileName.lastIndexOf("."));
}
// 使用UUID给文件重命名
String newFileName = UUID.randomUUID().toString() + externalFileName;
// 先保存到本地
String filePath = "E:/images/" + newFileName;
image.transferTo(new File(filePath));
// 准备阿里云
// 声明bucket的Endpoint
String endpoint = "https://oss-cn-chengdu.aliyun.com";
// 声明阿里云账号
String accessKeyId = "LTAI5tKr5ZzpE1BRJ4yHGEVQ";
String accessKeySecret = "DqZAfe3cXfX79J72RmcszqEX0wG3D5";
// 声明bucket的名称
String bucketName = "wzb-study";
// 创建ossClient对象
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 创建ossClient的方法:OssClientBuilder中的build方法,需要传递endpoint、accessKeyId和accessKeySecret才可以成功创建
// 准备上传
try {
// 开启文件输入流
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObjectRequest对象
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newFileName, inputStream);
// 这是一个上传对象的请求类,需要传递保存至哪个bucket的bucketName,需要上传的文件的文件名,文件输入流
// 可以返回response,若不设置,则返回的response为null
putObjectRequest.setProcess("true");
// 创建PutObject请求 >> 这步就是上传了
PutObjectResult result = ossClient.putObject(putObjectRequest);
// 上传成功后的返回值
System.out.println(result.getResponse().getStatusCode());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
// 响应
return Result.success();
}
}
EmpService
package com.wzb.service20241009;
import com.wzb.mapper20241009.EmpMapper;
import com.wzb.pojo20241009.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@Service
public class EmpServiceImplement implements EmpService{
@Autowired
private EmpMapper empMapper;
@Override
public Emp selectEmp(Integer id) {
return empMapper.selectEmp(id);
}
@Override
public void update(Emp emp) {
// 为emp补全信息————将员工的修改时间更新为当前的时间
emp.setUpdateTime(LocalDateTime.now());
// 调用Mapper层的方法操作数据库完成更新
empMapper.updateEmp(emp);
}
}
EmpMapper
package com.wzb.mapper20241009;
import com.wzb.pojo20241009.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface EmpMapper {
@Select("select * from emp where id = #{id}")
public Emp selectEmp(Integer id);
// 因为更新员工数据的SQL语句相对比较复杂,所以说需要使用xml动态SQL
public void updateEmp(Emp emp);
}
Emp类
package com.wzb.pojo20241009;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private Integer id;
private String username;
private String password;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate;
private Integer deptId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
响应结果Result
package com.wzb.pojo20241009;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code; // 响应码,1是响应成功,2是响应失败
private String message; // 响应信息,描述响应结果的字符串
private Object data; // 响应返回的数据
// 响应成功,但不返回数据的方法
public static Result success() {
return new Result(1, "success", null);
}
// 响应成功,并且返回数据的方法
public static Result success(Object data) {
return new Result(1,"success", data);
}
// 响应失败的方法
public static Result fail(String message) {
return new Result(0, message, null);
}
}
标签:存储,String,OSS,private,Result,import,com,public,Springboot From: https://blog.csdn.net/Aishangyuwen/article/details/142798987