|
@@ -22,16 +22,17 @@ import java.util.UUID;
|
|
|
|
|
|
public class CosUtils {
|
|
|
private static final String ACCESSKEY = "AKIDE6IpMi8fJQRCg1iuWzFajjRs43kbbets";
|
|
|
- private static final String SECRETKEY ="tXzuwMfplTTK3c9GFUyETilasvQfePu9";
|
|
|
+ private static final String SECRETKEY = "tXzuwMfplTTK3c9GFUyETilasvQfePu9";
|
|
|
private static final String APPID = "1301855440";
|
|
|
- private static final String BUCKETNAME = "media-"+APPID;
|
|
|
+ private static final String BUCKETNAME = "media-" + APPID;
|
|
|
private static final String REGIONID = "ap-chongqing";
|
|
|
- private static final String URL_PREFIX = "https://"+BUCKETNAME+".cos.ap-chongqing.myqcloud.com/";
|
|
|
+ private static final String URL_PREFIX = "https://" + BUCKETNAME + ".cos.ap-chongqing.myqcloud.com/";
|
|
|
/**
|
|
|
* https://media-1301855440.cos.ap-chongqing.myqcloud.com/MyFile1/1234.mp4
|
|
|
*/
|
|
|
/**
|
|
|
* 初始化CosClient相关配置, appid、accessKey、secretKey、region
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public static COSClient getCosClient() {
|
|
@@ -50,12 +51,12 @@ public class CosUtils {
|
|
|
/**
|
|
|
* URL_PREFIX+fileSavePath+fileName
|
|
|
* 上传文件
|
|
|
- * @return
|
|
|
- * //绝对路径和相对路径都OK
|
|
|
+ *
|
|
|
+ * @return //绝对路径和相对路径都OK
|
|
|
*/
|
|
|
- public static String uploadFile(String filePath,String fileSavePath,String fileName) {
|
|
|
+ public static String uploadFile(String filePath, String fileSavePath, String fileName) {
|
|
|
File localFile = new File(filePath);
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, fileSavePath+fileName, localFile);
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, fileSavePath + fileName, localFile);
|
|
|
// 设置存储类型, 默认是标准(Standard), 低频(standard_ia),一般为标准的
|
|
|
putObjectRequest.setStorageClass(StorageClass.Standard);
|
|
|
COSClient cc = getCosClient();
|
|
@@ -67,7 +68,7 @@ public class CosUtils {
|
|
|
e.printStackTrace();
|
|
|
} catch (CosClientException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
// 关闭客户端
|
|
|
cc.shutdown();
|
|
|
}
|
|
@@ -76,6 +77,7 @@ public class CosUtils {
|
|
|
|
|
|
/**
|
|
|
* 下载文件
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @param key
|
|
|
* @return
|
|
@@ -93,6 +95,7 @@ public class CosUtils {
|
|
|
|
|
|
/**
|
|
|
* 删除文件
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @param key
|
|
|
*/
|
|
@@ -110,6 +113,7 @@ public class CosUtils {
|
|
|
|
|
|
/**
|
|
|
* 创建桶
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @return
|
|
|
* @throws CosClientException
|
|
@@ -125,15 +129,18 @@ public class CosUtils {
|
|
|
} finally {
|
|
|
}
|
|
|
return bucket;
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
|
|
|
/**
|
|
|
* 删除桶
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @throws CosClientException
|
|
|
* @throws CosServiceException
|
|
|
*/
|
|
|
- public void deleteBucket(String bucketName){
|
|
|
+ public void deleteBucket(String bucketName) {
|
|
|
COSClient cc = getCosClient();
|
|
|
try {
|
|
|
cc.deleteBucket(bucketName);
|
|
@@ -141,20 +148,24 @@ public class CosUtils {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
}
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
|
|
|
/**
|
|
|
* 判断桶是否存在
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean doesBucketExist(String bucketName){
|
|
|
+ public static boolean doesBucketExist(String bucketName) {
|
|
|
COSClient cc = getCosClient();
|
|
|
return cc.doesBucketExist(bucketName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查看桶文件
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @return
|
|
|
* @throws CosClientException
|
|
@@ -197,31 +208,32 @@ public class CosUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *查询一个 Bucket 所在的 Region。
|
|
|
+ * 查询一个 Bucket 所在的 Region。
|
|
|
+ *
|
|
|
* @param bucketName
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getBucketLocation(String bucketName){
|
|
|
+ public static String getBucketLocation(String bucketName) {
|
|
|
COSClient cosClient = getCosClient();
|
|
|
// bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
|
|
return cosClient.getBucketLocation(bucketName);
|
|
|
}
|
|
|
|
|
|
- public static ResFileDTO uploadMultipartFile(MultipartFile file,String fileSavePath){
|
|
|
+ public static ResFileDTO uploadMultipartFile(MultipartFile file, String fileSavePath) {
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
InputStream inputStream;
|
|
|
ResFileDTO o = new ResFileDTO();
|
|
|
- String fileType ;
|
|
|
+ String fileType;
|
|
|
long fileSize = file.getSize();
|
|
|
try {
|
|
|
inputStream = file.getInputStream();
|
|
|
FileType type = FileTypeJudge.getType(inputStream);
|
|
|
|
|
|
- if(type == null || "null".equals(type.toString()) ||
|
|
|
- "XLS_DOC".equals(type.toString())|| "XLSX_DOCX".equals(type.toString()) ||
|
|
|
- "WPSUSER".equals(type.toString())|| "WPS".equals(type.toString())){
|
|
|
+ if (type == null || "null".equals(type.toString()) ||
|
|
|
+ "XLS_DOC".equals(type.toString()) || "XLSX_DOCX".equals(type.toString()) ||
|
|
|
+ "WPSUSER".equals(type.toString()) || "WPS".equals(type.toString())) {
|
|
|
fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
fileType = type.toString().toLowerCase();
|
|
|
}
|
|
|
|
|
@@ -232,7 +244,7 @@ public class CosUtils {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- o = uploadDetailInputStream(file.getInputStream(),fileName,fileType,fileSize,fileSavePath);
|
|
|
+ o = uploadDetailInputStream(file.getInputStream(), fileName, fileType, fileSize, fileSavePath);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -240,12 +252,12 @@ public class CosUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static ResFileDTO uploadDetailInputStream (InputStream in, String fileName , String fileType, long fileSize,String fileSavePath) {
|
|
|
- String uuidFileName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
|
|
|
- String fileUrl = URL_PREFIX+fileSavePath+uuidFileName;
|
|
|
+ public static ResFileDTO uploadDetailInputStream(InputStream in, String fileName, String fileType, long fileSize, String fileSavePath) {
|
|
|
+ String uuidFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ String fileUrl = URL_PREFIX + fileSavePath + uuidFileName;
|
|
|
String md5key = uploadFileByInputStream(in, fileSavePath, uuidFileName);
|
|
|
ResFileDTO o = new ResFileDTO();
|
|
|
- if(md5key != null){
|
|
|
+ if (md5key != null) {
|
|
|
o.setFileType(fileType);
|
|
|
o.setFileName(fileName);
|
|
|
o.setCFileName(uuidFileName);
|
|
@@ -256,9 +268,27 @@ public class CosUtils {
|
|
|
return o;
|
|
|
}
|
|
|
|
|
|
- public static String uploadFileByInputStream(InputStream fileStream, String fileSavePath, String fileName){
|
|
|
+
|
|
|
+ public static ResFileDTO uploadDetailInputStreamV2(InputStream in, String fileName, String fileType, long fileSize, String fileSavePath) {
|
|
|
+ // String uuidFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ String fileUrl = URL_PREFIX + fileSavePath + fileName;
|
|
|
+ String md5key = uploadFileByInputStream(in, fileSavePath, fileName);
|
|
|
+ ResFileDTO o = new ResFileDTO();
|
|
|
+ if (md5key != null) {
|
|
|
+ o.setFileType(fileType);
|
|
|
+ o.setFileName(fileName);
|
|
|
+ o.setCFileName(fileName);
|
|
|
+ o.setFileUrl(fileUrl);
|
|
|
+ o.setFileSize(fileSize);
|
|
|
+ o.setMd5key(md5key);
|
|
|
+ }
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String uploadFileByInputStream(InputStream fileStream, String fileSavePath, String fileName) {
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, fileSavePath+fileName, fileStream,metadata);
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, fileSavePath + fileName, fileStream, metadata);
|
|
|
// 设置存储类型, 默认是标准(Standard), 低频(standard_ia),一般为标准的
|
|
|
putObjectRequest.setStorageClass(StorageClass.Standard);
|
|
|
COSClient cc = getCosClient();
|
|
@@ -270,7 +300,7 @@ public class CosUtils {
|
|
|
e.printStackTrace();
|
|
|
} catch (CosClientException e) {
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
// 关闭客户端
|
|
|
cc.shutdown();
|
|
|
}
|
|
@@ -278,11 +308,7 @@ public class CosUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- String filepath = "D:\\1234.mp4";
|
|
|
- String fileSavePath = "script-lib/wps/";
|
|
|
- String fileName = UUID.randomUUID().toString()+filepath.substring(filepath.lastIndexOf("."));
|
|
|
- String filePath = uploadFile(filepath, fileSavePath, fileName);
|
|
|
- System.out.println(filePath);
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|