2 changed files with 0 additions and 101 deletions
@ -1,48 +0,0 @@ |
|||||
package cc.hiver.mall.controller; |
|
||||
|
|
||||
import cc.hiver.core.common.utils.ResultUtil; |
|
||||
import cc.hiver.core.common.vo.Result; |
|
||||
import cc.hiver.mall.utils.OssUtil; |
|
||||
import com.aliyun.oss.OSS; |
|
||||
import com.aliyun.oss.OSSClient; |
|
||||
import com.aliyun.oss.model.OSSObject; |
|
||||
import io.swagger.annotations.Api; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.core.io.ByteArrayResource; |
|
||||
import org.springframework.http.MediaType; |
|
||||
import org.springframework.http.ResponseEntity; |
|
||||
import org.springframework.util.ObjectUtils; |
|
||||
import org.springframework.web.bind.annotation.*; |
|
||||
import org.springframework.web.multipart.MultipartFile; |
|
||||
|
|
||||
import java.io.File; |
|
||||
import java.io.IOException; |
|
||||
import java.io.InputStream; |
|
||||
|
|
||||
/** |
|
||||
* @类描述 OSS文件传输接口 |
|
||||
* @作者 冯彬 |
|
||||
* @时间 2023/10/1-下午11:19 |
|
||||
*/ |
|
||||
@Slf4j |
|
||||
@RestController |
|
||||
@Api(tags = "文件传输接口") |
|
||||
@RequestMapping(value = "/hiver/mall/oss/") |
|
||||
public class OSSController { |
|
||||
|
|
||||
@Autowired |
|
||||
private OssUtil ossUtil; |
|
||||
|
|
||||
@PostMapping("/upload") |
|
||||
public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception { |
|
||||
if (ObjectUtils.isEmpty(file)){ |
|
||||
return ResultUtil.error("上传失败,文件为空。"); |
|
||||
} |
|
||||
String objectName = file.getOriginalFilename(); // 文件名作为对象名称
|
|
||||
InputStream inputStream = file.getInputStream(); |
|
||||
String path = ossUtil.uploadFile(objectName,inputStream); // 上传文件并返回文件URL
|
|
||||
return ResultUtil.success(path); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,53 +0,0 @@ |
|||||
package cc.hiver.mall.utils; |
|
||||
|
|
||||
import com.aliyun.oss.ClientException; |
|
||||
import com.aliyun.oss.OSS; |
|
||||
import com.aliyun.oss.OSSClientBuilder; |
|
||||
import com.aliyun.oss.OSSException; |
|
||||
import org.springframework.beans.factory.annotation.Value; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.io.File; |
|
||||
import java.io.InputStream; |
|
||||
import java.net.URL; |
|
||||
import java.util.Calendar; |
|
||||
import java.util.Date; |
|
||||
|
|
||||
/** |
|
||||
* @类描述 |
|
||||
* @作者 冯彬 |
|
||||
* @时间 2023/10/1-下午11:45 |
|
||||
*/ |
|
||||
|
|
||||
@Component |
|
||||
public class OssUtil { |
|
||||
@Value("${aliyun.oss.endpoint}") |
|
||||
private String endpoint; |
|
||||
@Value("${aliyun.oss.accessKeyId}") |
|
||||
private String accessKeyId; |
|
||||
@Value("${aliyun.oss.accessKeySecret}") |
|
||||
private String accessKeySecret; |
|
||||
@Value("${aliyun.oss.bucketName}") |
|
||||
private String bucketName; |
|
||||
|
|
||||
public OSS getOssClient() { |
|
||||
return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
||||
} |
|
||||
|
|
||||
public String uploadFile(String objectName, InputStream file) throws ClientException, OSSException { |
|
||||
OSS ossClient = getOssClient(); |
|
||||
ossClient.putObject(bucketName, objectName, file); |
|
||||
ossClient.shutdown(); |
|
||||
Date currentDate = new Date(); |
|
||||
// 创建Calendar对象并设置为当前日期
|
|
||||
Calendar calendar = Calendar.getInstance(); |
|
||||
calendar.setTime(currentDate); |
|
||||
// 添加10000天
|
|
||||
calendar.add(Calendar.DAY_OF_MONTH, 10000); |
|
||||
// 获取10000天后的日期
|
|
||||
Date expirationDate = calendar.getTime(); |
|
||||
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expirationDate); |
|
||||
String path = url.getPath(); |
|
||||
return path; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue