SysFileController.java
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.pashanhoo.qys.controller;
import com.pashanhoo.common.Result;
import com.pashanhoo.qys.service.SysFileService;
import io.minio.GetObjectArgs;
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.messages.Item;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Api(tags = "Minio文件上传")
@RestController
@RequestMapping("/system/file")
public class SysFileController {
@Autowired
private SysFileService sysFileService;
@Autowired
private MinioClient minioClient;
/**
* 图片上传minio
*
* @param file 图片文件
* @return 返回
*/
@ApiOperation("点选文件-文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Result uploadFileMinio(MultipartFile file) {
String url = sysFileService.uploadFileMinio(file);
if (!StringUtils.isEmpty(url)) {
return Result.ok(url);
}
return Result.error(-1,"上传失败");
}
@PostMapping(value = "/upload-files" )
@ApiOperation(value = "文件上传(支持批量)", notes = "文件上传(支持批量)")
public Result uploadFiles(@RequestPart MultipartFile[] files ) throws IOException {
String url = sysFileService.uploadFileMinio(files);
if (!StringUtils.isEmpty(url)) {
return Result.ok(url);
}
return Result.error(-1,"上传失败");
}
// 暂未实现
@ApiOperation("文件下载")
@RequestMapping(value = "/download", method = RequestMethod.POST)
public String download(HttpServletResponse response) {
try {
Iterable<io.minio.Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket("eci-bucket").build()//获取bucket里所有文件信息
);
String fileName = null;
for (io.minio.Result<Item> result : results) {
Item item = result.get();
fileName = item.objectName();
System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
}
InputStream object = minioClient.getObject(GetObjectArgs.builder().bucket("eci-bucket").object(fileName).build());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
IOUtils.copy(object, bos);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
bos.flush();
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}