|
@@ -0,0 +1,96 @@
|
|
|
+package com.tofly.controller;
|
|
|
+
|
|
|
+import com.tofly.client.MinioFileClient;
|
|
|
+import com.tofly.client.MinioFileUploadClient;
|
|
|
+import com.tofly.common.core.result.ResultData;
|
|
|
+import com.tofly.vo.FileInfo;
|
|
|
+import com.tofly.vo.FileUploadResponse;
|
|
|
+import feign.Response;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("testupload")
|
|
|
+public class TestController {
|
|
|
+@Autowired
|
|
|
+private MinioFileClient minioFileClient;
|
|
|
+
|
|
|
+ @RequestMapping("/getFile")
|
|
|
+ public ResultData test2(){
|
|
|
+ final FileInfo byFileId = minioFileClient.getByFileId("1");
|
|
|
+ return new ResultData().ok(byFileId);
|
|
|
+ }
|
|
|
+ @PostMapping("/uploadFile")
|
|
|
+ public ResultData test3(@RequestPart("file") MultipartFile file){
|
|
|
+ final FileUploadResponse test = minioFileClient.uploadFile(file);
|
|
|
+ return new ResultData().ok(test);
|
|
|
+ }
|
|
|
+ @PostMapping("/uploadZip")
|
|
|
+ public ResultData testuploadzip(@RequestPart("file") MultipartFile file){
|
|
|
+ final ResponseEntity<List<FileInfo>> listResponseEntity = minioFileClient.uploadZip( file);
|
|
|
+ return new ResultData().ok(listResponseEntity.getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/downZip")
|
|
|
+ public void testDownloadzip(String[] fileNames, HttpServletResponse response){
|
|
|
+ final ResponseEntity<byte[]> responseEntity = minioFileClient.downloadZip(fileNames);
|
|
|
+ final byte[] zipContent = responseEntity.getBody();
|
|
|
+
|
|
|
+ if (zipContent != null) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
+ response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=downloaded_files.zip");
|
|
|
+ response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
+ response.setContentLength(zipContent.length);
|
|
|
+ try {
|
|
|
+ response.getOutputStream().write(zipContent);
|
|
|
+ response.getOutputStream().flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/getFileInfo")
|
|
|
+ public ResultData test3(String fileName){
|
|
|
+ final FileInfo fileInfo = minioFileClient.getFileInfo(fileName);
|
|
|
+
|
|
|
+ return new ResultData().ok(fileInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/download")
|
|
|
+ public void test3(String fileName, HttpServletResponse response){
|
|
|
+ Response feignResponse = minioFileClient.download(fileName);
|
|
|
+
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
|
|
+
|
|
|
+ try (InputStream inputStream = feignResponse.body().asInputStream();
|
|
|
+ OutputStream outputStream = response.getOutputStream()) {
|
|
|
+
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ int bytesRead;
|
|
|
+
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ // Handle exception
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|