package com.dsh.utils;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.UUID;
|
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.Executors;
|
|
import com.qcloud.cos.COSClient;
|
import com.qcloud.cos.ClientConfig;
|
import com.qcloud.cos.auth.BasicCOSCredentials;
|
import com.qcloud.cos.auth.COSCredentials;
|
import com.qcloud.cos.model.PutObjectRequest;
|
import com.qcloud.cos.region.Region;
|
import com.qcloud.cos.transfer.Transfer;
|
import com.qcloud.cos.transfer.TransferManager;
|
import com.qcloud.cos.transfer.Upload;
|
import org.springframework.web.multipart.MultipartFile;
|
|
public class ImgUtil {
|
/**
|
* 设置固定常量
|
*/
|
public static final int APP_ID = 1311705114;
|
public static final String SECRET_ID = "AKIDc4GCsKXT8WdT3MO8cpkOlsjJcePs40e1";
|
public static final String SECRET_KEY = "7xYsvhQt4yiCPL0PXNwEG0TgdIgndCUU";
|
public static final String BUCKETNAME = "chengqi-1311705114";
|
public static final String URL = "https://chengqi-1311705114.cos.ap-chengdu.myqcloud.com";
|
|
/* public static void main(String[] args) {
|
//uploadFile();
|
}*/
|
/**
|
* 上传文件
|
*/
|
public static String uploadFile(MultipartFile file) {
|
|
String pictureName = UUID.randomUUID().toString() + "." + getFileSuffix(file.getOriginalFilename());
|
// 1 初始化用户身份信息(secretId, secretKey)
|
COSCredentials cred = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
|
// 2 设置bucket的地域简称(参照下图)
|
ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
|
// 3 生成cos客户端
|
COSClient cosclient = new COSClient(cred, clientConfig);
|
// bucket名需包含appid
|
String bucketName = BUCKETNAME;
|
ExecutorService threadPool = Executors.newFixedThreadPool(32);
|
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
|
TransferManager transferManager = new TransferManager(cosclient, threadPool);
|
//cos文件路径
|
String key = "/img/"+pictureName;
|
//本地文件路径
|
//String fileUrl="D:\\123.png";
|
|
try {
|
File localFile =multipartFileToFile(file);
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
|
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
|
Upload upload = transferManager.upload(putObjectRequest);
|
showTransferProgress(upload);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
transferManager.shutdownNow();
|
cosclient.shutdown();
|
return URL+key;
|
}
|
/**
|
* 获取文件后缀名 不包含点
|
*/
|
public static String getFileSuffix(String fileWholeName) {
|
if (isEmpty(fileWholeName)) {
|
return "none";
|
}
|
int lastIndexOf = fileWholeName.lastIndexOf(".");
|
return fileWholeName.substring(lastIndexOf + 1);
|
}
|
/**
|
* 对象是否为空
|
*/
|
@SuppressWarnings("rawtypes")
|
public static boolean isEmpty(Object o) {
|
if (o == null) {
|
return true;
|
}
|
if (o instanceof String) {
|
if (o.toString().trim().equals("")) {
|
return true;
|
}
|
} else if (o instanceof List) {
|
if (((List) o).size() == 0) {
|
return true;
|
}
|
} else if (o instanceof Map) {
|
if (((Map) o).size() == 0) {
|
return true;
|
}
|
} else if (o instanceof Set) {
|
if (((Set) o).size() == 0) {
|
return true;
|
}
|
} else if (o instanceof Object[]) {
|
if (((Object[]) o).length == 0) {
|
return true;
|
}
|
} else if (o instanceof int[]) {
|
if (((int[]) o).length == 0) {
|
return true;
|
}
|
} else if (o instanceof long[]) {
|
if (((long[]) o).length == 0) {
|
return true;
|
}
|
}
|
return false;
|
}
|
/**
|
* MultipartFile 转 File
|
*
|
* @param file
|
* @throws Exception
|
*/
|
public static File multipartFileToFile(MultipartFile file) throws Exception {
|
|
File toFile = null;
|
if (file.equals("") || file.getSize() <= 0) {
|
file = null;
|
} else {
|
InputStream ins = null;
|
ins = file.getInputStream();
|
toFile = new File(file.getOriginalFilename());
|
inputStreamToFile(ins, toFile);
|
ins.close();
|
}
|
return toFile;
|
}
|
//获取流文件
|
private static void inputStreamToFile(InputStream ins, File file) {
|
try {
|
OutputStream os = new FileOutputStream(file);
|
int bytesRead = 0;
|
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();
|
}
|
}
|
|
private static void showTransferProgress(Transfer transfer) {
|
do {
|
try {
|
Thread.sleep(2000);
|
} catch (InterruptedException e) {
|
return;
|
}
|
} while (transfer.isDone() == false);
|
}
|
}
|