|
@@ -1,8 +1,11 @@
|
|
|
-package com.txz.operating.config.minio;
|
|
|
+package com.txz.operating.controller;
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.txz.operating.config.minio.MinioUtil;
|
|
|
+import com.txz.operating.config.minio.SplitFileDto;
|
|
|
import com.txz.operating.result.Result;
|
|
|
import io.minio.ObjectWriteResponse;
|
|
|
import io.swagger.annotations.Api;
|
|
@@ -22,7 +25,7 @@ import java.util.List;
|
|
|
@Api(tags = "文件操作接口")
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/file")
|
|
|
-public class FileController {
|
|
|
+public class UploadController {
|
|
|
|
|
|
@Autowired
|
|
|
MinioUtil minioUtil;
|
|
@@ -31,17 +34,21 @@ public class FileController {
|
|
|
|
|
|
@ApiOperation("上传一个文件")
|
|
|
@PostMapping(value = "/upload")
|
|
|
- public Result fileUpload(@RequestParam MultipartFile file, @RequestParam String bucket,
|
|
|
- @RequestParam(required = false) String objectName) throws Exception {
|
|
|
- String dir = DateUtil.format(new Date(), NORM_DAY_PATTERN)+ "/";
|
|
|
-// String fileName = IdUtil.simpleUUID();
|
|
|
+ public Result fileUpload(@RequestParam MultipartFile file) throws Exception {
|
|
|
+ String dir = DateUtil.format(new Date(), NORM_DAY_PATTERN) + "/";
|
|
|
+ String fileName = IdUtil.simpleUUID();
|
|
|
+
|
|
|
+ // 暂时都放在img里边
|
|
|
+ String bucket = "img";
|
|
|
+ String url = "";
|
|
|
+ String objectName = "";
|
|
|
minioUtil.createBucket(bucket);
|
|
|
if (objectName != null) {
|
|
|
- minioUtil.uploadFile(file.getInputStream(), bucket, objectName + "/" + file.getOriginalFilename());
|
|
|
+ url = minioUtil.uploadFile(file.getInputStream(), bucket, dir + "/" + fileName);
|
|
|
} else {
|
|
|
- minioUtil.uploadFile(file.getInputStream(), bucket, file.getOriginalFilename());
|
|
|
+ url = minioUtil.uploadFile(file.getInputStream(), bucket, fileName);
|
|
|
}
|
|
|
- return Result.success();
|
|
|
+ return Result.success(url);
|
|
|
}
|
|
|
|
|
|
@ApiOperation("列出所有的桶")
|
|
@@ -61,14 +68,20 @@ public class FileController {
|
|
|
public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
|
|
|
HttpServletResponse response) throws Exception {
|
|
|
InputStream stream = minioUtil.download(bucket, objectName);
|
|
|
- ServletOutputStream output = response.getOutputStream();
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
|
|
- response.setContentType("application/octet-stream");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- IOUtils.copy(stream, output);
|
|
|
+ try (ServletOutputStream output = response.getOutputStream()) {
|
|
|
+ // 设置响应头
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ IOUtils.copy(stream, output);
|
|
|
+ output.flush();
|
|
|
+ } finally {
|
|
|
+ if (stream != null) {
|
|
|
+ stream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@ApiOperation("删除一个文件")
|
|
|
@GetMapping(value = "/deleteFile")
|
|
|
public Result deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
|
|
@@ -99,9 +112,8 @@ public class FileController {
|
|
|
|
|
|
@GetMapping("/getResignedObjectUrl")
|
|
|
@ApiOperation("获取一个连接以供下载")
|
|
|
- public Result getPresignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
|
|
|
-
|
|
|
- return Result.success(minioUtil.getPresignedObjectUrl(bucket, objectName, expires));
|
|
|
+ public Result getResignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
|
|
|
+ return Result.success(minioUtil.getResignedObjectUrl(bucket, objectName, expires));
|
|
|
}
|
|
|
|
|
|
@GetMapping("/listAllFile")
|