manage:添加文件上传下载功能
parent
7456f28538
commit
905add0786
@ -0,0 +1,34 @@
|
|||||||
|
package com.supervision.manage.controller.file;
|
||||||
|
|
||||||
|
import com.supervision.manage.service.FileManageService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Api(tags = "文件管理")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/fileManage")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileManageController {
|
||||||
|
|
||||||
|
private final FileManageService fileManageService;
|
||||||
|
@ApiOperation("上传文件")
|
||||||
|
@PostMapping("/uploadFile")
|
||||||
|
public String uploadFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
|
||||||
|
|
||||||
|
return fileManageService.uploadFile(multipartFile,multipartFile.getContentType());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation("下载文件")
|
||||||
|
@GetMapping("/downloadFile")
|
||||||
|
public void downloadFile(@RequestParam String fileId, HttpServletResponse response) throws Exception {
|
||||||
|
|
||||||
|
fileManageService.downloadFile(fileId, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.supervision.manage.service;
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public interface FileManageService {
|
||||||
|
String uploadFile(MultipartFile multipartFile, String contentType) throws Exception;
|
||||||
|
|
||||||
|
void downloadFile(String fileId, HttpServletResponse response) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.supervision.manage.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.supervision.manage.service.FileManageService;
|
||||||
|
import com.supervision.model.FileResource;
|
||||||
|
import com.supervision.service.FileResourceService;
|
||||||
|
import com.supervision.util.MinioUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileManageServiceImpl implements FileManageService {
|
||||||
|
|
||||||
|
private final FileResourceService fileResourceService;
|
||||||
|
@Override
|
||||||
|
public String uploadFile(MultipartFile multipartFile, String contentType) throws Exception {
|
||||||
|
// 把文件上传到minio
|
||||||
|
String minoId = MinioUtil.uploadFile(multipartFile.getInputStream(), contentType);
|
||||||
|
|
||||||
|
//设置文件信息
|
||||||
|
FileResource fileResource = new FileResource();
|
||||||
|
fileResource.setFileName(multipartFile.getOriginalFilename());
|
||||||
|
fileResource.setFileSize(multipartFile.getSize());
|
||||||
|
fileResource.setMinioId(minoId);
|
||||||
|
fileResource.setFileType(contentType);
|
||||||
|
fileResourceService.save(fileResource);
|
||||||
|
|
||||||
|
return fileResource.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void downloadFile(String fileId, HttpServletResponse response) throws Exception {
|
||||||
|
Assert.notEmpty(fileId, "文件id不能为空");
|
||||||
|
|
||||||
|
FileResource fileResource = fileResourceService.getById(fileId);
|
||||||
|
Assert.notNull(fileResource, "文件不存在");
|
||||||
|
|
||||||
|
try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId());
|
||||||
|
ServletOutputStream outputStream = response.getOutputStream()){
|
||||||
|
if (StrUtil.isNotEmpty(fileResource.getFileType())){
|
||||||
|
response.setContentType(fileResource.getFileType());
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = inputStream.read(bytes))!= -1){
|
||||||
|
outputStream.write(bytes, 0, len);
|
||||||
|
}
|
||||||
|
outputStream.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue