IntelligentRecognition/ah-jjsp-service/.svn/pristine/76/765dff084210df8a7634d35d240...

114 lines
3.8 KiB
Plaintext
Raw Normal View History

2024-05-24 16:09:40 +08:00
package com.sercurityControl.proteam.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.securityControl.common.core.utils.StringUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
@Component
public class OssUtil {
@Value("${oss.endpoint}")
private String endpoint;
@Value("${oss.accessKeyId}")
private String accessKeyId;
@Value("${oss.accessKeySecret}")
private String accessKeySecret;
@Value("${oss.bucketName}")
private String bucketName;
public String fileUpload(MultipartFile param) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String objectName = IDUtils.createID();
File file = transferToFile(param);
try {
ossClient.putObject(bucketName, objectName, file);
return objectName;
} catch (Exception e) {
System.err.println("文件上传失败");
e.printStackTrace();
return null;
} finally {
if (file.exists()) {
file.delete();
}
ossClient.shutdown();
}
}
public byte[] getFile(String objectName) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
byte[] bytes = null;
InputStream inputStream = null;
try {
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
inputStream = ossObject.getObjectContent();
bytes = IOUtils.toByteArray(inputStream);
} catch (Exception e) {
System.out.println("下载文件失败!");
e.printStackTrace();
return null;
} finally {
ossClient.shutdown();
if (!ObjectUtils.isEmpty(inputStream)) {
try {
inputStream.close();
} catch (IOException e) {
System.out.println("关闭流失败!");
e.printStackTrace();
}
}
}
return bytes;
}
public File transferToFile(MultipartFile multipartFile) {
//选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
File file = null;
try {
file = File.createTempFile(System.currentTimeMillis() + new SecureRandom().nextInt(0x0400) + "", ".jpg");
multipartFile.transferTo(file);
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public List<String> getBase64List(String imgPath,int type){
List<String> base64List = new ArrayList<>();
if(StringUtils.isNotBlank(imgPath)){
String[] imgPathArr = imgPath.split(",");
for (String imgId : imgPathArr) {
if(getFile(imgId) !=null){
String base64 = "";
if(type == 1){ // 页面展示
base64 = "data:image/Jpeg;base64," + Base64Utils.encodeToString(getFile(imgId));
}else{ // 图片下载
base64 = Base64Utils.encodeToString(getFile(imgId));
}
base64List.add(base64);
}else{
base64List.add("");
}
}
}
return base64List;
}
}