新增文件下载功能

release_1.0.0
xueqingkun 10 months ago
parent 4919b5b58c
commit bf843d00d2

@ -3,6 +3,7 @@ package com.supervision.knowsub.controller;
import com.supervision.knowsub.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@ -31,4 +32,12 @@ public class FileController {
public void deleteFileById(String fileId){
fileService.deleteFileById(fileId);
}
@Operation(summary = "下载文件")
@GetMapping("/downloadFile")
public void downloadFile(@RequestParam String fileId, HttpServletResponse response) {
fileService.downloadFile(fileId, response);
}
}

@ -1,7 +1,7 @@
package com.supervision.knowsub.service;
import com.supervision.knowsub.model.FileInfo;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
@ -12,4 +12,6 @@ public interface FileService {
void deleteFileById(String fileId);
List<FileInfo> listByFileId(List<String> fileIdList);
void downloadFile(String fileId, HttpServletResponse response);
}

@ -1,22 +1,26 @@
package com.supervision.knowsub.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import cn.hutool.core.util.StrUtil;
import com.supervision.knowsub.exception.BusinessException;
import com.supervision.knowsub.model.FileBlob;
import com.supervision.knowsub.model.FileInfo;
import com.supervision.knowsub.service.FileBlobService;
import com.supervision.knowsub.service.FileInfoService;
import com.supervision.knowsub.service.FileService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@ -68,4 +72,27 @@ public class FileServiceImpl implements FileService {
}
return fileInfoService.lambdaQuery().in(FileInfo::getId, fileIdList).list();
}
@Override
public void downloadFile(String fileId, HttpServletResponse response) {
Assert.notEmpty(fileId, "文件id不能为空");
FileInfo fileInfo = fileInfoService.getById(fileId);
Assert.notNull(fileInfo, "文件不存在");
FileBlob fileBlob = fileBlobService.getById(fileInfo.getFileBlobId());
Assert.notNull(fileBlob, "文件内容不存在");
if (StrUtil.isNotEmpty(fileInfo.getFileName())){
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(fileInfo.getFileName(), StandardCharsets.UTF_8));
}
try (ServletOutputStream outputStream = response.getOutputStream()){
response.setContentType("application/octet-stream");
outputStream.write(fileBlob.getBlobByte());
outputStream.flush();
}catch (Exception e){
log.error("文件下载失败",e);
throw new BusinessException("下载文件失败",e);
}
}
}

Loading…
Cancel
Save