直接贴出已经使用过的工具类代码
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.moonlight.sdk.consts.SystemConst;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author
* @project
* @Description: TODO
* @date 2020-03-20 9:15
*/
public class OssUploadUtils {
/**
* 创建OSSClient
*
* @return
*/
public static OSSClient getOSSClient(Map<String, String> config) {
String endpoint = config.get("endpoint");
String accessKeyId = config.get("accessKeyId");
String accessKeySecret = config.get("accessKeySecret");
// 创建OSSClient实例
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
/**
* 创建OSSClient
*
* @return
*/
public static OSSClient getOSSClient() {
Map<String, String> config = getConfig();
String endpoint = config.get("endpoint");
String accessKeyId = config.get("accessKeyId");
String accessKeySecret = config.get("accessKeySecret");
// 创建OSSClient实例
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
/**
* 关闭OSSClient
*
* @return
*/
public static void closeOSSClient(OSSClient ossClient) {
// 关闭client
ossClient.shutdown();
}
private static Map<String, String> getConfig(){
Map<String, String> config = new HashMap<>();
config.put("endpoint", SystemConst.OSS_ENDPOINT);
config.put("accessKeyId", SystemConst.OSS_ACCESS_KEY_ID);
config.put("accessKeySecret", SystemConst.OSS_ACCESS_KEY_SECRET);
config.put("bucketName", SystemConst.OSS_BUKET_NAME);
config.put("filepath", SystemConst.OSS_OSS_PATH);
return config;
}
public static Map<String, String> saveFile(Map<String, MultipartFile> map) throws Exception {
Map<String, String> config = getConfig();
if (config == null || map == null) {
return null;
}
String bucketName = config.get("bucketName");
String filepath = config.get("filepath");
// 创建OSSClient实例
OSSClient ossClient = getOSSClient(config);
// 上传
int size = map.size();
System.out.println("上传文件个数" + size);
Map<String, String> url = new HashMap<>();
Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 100);
for (Map.Entry entry : map.entrySet()) {
ossClient.putObject(bucketName, filepath + entry.getKey(), new ByteArrayInputStream(((MultipartFile) entry.getValue()).getBytes()));
url.put(entry.getKey().toString(), ossClient.generatePresignedUrl(bucketName, filepath + entry.getKey(), expiration).toString());
}
// 关闭client
closeOSSClient(ossClient);
return url;
}
/**
* 根据文件名称获取文件类型后缀
* @param url
* @return
*/
public static String getFileSuffix(String url) {
if (StringUtils.isBlank(url)) {
return null;
}
int index = url.lastIndexOf(".");
if (index < 0) {
return null;
}
return url.substring(index);
}
/**
* 下载文件
* @param bucketName
* @param url
* @return
*/
public static void getObject(String bucketName, String url, String fileName) throws IOException {
if (StringUtils.isBlank(url)) {
return;
}
String[] array = url.split("[?]");
String u = array[0];
String key = getConfig().get("filepath") + u.substring(u.lastIndexOf("/") + 1);
String suffix = getFileSuffix(u);
fileName = fileName + suffix;
if (StringUtils.isBlank(bucketName)) {
bucketName = getConfig().get("bucketName");
}
OSSClient ossClient = getOSSClient(getConfig());
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
ossClient.getObject(new GetObjectRequest(bucketName, key), file);
closeOSSClient(ossClient);
}
public static void main(String[] args) {
String name = "accsd.jpeg";
String a = getFileSuffix(name);
System.out.println(a);
}
}
注意:oss下载只能用ossClient里面的api,用HttpUrl的方式下载是会被拦截的,就是加上了模拟浏览器的参数也不成功,然后oss的object的key值是包含filePath的,需要注意下