forked from Q-1515/reggie_parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
reggie_server/src/main/java/com/reggie/config/OSSConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.reggie.config; | ||
|
||
import com.reggie.properties.AliOSSProperties; | ||
import com.reggie.utils.AliOSSUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* 对象存储配置类 | ||
*/ | ||
@Configuration | ||
@Slf4j | ||
public class OSSConfiguration { | ||
|
||
/** | ||
* 创建对象存储工具类 | ||
* | ||
* @param aliOSSProperties 对象存储参数注入 | ||
* @return 对象存储 | ||
*/ | ||
@Bean | ||
public AliOSSUtil aliOSSUtil(AliOSSProperties aliOSSProperties) { | ||
return AliOSSUtil.builder() | ||
.endpoint(aliOSSProperties.getEndpoint()) | ||
.accessKeyId(aliOSSProperties.getAccessKeyId()) | ||
.accessKeySecret(aliOSSProperties.getAccessKeySecret()) | ||
.bucketName(aliOSSProperties.getBucketName()) | ||
.build(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
reggie_server/src/main/java/com/reggie/controller/admin/CommonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.reggie.controller.admin; | ||
|
||
import com.reggie.constant.MessageConstant; | ||
import com.reggie.result.R; | ||
import com.reggie.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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.util.UUID; | ||
|
||
/** | ||
* 通用接口 | ||
*/ | ||
@RestController | ||
@RequestMapping("/admin/common") | ||
@Slf4j | ||
@Api(tags = "通用接口") | ||
public class CommonController { | ||
@Autowired | ||
private AliOSSUtil aliOSSUtil; //对象存储工具类注入 | ||
|
||
|
||
@PostMapping("/upload") | ||
@ApiOperation("文件上传") | ||
public R<String> upload(@RequestParam("file") MultipartFile file) { | ||
log.info("文件上传:{}", file); | ||
String fileURL = null; | ||
|
||
try { | ||
//通过图片流读取进行图片校验 | ||
ImageIO.read(file.getInputStream()); | ||
|
||
//获取原文件名 | ||
String originalFilename = file.getOriginalFilename(); | ||
|
||
//生成唯一文件名 | ||
String fileName = UUID.randomUUID().toString() + originalFilename; | ||
|
||
//存入阿里云oss对象存储返回URL | ||
fileURL = aliOSSUtil.upload(file.getBytes(), fileName); | ||
} catch (Exception e) { | ||
log.info("图片保存失败"); | ||
return R.error(MessageConstant.UPLOAD_FAILED); | ||
} | ||
return R.success(fileURL); | ||
} | ||
|
||
|
||
} |