|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|