109 lines
3.6 KiB
Plaintext
109 lines
3.6 KiB
Plaintext
package com.sercurityControl.decision.utils;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
import com.aliyun.oss.model.OSSObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
@Service
|
|
@Slf4j
|
|
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) {
|
|
try{
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
String objectName = IdUtil.fastSimpleUUID();
|
|
File file = MultipartFileToFile(param);
|
|
try {
|
|
ossClient.putObject(bucketName, objectName, file);
|
|
return objectName;
|
|
} catch (Exception e) {
|
|
log.error(e.toString(),e);
|
|
return null;
|
|
} finally {
|
|
assert file != null;
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
ossClient.shutdown();
|
|
}
|
|
}catch (Exception e){
|
|
log.error(e.toString(),e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
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();
|
|
} finally {
|
|
ossClient.shutdown();
|
|
if (!ObjectUtils.isEmpty(inputStream)) {
|
|
try {
|
|
inputStream.close();
|
|
} catch (IOException e) {
|
|
System.out.println("关闭流失败!");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
// try {
|
|
// InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("java.io.tmpdir") + File.separator + objectName));
|
|
// bytes = IOUtils.toByteArray(inputStream);
|
|
// } catch (IOException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
return bytes;
|
|
}
|
|
|
|
|
|
public static File MultipartFileToFile(MultipartFile multiFile) {
|
|
// 获取文件名
|
|
String fileName = multiFile.getOriginalFilename();
|
|
// 获取文件后缀
|
|
assert fileName != null;
|
|
String prefix = fileName.substring(0,fileName.lastIndexOf("."));
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
|
|
// 若须要防止生成的临时文件重复,能够在文件名后添加随机码
|
|
prefix = prefix + System.currentTimeMillis();
|
|
try {
|
|
File file = File.createTempFile(prefix, suffix);
|
|
// File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
|
|
multiFile.transferTo(file);
|
|
return file;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|