<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<!-- 顺便导入一下lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
这里需要配置一下自己的 endpoint、accessKeyId、accessKeySecret、bucketName,需要去自己的阿里云查看
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;
@Data
@Slf4j
public class AliOssUtil {
/*
* 去aliyun oss管理控制台查看
*/
private static String endpoint="自己的endpoint";
private static String accessKeyId="密钥ID";
private static String accessKeySecret="密钥密码";
private static String bucketName="bucket名字";
/**
* 文件上传
*
* @param bytes
* @param objectName
* @return
*/
public static String upload(byte[] bytes, String objectName) {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
} 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();
}
}
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
StringBuilder stringBuilder = new StringBuilder("https://");
stringBuilder
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);
log.info("文件上传到:{}", stringBuilder.toString());
return stringBuilder.toString();
}
}
到这里已经可以使用 AliOssUtil这个工具了,下面是我写的一个文件上传的方法
想自定义Controller的小伙伴这一步可以自己写!
import com.example.testutils.utils.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileUpLoadController {
@PostMapping("/upload")
public String upload(MultipartFile file){
try {
String originName= file.getOriginalFilename();
String newName=UUID.randomUUID()+originName.substring(originName.lastIndexOf("."));
AliOssUtil.upload(file.getBytes(), newName);
} catch (IOException e) {
return "上传失败!";
}
return "上传成功!";
}
}