AliOss图片上传
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package com.sky.config;
|
||||||
|
|
||||||
|
import com.sky.properties.AliOssProperties;
|
||||||
|
import com.sky.utils.AliOssUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AliOss配置类
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
|
public class OSSConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {
|
||||||
|
log.info("创建AliOss上传工具类: {}",aliOssProperties);
|
||||||
|
return new AliOssUtil(aliOssProperties.getEndpoint(),
|
||||||
|
aliOssProperties.getAccessKeyId(),
|
||||||
|
aliOssProperties.getAccessKeySecret(),
|
||||||
|
aliOssProperties.getBucketName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.sky.controller.admin;
|
||||||
|
|
||||||
|
import com.sky.result.Result;
|
||||||
|
import com.sky.utils.AliOssUtil;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/admin/common")
|
||||||
|
@Slf4j
|
||||||
|
@Api(tags = "通用接口")
|
||||||
|
public class CommonController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AliOssUtil aliOssUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/upload")
|
||||||
|
@ApiOperation("文件上传")
|
||||||
|
public Result<String> upload(MultipartFile file) {
|
||||||
|
log.info("文件上传: {}", file);
|
||||||
|
try {
|
||||||
|
int index = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf(".");
|
||||||
|
String lastName = file.getOriginalFilename().substring(index);
|
||||||
|
String filePath = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString() + lastName);
|
||||||
|
return Result.success(filePath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("文件上传失败: " + e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user