mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 12:39:05 +08:00
✨ minio 文件上传、获取 结合通过头像上传及其前端img 权限控制
This commit is contained in:
1 parent
5a159f8d45
commit
72654046a8
5 files changed
+117
-2
No files matched your search
@@ -8,6 +8,7 @@ pigx
|
||||
├ ├── pigx-common-data -- 数据相关
|
||||
├ ├── pigx-common-job -- 定时任务
|
||||
├ ├── pigx-common-log -- 日志服务
|
||||
├ ├── pigx-common-minio -- 文件系统
|
||||
├ └── pigx-common-security -- 安全工具类
|
||||
├ └── pigx-common-swagger -- Swagger Api文档生成
|
||||
├ └── pigx-common-transaction -- 分布式事务工具包
|
||||
|
||||
+7
-2
@@ -80,9 +80,14 @@ public interface CommonConstants {
|
||||
/**
|
||||
* 成功标记
|
||||
*/
|
||||
Integer SUCCESS=0;
|
||||
Integer SUCCESS = 0;
|
||||
/**
|
||||
* 失败标记
|
||||
*/
|
||||
Integer FAIL=1;
|
||||
Integer FAIL = 1;
|
||||
|
||||
/**
|
||||
* 默认存储bucket
|
||||
*/
|
||||
String BUCKET_NAME = "lengleng";
|
||||
}
|
||||
@@ -33,6 +33,13 @@ swagger:
|
||||
description: 'read all'
|
||||
- scope: 'write'
|
||||
description: 'access all'
|
||||
|
||||
# 文件系统
|
||||
minio:
|
||||
url: http://139.224.200.249:9090
|
||||
access-key: lengleng
|
||||
secret-key: lengleng
|
||||
|
||||
# Logger Config
|
||||
logging:
|
||||
level:
|
||||
|
||||
@@ -62,6 +62,12 @@
|
||||
<artifactId>pigx-common-swagger</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<!--文件系统-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-minio</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<!--eureka 客户端-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.admin.controller;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.common.minio.service.MinioTemplate;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018-12-30
|
||||
* <p>
|
||||
* 文件管理
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/file")
|
||||
public class FileController {
|
||||
private final MinioTemplate minioTemplate;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file 资源
|
||||
* @return R(bucketName, filename)
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public R upload(@RequestParam("file") MultipartFile file) {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String newName = RandomUtil.randomString(16) + originalFilename;
|
||||
|
||||
Map<String, String> resultMap = new HashMap<>(4);
|
||||
resultMap.put("bucketName", CommonConstants.BUCKET_NAME);
|
||||
resultMap.put("fileName", newName);
|
||||
|
||||
try {
|
||||
minioTemplate.putObject(CommonConstants.BUCKET_NAME, newName, file.getInputStream());
|
||||
} catch (Exception e) {
|
||||
log.error("上传失败", e);
|
||||
return R.builder().code(CommonConstants.FAIL)
|
||||
.msg(e.getLocalizedMessage()).build();
|
||||
}
|
||||
return R.builder().data(resultMap).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param fileName 文件空间/名称
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/{fileName}")
|
||||
public R file(@PathVariable String fileName, HttpServletResponse response) {
|
||||
String[] nameArray = StrUtil.split(fileName, StrUtil.DASHED);
|
||||
|
||||
try (InputStream inputStream = minioTemplate.getObject(nameArray[0], nameArray[1])) {
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(inputStream, response.getOutputStream());
|
||||
} catch (Exception e) {
|
||||
log.error("文件读取异常", e);
|
||||
return R.builder().code(CommonConstants.FAIL)
|
||||
.msg(e.getLocalizedMessage()).build();
|
||||
}
|
||||
|
||||
return R.builder().build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user