|
|
|
@@ -10,11 +10,16 @@ import com.aliyun.oss.model.PutObjectRequest; |
|
|
|
import com.aliyun.oss.model.PutObjectResult; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.file.aliyun.bean.AliyunOSSConfig; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.io.BufferedOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
@@ -28,6 +33,21 @@ public class AliyunOSS { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AliyunOSSConfig aliyunOSSConfig; |
|
|
|
|
|
|
|
public ResultData uploadFile(String folder, String suffix,byte[] fileBytes){ |
|
|
|
String upload = upload(folder,suffix, fileBytes); |
|
|
|
ResultData data; |
|
|
|
if(StringUtils.isNotBlank(upload)){ |
|
|
|
data = new ResultData(); |
|
|
|
Map<String, String> map = new HashMap<>(); |
|
|
|
map.put("url", upload); |
|
|
|
map.put("bucketName", aliyunOSSConfig.getBucketname()); |
|
|
|
data.data = map; |
|
|
|
}else{ |
|
|
|
data = new ResultData(ResultData.ERROR,"上传失败"); |
|
|
|
} |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
public ResultData uploadFile(String folder, String suffix,InputStream inputStream){ |
|
|
|
String upload = upload(folder,suffix, inputStream); |
|
|
|
@@ -103,5 +123,84 @@ public class AliyunOSS { |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 上传 |
|
|
|
* @param inputStream |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String upload(String folder, String suffix,byte[] fileBytes){ |
|
|
|
log.info("=========>OSS文件上传开始:"); |
|
|
|
String endpoint = aliyunOSSConfig.getEndpoint(); |
|
|
|
String accessKeyId = aliyunOSSConfig.getKeyid(); |
|
|
|
String accessKeySecret = aliyunOSSConfig.getKeysecret(); |
|
|
|
String bucketName = aliyunOSSConfig.getBucketname(); |
|
|
|
String fileHost = aliyunOSSConfig.getFilehost(); |
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
String dateStr = format.format(new Date()); |
|
|
|
|
|
|
|
if(null == fileBytes){ |
|
|
|
return null; |
|
|
|
} |
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret); |
|
|
|
try { |
|
|
|
//容器不存在,就创建 |
|
|
|
if(! ossClient.doesBucketExist(bucketName)){ |
|
|
|
ossClient.createBucket(bucketName); |
|
|
|
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); |
|
|
|
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead); |
|
|
|
ossClient.createBucket(createBucketRequest); |
|
|
|
} |
|
|
|
if(StringUtils.isNotBlank(folder)){ |
|
|
|
folder = folder + "/"; |
|
|
|
}else{ |
|
|
|
folder = ""; |
|
|
|
} |
|
|
|
//创建文件路径 |
|
|
|
String fileUrl = folder + fileHost + "/" + dateStr + "/" |
|
|
|
+ UUID.randomUUID().toString().replace("-","") + suffix; |
|
|
|
File file = saveBinaryFile(fileBytes,fileUrl); |
|
|
|
if (null == file) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
//上传文件 |
|
|
|
PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, file)); |
|
|
|
//设置权限 这里是公开读 |
|
|
|
ossClient.setBucketAcl(bucketName,CannedAccessControlList.PublicRead); |
|
|
|
if(null != result){ |
|
|
|
log.info("==========>OSS文件上传成功,OSS地址:"+fileUrl); |
|
|
|
//JPG的加上压缩 |
|
|
|
if (suffix.equals(".JPG") || suffix.equals(".jpg")) { |
|
|
|
return aliyunOSSConfig.getFiledomain() + "/" + fileUrl+"?x-oss-process=image/resize,w_10000/quality,q_60"; |
|
|
|
}else { |
|
|
|
return aliyunOSSConfig.getFiledomain() + "/" + fileUrl; |
|
|
|
} |
|
|
|
} |
|
|
|
}catch (OSSException oe){ |
|
|
|
log.error(oe.getMessage()); |
|
|
|
}catch (ClientException ce){ |
|
|
|
log.error(ce.getMessage()); |
|
|
|
}finally { |
|
|
|
//关闭 |
|
|
|
ossClient.shutdown(); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private static File saveBinaryFile(byte[] data, String path) { |
|
|
|
File file = null; |
|
|
|
try { |
|
|
|
file = new File(path); |
|
|
|
BufferedOutputStream bos = new BufferedOutputStream( |
|
|
|
new FileOutputStream(file)); |
|
|
|
bos.write(data); |
|
|
|
bos.flush(); |
|
|
|
bos.close(); |
|
|
|
} catch (IOException ioe) { |
|
|
|
file = null; |
|
|
|
log.error("saveBinaryFile error.",ioe); |
|
|
|
} |
|
|
|
return file; |
|
|
|
} |
|
|
|
|
|
|
|
} |