定义接口
byte[] readFile(String url);
List<byte[]> batchReadFile(List<String> urls);
依赖包
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
实现方法
/**
* 单个获取
* @param url
* @return byte[]
**/
public byte[] readFile(String url) {
byte[] content = null;
try {
OSS ossClient = (new OSSClientBuilder())
.build( this.providerProperties.getEndPoint(),
this.providerProperties.getAccessKey(),
this.providerProperties.getSecretKey()
);
content = this.ossRead(ossClient, url);
ossClient.shutdown();
} catch (Exception e) {
log.error("读取文件失败,{}", e);
}
return content;
}
/**
* 批量获取
* @param urls
* @return List<byte[]>
**/
public List<byte[]> batchReadFile(List<String> urls) {
ArrayList bytes = new ArrayList();
try {
OSS ossClient = (new OSSClientBuilder())
.build( this.providerProperties.getEndPoint(),
this.providerProperties.getAccessKey(),
this.providerProperties.getSecretKey()
);
Iterator var4 = urls.iterator();
while(var4.hasNext()) {
String url = (String)var4.next();
byte[] content = this.ossRead(ossClient, url);
bytes.add(content);
}
ossClient.shutdown();
} catch (Exception var7) {
log.error("批量读取文件失败,{}", var7);
}
return bytes;
}
private byte[] ossRead(OSS ossClient, String url) throws Exception {
String objectName = url.replace(this.providerProperties.getBucketHost() + "/", "");
OSSObject ossObject = ossClient.getObject(
this.providerProperties.getBucketName(),
objectName
);
InputStream in = ossObject.getObjectContent();
int buffSize = 1024;
ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
IOUtils.copyLarge(in, out);
in.close();
out.close();
return out.toByteArray();
}
this.providerProperties
就是OSS的配置,不同项目的配置方式不同,大家根据自身项目的配置方式调整,将配置类注入到上面的实现类中即可
@ConfigurationProperties(
prefix = "file.provider"
)
public class FileProviderProperties {
private String bucketName;
private String bucketHost;
private String endPoint;
private String accessKey;
private String secretKey;
public FileProviderProperties() {
}
public String getBucketName() {
return this.bucketName;
}
public String getBucketHost() {
return this.bucketHost;
}
public String getEndPoint() {
return this.endPoint;
}
public String getAccessKey() {
return this.accessKey;
}
public String getSecretKey() {
return this.secretKey;
}
public void setBucketName(final String bucketName) {
this.bucketName = bucketName;
}
public void setBucketHost(final String bucketHost) {
this.bucketHost = bucketHost;
}
public void setEndPoint(final String endPoint) {
this.endPoint = endPoint;
}
public void setAccessKey(final String accessKey) {
this.accessKey = accessKey;
}
public void setSecretKey(final String secretKey) {
this.secretKey = secretKey;
}
}