✨ 添加新特性. 支持前端文件上传,返回link

This commit is contained in:
冷冷 committed 2019-10-19 14:00:57 +08:00
1 parent 4c50a6ec11
commit 89ec22dbab
3 files changed
+15 -11

No files matched your search

@@ -23,6 +23,7 @@ import com.pig4cloud.pigx.admin.api.entity.SysFile;
import com.pig4cloud.pigx.admin.service.SysFileService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
@@ -79,7 +80,7 @@ public class SysFileController {
* 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
*
* @param file 资源
* @return R(bucketName, filename)
* @return R(/ admin / bucketName / filename)
*/
@PostMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file) {
@@ -89,13 +90,15 @@ public class SysFileController {
/**
* 获取文件
*
* @param bucket 桶名称
* @param fileName 文件空间/名称
* @param response
* @return
*/
@GetMapping("/{fileName}")
public void file(@PathVariable String fileName, HttpServletResponse response) {
sysFileService.getFile(fileName, response);
@Inner(false)
@GetMapping("/{bucket}/{fileName}")
public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) {
sysFileService.getFile(bucket, fileName, response);
}
}
@@ -43,10 +43,11 @@ public interface SysFileService extends IService<SysFile> {
/**
* 读取文件
*
* @param fileName
* @param response
* @param bucket 桶名称
* @param fileName 文件名称
* @param response 输出流
*/
void getFile(String fileName, HttpServletResponse response);
void getFile(String bucket, String fileName, HttpServletResponse response);
/**
* 删除文件
@@ -65,6 +65,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
Map<String, String> resultMap = new HashMap<>(4);
resultMap.put("bucketName", CommonConstants.BUCKET_NAME);
resultMap.put("fileName", fileName);
resultMap.put("url", String.format("/admin/sys-file/%s/%s", CommonConstants.BUCKET_NAME, fileName));
try {
minioTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());
@@ -80,14 +81,13 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
/**
* 读取文件
*
* @param bucket
* @param fileName
* @param response
*/
@Override
public void getFile(String fileName, HttpServletResponse response) {
int separator = fileName.lastIndexOf(StrUtil.DASHED);
try (InputStream inputStream = minioTemplate.getObject(fileName.substring(0, separator),
fileName.substring(separator + 1))) {
public void getFile(String bucket, String fileName, HttpServletResponse response) {
try (InputStream inputStream = minioTemplate.getObject(bucket, fileName)) {
response.setContentType("application/octet-stream; charset=UTF-8");
IoUtil.copy(inputStream, response.getOutputStream());
} catch (Exception e) {