文件上传;修改pom配置

This commit is contained in:
bunny 2024-01-06 23:09:54 +08:00
parent f3ff338a23
commit 61b71ae77e
12 changed files with 293 additions and 26 deletions

View File

@ -25,7 +25,7 @@
<druid>1.2.1</druid>
<pagehelper>1.3.0</pagehelper>
<aliyun.sdk.oss>3.10.2</aliyun.sdk.oss>
<knife4j>3.0.2</knife4j>
<knife4j>3.0.3</knife4j>
<aspectj>1.9.4</aspectj>
<jjwt>0.9.1</jjwt>
<jaxb-api>2.3.1</jaxb-api>

View File

@ -115,6 +115,13 @@
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
</dependencies>
<build>

View File

@ -13,6 +13,4 @@ import java.lang.annotation.Target;
public @interface AutoFill {
// 数据库操作类型UPDATE INSERT
OperationType value();
}

View File

@ -0,0 +1,22 @@
package com.sky.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;
@Data
@Configuration
@ConfigurationProperties(prefix = "sky.minio")
public class MinioConfig {
private String endpointUrl;
private String accessKey;
private String secretKey;
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build();
}
}

View File

@ -6,8 +6,8 @@ import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@ -19,7 +19,7 @@ import java.util.List;
*/
@RestController
@RequestMapping("/admin/category")
@Tag(name = "分类相关接口")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
@ -28,12 +28,13 @@ public class CategoryController {
/**
* 新增分类
*
* @param categoryDTO CategoryDTO
* @return Result<String>
*/
@PostMapping("")
@ApiOperation("新增分类")
public Result<String> save(@RequestBody CategoryDTO categoryDTO){
public Result<String> save(@RequestBody CategoryDTO categoryDTO) {
log.info("新增分类:{}", categoryDTO);
categoryService.save(categoryDTO);
return Result.success();
@ -41,12 +42,13 @@ public class CategoryController {
/**
* 分类分页查询
*
* @param categoryPageQueryDTO CategoryPageQueryDTO
* @return Result<PageResult>
*/
@GetMapping("/page")
@ApiOperation("分类分页查询")
public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
log.info("分页查询:{}", categoryPageQueryDTO);
PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
return Result.success(pageResult);
@ -54,12 +56,13 @@ public class CategoryController {
/**
* 删除分类
*
* @param id Long
* @return Result<String>
*/
@DeleteMapping
@ApiOperation("删除分类")
public Result<String> deleteById(Long id){
public Result<String> deleteById(Long id) {
log.info("删除分类:{}", id);
categoryService.deleteById(id);
return Result.success();
@ -67,37 +70,40 @@ public class CategoryController {
/**
* 修改分类
*
* @param categoryDTO CategoryDTO
* @return Result<String>
*/
@PutMapping
@ApiOperation("修改分类")
public Result<String> update(@RequestBody CategoryDTO categoryDTO){
public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
categoryService.update(categoryDTO);
return Result.success();
}
/**
* 启用禁用分类
*
* @param status Integer
* @param id Long
* @param id Long
* @return Result<String>
*/
@PostMapping("/status/{status}")
@ApiOperation("启用禁用分类")
public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
categoryService.startOrStop(status,id);
public Result<String> startOrStop(@PathVariable("status") Integer status, Long id) {
categoryService.startOrStop(status, id);
return Result.success();
}
/**
* 根据类型查询分类
*
* @param type Integer
* @return Result<List<Category>>
* @return Result<List < Category>>
*/
@GetMapping("/list")
@ApiOperation("根据类型查询分类")
public Result<List<Category>> list(Integer type){
public Result<List<Category>> list(Integer type) {
List<Category> list = categoryService.list(type);
return Result.success(list);
}

View File

@ -0,0 +1,44 @@
package com.sky.controller.admin;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.IOException;
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
@Resource
private MinioService minioService;
/**
* 文件上传
*
* @param file MultipartFile
* @return Result<String>
*/
@ApiOperation(value = "文件上传")
@PostMapping("/upload")
public Result<String> upload(MultipartFile file) {
log.info("文件上传:{}", file);
try {
String filename = minioService.upload(file);
return Result.success(filename);
} catch (IOException e) {
log.info("文件上传失败");
return Result.error(MessageConstant.UPLOAD_FAILED);
}
}
}

View File

@ -12,9 +12,8 @@ import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@ -41,7 +40,7 @@ public class EmployeeController {
* @param employeeLoginDTO EmployeeLoginDTO
* @return Result<EmployeeLoginVO>
*/
@Operation(summary = "员工登录接口")
@ApiOperation(value = "员工登录接口")
@PostMapping("/login")
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
log.info("员工登录:{}", employeeLoginDTO);
@ -69,9 +68,9 @@ public class EmployeeController {
/**
* 退出
*
* @return
* @return Result<String>
*/
@Operation(summary = "员工退出登录")
@ApiOperation(value = "员工退出登录")
@PostMapping("/logout")
public Result<String> logout() {
return Result.success();
@ -83,7 +82,7 @@ public class EmployeeController {
* @param employeeDTO 请求新增员工参数
* @return Result
*/
@Operation(summary = "新增员工")
@ApiOperation(value = "新增员工")
@PostMapping
public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
log.info("新增员工:{}", employeeDTO);
@ -96,7 +95,7 @@ public class EmployeeController {
*
* @return Result<PageResult>
*/
@Operation(summary = "员工分页查询")
@ApiOperation(value = "员工分页查询")
@GetMapping("/page")
public Result<PageResult> pageResultResult(EmployeePageQueryDTO employeePageQueryDTO) {
log.info("员工查找,参数为:{}", employeeService);
@ -111,7 +110,7 @@ public class EmployeeController {
* @param id Long
* @return Result
*/
@Operation(summary = "启用或禁用员工账号")
@ApiOperation(value = "启用或禁用员工账号")
@PostMapping("/status/{status}")
public Result<String> startOrStop(@PathVariable Integer status, Long id) {
log.info("启用或禁用员工账号:{}{}", status, id);
@ -125,18 +124,18 @@ public class EmployeeController {
* @param id Integer
* @return Result<Employee>
*/
@Operation(summary = "根据id查询员工信息")
@ApiOperation(value = "根据id查询员工信息")
@GetMapping("/{id}")
public Result<Employee> getById(@PathVariable Integer id) {
Employee employee = employeeService.getById(id);
return Result.success(employee);
}
@Operation(summary = "编辑员工信息")
@ApiOperation(value = "编辑员工信息")
@PutMapping()
public Result update(@RequestBody EmployeeDTO employeeDTO) {
public Result<String> update(@RequestBody EmployeeDTO employeeDTO) {
log.info("编辑员工信息:{}", employeeDTO);
employeeService.update(employeeDTO);
return Result.success();
return Result.success("成功");
}
}

View File

@ -0,0 +1,16 @@
package com.sky.service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
public interface MinioService {
/**
* 上传文件
*
* @param file 文件
*/
String upload(MultipartFile file) throws IOException;
}

View File

@ -0,0 +1,37 @@
package com.sky.service.impl;
import com.sky.config.MinioConfig;
import com.sky.result.Result;
import com.sky.service.MinioService;
import com.sky.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.UUID;
@Service
@Slf4j
public class MinioServiceImpl implements MinioService {
@Resource
MinioConfig minioConfig;
@Resource
private MinioUtils minioUtils;
@Override
public String upload(MultipartFile file) throws IOException {
String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String filename = LocalDate.now() + "/" + UUID.randomUUID().toString() + extension;
log.info("上传文件:{}", filename);
minioUtils.bucketCreate(minioConfig.getBucketName());
return minioUtils
.uploadFile(minioConfig.getBucketName(),
filename,
file.getInputStream(),
file.getSize());
}
}

View File

@ -0,0 +1,122 @@
package com.sky.utils;
import com.sky.config.MinioConfig;
import io.minio.*;
import io.minio.errors.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
@Slf4j
public class MinioUtils {
@Resource
private MinioClient minioClient;
@Resource
private MinioConfig config;
/**
* 判断桶是否存在
*
* @param bucketName String
* @return found
*/
public boolean bucketExists(String bucketName) {
boolean found = false;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
}
return found;
}
/**
* 如果桶不存在就新建
*
* @param bucketName String
*/
public void bucketCreate(String bucketName) {
boolean exists = bucketExists(bucketName);
if (!exists) {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 上传文件
*
* @param fileName 文件名
* @param inputStream 输入流
* @param size 文件大小
*/
public String uploadFile(String bucketName, String fileName, InputStream inputStream, Long size) {
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(inputStream, size, -1)
.build());
return config.getEndpointUrl() + "/" + bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 上传文件
*
* @param fileName 文件名
* @param inputStream 输入流
* @param size 文件大小
* @param contentType 文件类型
*/
public String uploadFile(String bucketName, String fileName, InputStream inputStream, Long size, String contentType) {
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(inputStream, size, -1)
.contentType(contentType)
.build());
return config.getEndpointUrl() + "/" + bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取文件
*
* @param bucketName 桶名称
* @param fileName 对象名称
*/
public InputStream getFile(String bucketName, String fileName) {
try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.build());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -6,3 +6,9 @@ sky:
database: sky_take_out
username: root
password: "02120212"
minio:
endpointUrl: "http://129.211.31.58:9000"
bucket-name: sky
accessKey: "bunny"
secretKey: "02120212"

View File

@ -12,6 +12,10 @@ spring:
url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: ${sky.datasource.username}
password: ${sky.datasource.password}
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
mybatis:
#mapper配置文件
@ -37,3 +41,9 @@ sky:
admin-ttl: 66666666666666
# 设置前端传递过来的令牌名称
admin-token-name: token
minio:
endpointUrl: ${sky.minio.endpointUrl}
accessKey: ${sky.minio.accessKey}
secretKey: ${sky.minio.secretKey}
bucket-name: ${sky.minio.bucket-name}