最新代码
This commit is contained in:
@@ -4,14 +4,12 @@ import com.starry.admin.common.oss.service.IOssFileService;
|
||||
import com.starry.common.result.R;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
@@ -36,11 +34,4 @@ public class CosController {
|
||||
}
|
||||
return R.error("上传照片异常,请联系管理员");
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取cos临时密钥")
|
||||
@GetMapping("/temp-key")
|
||||
public R getTempKey() throws FileNotFoundException {
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,15 @@ import java.io.InputStream;
|
||||
public interface IOssFileService {
|
||||
|
||||
/**
|
||||
* 文件上传阿里云
|
||||
* 文件上传只阿里云OSS
|
||||
*
|
||||
* @param inputStream InputStream
|
||||
* @param module String
|
||||
* @param originalFilename 文件名称
|
||||
*/
|
||||
* @param inputStream 文件流
|
||||
* @param module 文件保存模块地址
|
||||
* @param originalFilename 原始文件名
|
||||
* @return String
|
||||
* @author admin
|
||||
* @since 2024/4/11 10:24
|
||||
**/
|
||||
String upload(InputStream inputStream, String module, String originalFilename);
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,8 +17,13 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OssFileServiceImpl implements IOssFileService {
|
||||
@@ -26,6 +31,15 @@ public class OssFileServiceImpl implements IOssFileService {
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String module, String filename) {
|
||||
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
||||
bufferedInputStream.mark(0);
|
||||
String fileType = FileTypeUtil.getType(bufferedInputStream);
|
||||
try {
|
||||
bufferedInputStream.reset();
|
||||
} catch (IOException e) {
|
||||
throw new CustomException("文件上传到OSS失败");
|
||||
}
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.KEY_ID, OssProperties.KEY_SECRET);
|
||||
log.info("OSSClient实例创建成功");
|
||||
@@ -42,16 +56,21 @@ public class OssFileServiceImpl implements IOssFileService {
|
||||
// 构建日期路径:avatar/2019/02/26/文件名
|
||||
String folder = new DateTime().toString("yyyy/MM/dd");
|
||||
// 文件名:uuid.扩展名
|
||||
filename = IdUtil.fastSimpleUUID() + FileTypeUtil.getType(inputStream);
|
||||
|
||||
|
||||
filename = IdUtil.fastSimpleUUID() + "." + fileType;
|
||||
// 文件根路径
|
||||
String key = module + "/" + folder + "/" + filename;
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET_NAME, key, inputStream);
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET_NAME, key, bufferedInputStream);
|
||||
// 创建PutObject请求。
|
||||
ossClient.putObject(putObjectRequest);
|
||||
log.info("oss文件上传成功");
|
||||
// 阿里云文件绝对路径
|
||||
String endpoint = OssProperties.ENDPOINT.substring(OssProperties.ENDPOINT.lastIndexOf("//") + 2);
|
||||
String endpoint = OssProperties.ENDPOINT;
|
||||
if (OssProperties.ENDPOINT.contains("//")) {
|
||||
endpoint = OssProperties.ENDPOINT.substring(OssProperties.ENDPOINT.lastIndexOf("//") + 2);
|
||||
}
|
||||
// 返回文件的访问路径
|
||||
return "https://" + OssProperties.BUCKET_NAME + "." + endpoint + "/" + key;
|
||||
} catch (OSSException oe) {
|
||||
|
||||
Reference in New Issue
Block a user