<!-- 阿里云 OSS SDK 依赖 --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.13.2</version> </dependency>
package com.alive.server.oss;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class AliyunOssUtils {
public static String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
//
public static String accessKeyId = "LTAI5t9qojLQd7XJ27SLtBvw";
public static String accessKeySecret = "RVpT3A9bva47n71E2Pc4k5CI9fprzt";
// 创建阿里云登录凭证
public static CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
// 创建OSSClient实例。
public static OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
/**
* 创建bucket
*/
public static void createBucket(){
String bucketName = "java-hello-world";
try {
// 创建存储空间。
ossClient.createBucket(bucketName);
} catch (Exception e) {
System.out.println("Error Message:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 上传文件
*/
public static void uploadFile(){
// 填写Bucket名称,如果Bucket不存在, 可以先创建一个
String bucketName = "java-hello-world";
// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称
String objectName = "test/test.txt";
try {
String content = "Hello OSS";
// 采用字节数组方式上传
PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
System.out.println("putObjectResult = " + putObjectResult.toString());
} catch (Exception e) {
System.out.println("Error Message:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 下载文件
*/
public static void downloadFile(){
// 填写Bucket名称
String bucketName = "java-hello-world";
// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称
String objectName = "test/test.txt";
try {
// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
InputStream content = ossObject.getObjectContent();
if (content != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
System.out.println("\n" + line);
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
content.close();
}
} catch (Exception e) {
System.out.println("Error Message:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 查询某个bucket的Object列表
*/
public static void getObjectList(){
// 填写Bucket名称
String bucketName = "java-hello-world";
try {
// ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。
ObjectListing objectListing = ossClient.listObjects(bucketName);
int size = objectListing.getObjectSummaries().size();
System.out.println("=========查询到的对象数量是:"+size);
// objectListing.getObjectSummaries获取所有文件的描述信息。
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" ======================== " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
}
} catch (Exception e) {
System.out.println("Error Message:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 删除某个对象
*/
public static void deleteObject(){
// 填写Bucket名称
String bucketName = "java-hello-world";
// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称
String objectName = "test/test.txt";
try {
// 删除文件
ossClient.deleteObject(bucketName, objectName);
} catch (OSSException e) {
System.out.println("Error Message:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 获取阿里云OSS客户端对象
*
* @return ossClient
*/
public static OSS getOSSClient() {
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
// 连接空闲超时时间,超时则关闭
conf.setIdleConnectionTime(1000);
// 在这里可以做一些配置,比如超时时间、最大连接数之类的
return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
}
/**
* 创建桶
*
* @param ossClient OSS连接
* @param bucketName 桶名称
* @return
*/
public static String createBucketName(OSS ossClient, String bucketName) {
if (!ossClient.doesBucketExist(bucketName)) {
//创建存储空间
Bucket bucket = ossClient.createBucket(bucketName);
log.info("创建存储空间成功");
return bucket.getName();
}
return bucketName;
}
/**
* 上传图片至OSS 文件流
*
* @param ossClient oss连接
* @param file 上传文件(文件全路径如:D:\\image\\cake.jpg)
* @param bucketName 桶名称
* @param folder 阿里云API的文件夹名称(父文件夹)
* @param format 阿里云API的文件夹名称(子文件夹)
* @param formats 文件名
* @return String 返回的唯一MD5数字签名
*/
public static String[] uploadObject2OSS(File file) {
String bucketName= "shenzhencangku";
createBucketName(ossClient,bucketName);
String folder= "";
String format="";
String formats="123";
// 创建一个可重用固定线程数的线程池
ExecutorService executorService = Executors.newFixedThreadPool(5);
String[] resultArr = new String[2];
try {
// 分片上传
folder = folder + format;
// 文件名
String fileName = file.getName();
// 文件扩展名
String fileExtension = fileName.substring(fileName.lastIndexOf("."));
// 最终文件名:UUID + 文件扩展名
fileName = formats + fileExtension;
fileName = "shenzhencangku_m-wz95qgbprp4ppqnq93io_system.raw.tar.gz";
// 上传路径 如:appversion/20200723/a3662009-897c-43ea-a6d8-466ab8310c6b.apk
// objectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
String objectName = folder + fileName;
log.info("上传到路径:" + objectName);
// 文件大小
long fileSize = file.length();
// 创建上传Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
// 指定该Object被下载时的网页的缓存行为
metadata.setCacheControl("no-cache");
// 指定该Object下设置Header
metadata.setHeader("Pragma", "no-cache");
// 指定该Object被下载时的内容编码格式
metadata.setContentEncoding("utf-8");
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata.setContentType(getContentType(fileExtension));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
// 创建InitiateMultipartUploadRequest对象
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName, metadata);
// 初始化分片
InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request);
// 返回uploadId,它是分片上传事件的唯一标识,您可以根据这个uploadId发起相关的操作,如取消分片上传、查询分片上传等
String uploadId = upresult.getUploadId();
// partETags是PartETag的集合。PartETag由分片的ETag和分片号组成
List<PartETag> partETags = Collections.synchronizedList(new ArrayList<>());
// 计算文件有多少个分片
final long maxPartCount = 10000;
final long fileLength = file.length();
long partSize = 1 * 1024 * 1024L; // 初始分片大小为1MB
// 计算分片数量,如果分片数量超过10000,则增大分片大小
long partCount = (fileLength / partSize);
if (fileLength % partSize != 0) {
partCount++;
}
while (partCount > maxPartCount) {
partSize *= 2; // 将分片大小增加一倍
partCount = (fileLength / partSize);
if (fileLength % partSize != 0) {
partCount++;
}
}
if (partCount > 10000) {
throw new RuntimeException("分片总块数不能超过10000");
} else {
log.info("分片总块数:" + partCount);
}
// 遍历分片上传
for (int i = 0; i < partCount; i++) {
long startPos = i * partSize;
long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
int partNumber = i + 1;
// 实现并启动线程
executorService.execute(new Runnable() {
@Override
public void run() {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
// 跳过已经上传的分片
inputStream.skip(startPos);
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(objectName);
uploadPartRequest.setUploadId(uploadId);
uploadPartRequest.setInputStream(inputStream);
// 设置分片大小。除了最后一个分片没有大小限制,其他的分片最小为100 KB。
uploadPartRequest.setPartSize(curPartSize);
// 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出这个范围,OSS将返回InvalidArgument的错误码。
uploadPartRequest.setPartNumber(partNumber);
// 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
// 每次上传分片之后,OSS的返回结果会包含一个PartETag。PartETag将被保存到PartETags中。
synchronized (partETags) {
partETags.add(uploadPartResult.getPartETag());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
});
}
// 等待所有的分片完成
// shutdown方法:通知各个任务(Runnable)的运行结束
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
// 指定的时间内所有的任务都结束的时候,返回true,反之返回false,返回false还有执行完的任务
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
}
// 立即关闭所有执行中的线程
// executorService.shutdownNow();
// 验证是否所有的分片都完成
if (partETags.size() != partCount) {
throw new IllegalStateException("文件的某些部分上传失败!");
} else {
log.info("文件上传成功:" + file.getName());
}
// 完成分片上传 进行排序。partETags必须按分片号升序排列
Collections.sort(partETags, new Comparator<PartETag>() {
@Override
public int compare(PartETag o1, PartETag o2) {
return o1.getPartNumber() - o2.getPartNumber();
}
});
// 创建CompleteMultipartUploadRequest对象
// 在执行完成分片上传操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);
// 设置文件访问权限
// completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.PublicRead);
// 完成上传
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
if (completeMultipartUploadResult != null) {
// 解析结果
resultArr[0] = completeMultipartUploadResult.getETag();
resultArr[1] = objectName;
return resultArr;
}
} catch (Exception e) {
e.printStackTrace();
log.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
} finally {
// 关闭OSSClient
ossClient.shutdown();
}
return null;
}
/**
* 通过文件名判断并获取OSS服务文件上传时文件的contentType
*
* @param fileExtension 文件名扩展名
* @return 文件的contentType
*/
public static String getContentType(String fileExtension) {
// 文件的后缀名
if (".bmp".equalsIgnoreCase(fileExtension)) {
return "image/bmp";
}
if (".gif".equalsIgnoreCase(fileExtension)) {
return "image/gif";
}
if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|| ".png".equalsIgnoreCase(fileExtension)) {
return "image/jpeg";
}
if (".html".equalsIgnoreCase(fileExtension)) {
return "text/html";
}
if (".txt".equalsIgnoreCase(fileExtension)) {
return "text/plain";
}
if (".vsd".equalsIgnoreCase(fileExtension)) {
return "application/vnd.visio";
}
if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
return "application/vnd.ms-powerpoint";
}
if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
return "application/msword";
}
if (".xml".equalsIgnoreCase(fileExtension)) {
return "text/xml";
}
if (".mp4".equalsIgnoreCase(fileExtension)) {
return "video/mp4";
}
// android
if (".apk".equalsIgnoreCase(fileExtension)) {
return "application/vnd.android.package-archive";
}
// ios
if (".ipa".equals(fileExtension)) {
return "application/vnd.iphone";
}
// 默认返回类型
return "application/octet-stream";
}
public static void main(String[] args) throws Exception {
uploadFile();
// downloadFile();
// getObjectList();
// deleteObject();
}
}
package com.alive.server.oss;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* 文件转换工具类
*
* @author xiegege
* @date 2020-07-23
*/
public class MultipartFileToFileUtil {
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) {
try {
File toFile;
if (file != null && file.getSize() > 0) {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
return toFile;
}
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取流文件
*
* @param ins
* @param file
*/
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地临时文件
*
* @param file
*/
public static void deleteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
}
获取accessKeyId 和 accessKeySecret
public static String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
看服务器 ,我这里是深圳 oss-cn-shenzhen
这个是俑的名称:我们创建oss的名称就是
String bucketName= "shenzhencangku";