1. 文件下载无法打开bug

This commit is contained in:
zhaowenyuan 2024-06-29 10:38:55 +08:00
parent 4bd86c2e0e
commit 3c28d43fe4
5 changed files with 77 additions and 29 deletions

View File

@ -39,7 +39,7 @@ public class FileController {
*/ */
@SysLog(value = "文件上传", businessType = BusinessType.INSERT) @SysLog(value = "文件上传", businessType = BusinessType.INSERT)
@PostMapping(value = "/upload") @PostMapping(value = "/upload")
@RequiresPermissions("system:file:upload") // @RequiresPermissions("system:file:upload")
public AjaxResult uploadFile(@RequestParam(value = "files") MultipartFile[] files, public AjaxResult uploadFile(@RequestParam(value = "files") MultipartFile[] files,
@RequestParam(value = "ocrType", required = false) String ocrType, @RequestParam(value = "ocrType", required = false) String ocrType,
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag, @RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
@ -52,7 +52,7 @@ public class FileController {
/** /**
* 合同上传 * 合同上传
*/ */
@SysLog(value = "合同上传", businessType = BusinessType.INSERT) @SysLog(value = "合同", businessType = BusinessType.UPLOAD)
@PostMapping(value = "/uploadContract") @PostMapping(value = "/uploadContract")
@RequiresPermissions("system:file:upload") @RequiresPermissions("system:file:upload")
public AjaxResult uploadContract(@RequestParam(value = "file") MultipartFile file) throws Exception public AjaxResult uploadContract(@RequestParam(value = "file") MultipartFile file) throws Exception
@ -60,8 +60,11 @@ public class FileController {
return AjaxResult.success(fileSourceService.uploadContract(file)); return AjaxResult.success(fileSourceService.uploadContract(file));
} }
/**
@SysLog(value = "文件预览", businessType = BusinessType.OTHER) * 文件预览
* @param fid 文件id
*/
@SysLog(value = "文件", businessType = BusinessType.PREVIEW)
@GetMapping(value="/{fid}/preview") @GetMapping(value="/{fid}/preview")
@RequiresPermissions("system:file:preview") @RequiresPermissions("system:file:preview")
public void preview(@PathVariable String fid, HttpServletResponse response) public void preview(@PathVariable String fid, HttpServletResponse response)
@ -70,14 +73,22 @@ public class FileController {
fileSourceService.getFileStream(fid, response, true); fileSourceService.getFileStream(fid, response, true);
} }
@SysLog(value = "文件下载", businessType = BusinessType.OTHER) /**
* 文件下载
* @param fid 文件id
*/
@SysLog(value = "文件下载", businessType = BusinessType.DOWNLOAD)
@GetMapping(value = "/download/{fid}") @GetMapping(value = "/download/{fid}")
@RequiresPermissions("system:file:download") // @RequiresPermissions("system:file:download")
public void downloadFile(@PathVariable String fid, HttpServletResponse response) { public void downloadFile(@PathVariable String fid, HttpServletResponse response) {
log.info("[OSS]download Request --> param:{}",fid); log.info("[OSS]download Request --> param:{}",fid);
fileSourceService.getFileStream(fid, response, false); fileSourceService.getFileStream(fid, response, false);
} }
/**
* 删除文件
* @param fid 文件id
*/
@SysLog(value = "文件", businessType = BusinessType.DELETE) @SysLog(value = "文件", businessType = BusinessType.DELETE)
@DeleteMapping(value = "/del/{fid}") @DeleteMapping(value = "/del/{fid}")
@RequiresPermissions("system:file:del") @RequiresPermissions("system:file:del")
@ -86,6 +97,10 @@ public class FileController {
return AjaxResult.success(); return AjaxResult.success();
} }
/**
* 删除文件
* @param fids 文件fid
*/
@SysLog(value = "批量文件", businessType = BusinessType.DELETE) @SysLog(value = "批量文件", businessType = BusinessType.DELETE)
@PostMapping(value = "/del/batch") @PostMapping(value = "/del/batch")
@RequiresPermissions("system:file:del") @RequiresPermissions("system:file:del")

View File

@ -40,6 +40,8 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -100,7 +102,7 @@ public class FileSourceService
info.setName(fName); info.setName(fName);
// mimeType 还不是 后缀 类型 // mimeType 还不是 后缀 类型
info.setMimeType(mimetype); info.setMimeType(mimetype);
info.setSize(file.getSize()); info.setSize((long) file.getBytes().length);
String md5 = FileUtils.getMd5(file.getInputStream()); String md5 = FileUtils.getMd5(file.getInputStream());
info.setMd5(md5); info.setMd5(md5);
String path = ossService.getPrefixPath(md5,fName); String path = ossService.getPrefixPath(md5,fName);
@ -150,26 +152,34 @@ public class FileSourceService
return result; return result;
} }
public void getFileStream(String fid, HttpServletResponse response, boolean convertText) {
public void getFileStream(String fid, HttpServletResponse response, boolean convertText)
{
// 文件信息 // 文件信息
FileSourceInfo info = getOne(new LambdaQueryWrapper<FileSourceInfo>() FileSourceInfo info = getOne(new LambdaQueryWrapper<FileSourceInfo>()
.eq(FileSourceInfo::getFid, fid) .eq(FileSourceInfo::getFid, fid)
.last(Operator.LIMIT_ONE.getCharacter())); .last(Operator.LIMIT_ONE.getCharacter()));
if (ObjUtil.isEmpty(info)) return; if (ObjUtil.isEmpty(info)) return;
InputStream fileStream = ossService.getFileStream(info); // InputStream fileStream = ossService.getFileStream(info);
byte[] fileBytes = ossService.getFileBytes(info);
response.setContentType(info.getMimeType()); response.setContentType(info.getMimeType());
try (OutputStream os = response.getOutputStream()){ try (OutputStream os = response.getOutputStream()){
if (FileUtils.isText(fileStream) && convertText){ response.addHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(info.getName(), StandardCharsets.UTF_8));
response.addHeader("Content-Length", "" + fileBytes.length);
response.setContentType("application/octet-stream");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytes);
if (FileUtils.isText(byteArrayInputStream) && convertText){
//是文本则读取内容输出 //是文本则读取内容输出
OutputStreamWriter osw = new OutputStreamWriter(os); OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(IOUtils.toString(fileStream, FileUtils.getStreamCharset(fileStream))); osw.write(IOUtils.toString(byteArrayInputStream, FileUtils.getStreamCharset(byteArrayInputStream)));
osw.flush(); osw.flush();
}else { }else {
byte[] readByte = new byte[1024]; // byte[] readByte = new byte[10240];
int len = 0; // int len = 0;
while ((len = fileStream.read(readByte, 0, readByte.length)) != -1) { // while((len = fileStream.read(readByte, 0, readByte.length)) != -1) {
os.write(readByte, 0, len); // os.write(readByte,0,len);
} // }
os.write(fileBytes);
os.flush(); os.flush();
} }
}catch (Exception e){ }catch (Exception e){
@ -178,7 +188,8 @@ public class FileSourceService
} }
@Transactional @Transactional
public void delFile(String fid) { public void delFile(String fid)
{
// 文件信息 // 文件信息
FileSourceInfo info = getOne(new LambdaQueryWrapper<FileSourceInfo>() FileSourceInfo info = getOne(new LambdaQueryWrapper<FileSourceInfo>()
.eq(FileSourceInfo::getFid, fid) .eq(FileSourceInfo::getFid, fid)
@ -189,7 +200,8 @@ public class FileSourceService
} }
@Transactional @Transactional
public String delFileBatch(List<String> fids) { public String delFileBatch(List<String> fids)
{
// 文件信息 // 文件信息
List<FileSourceInfo> list = list(new LambdaQueryWrapper<FileSourceInfo>() List<FileSourceInfo> list = list(new LambdaQueryWrapper<FileSourceInfo>()
.in(FileSourceInfo::getFid, fids)); .in(FileSourceInfo::getFid, fids));
@ -205,8 +217,8 @@ public class FileSourceService
return "success"; return "success";
} }
public String uploadQcCode() throws Exception
public String uploadQcCode() throws Exception{ {
String url = StrUtil.format("{}?userId={}&channelId={}&channelName={}&" + String url = StrUtil.format("{}?userId={}&channelId={}&channelName={}&" +
@ -222,26 +234,27 @@ public class FileSourceService
String ip = IPUtils.clientIp(ServletUtils.getRequest()); String ip = IPUtils.clientIp(ServletUtils.getRequest());
String fid = generateFid(); String fid = generateFid();
String fName = file.getName(); String fName = file.getName();
long length = file.length();
String md5 = FileUtils.getMd5(fileInputStream); String md5 = FileUtils.getMd5(fileInputStream);
// image/jpeg mimeType // image/jpeg mimeType
FileSourceInfo fileSourceInfo = new FileSourceInfo(); FileSourceInfo fileSourceInfo = new FileSourceInfo();
fileSourceInfo.setUploadIp(ip); fileSourceInfo.setUploadIp(ip);
fileSourceInfo.setFid(fid); fileSourceInfo.setFid(fid);
fileSourceInfo.setName(fName); fileSourceInfo.setName(fName);
fileSourceInfo.setSize(length); fileSourceInfo.setSize((long) fileInputStream.available());
fileSourceInfo.setPath("qcCode/"+fName); fileSourceInfo.setPath("qcCode/"+fName);
fileSourceInfo.setMd5(md5); fileSourceInfo.setMd5(md5);
fileSourceInfo.setRealPath(url); fileSourceInfo.setRealPath(url);
fileSourceInfo.setMimeType("image/jpeg"); fileSourceInfo.setMimeType("image/jpeg");
fileSourceInfo.setStorage(uploadConfig.getStorage());
String upload = ossService.upload(new FileInputStream(file), fileSourceInfo); String upload = ossService.upload(new FileInputStream(file), fileSourceInfo);
fileSourceInfo.setRealPath(upload); fileSourceInfo.setRealPath(upload);
save(fileSourceInfo); save(fileSourceInfo);
FileUtil.del(file);
return upload; return upload;
} }
public String uploadContract(MultipartFile file) throws Exception
public String uploadContract(MultipartFile file) throws Exception{ {
String ip = IPUtils.clientIp(ServletUtils.getRequest()); String ip = IPUtils.clientIp(ServletUtils.getRequest());
String fid = generateFid(); String fid = generateFid();
String fName = file.getOriginalFilename(); String fName = file.getOriginalFilename();
@ -265,9 +278,9 @@ public class FileSourceService
log.error("文件后缀识别失败"); log.error("文件后缀识别失败");
} }
fileSourceInfo.setName(fName); fileSourceInfo.setName(fName);
fileSourceInfo.setSize(length); fileSourceInfo.setSize((long) file.getBytes().length);
fileSourceInfo.setPath("contract/"+fName); fileSourceInfo.setPath("contract/"+fName);
fileSourceInfo.setStorage("contract/"+fName); fileSourceInfo.setStorage(uploadConfig.getStorage());
fileSourceInfo.setMimeType(mimetype); fileSourceInfo.setMimeType(mimetype);
String upload = ossService.upload(file.getInputStream(), fileSourceInfo); String upload = ossService.upload(file.getInputStream(), fileSourceInfo);
fileSourceInfo.setRealPath(upload); fileSourceInfo.setRealPath(upload);
@ -328,9 +341,8 @@ public class FileSourceService
return vo; return vo;
} }
public List<FileSourceVo> uploadFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType)
{
public List<FileSourceVo> uploadFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType) {
return Collections.singletonList(addFile(bytes, fileName, sealFlag, formats, fileType)); return Collections.singletonList(addFile(bytes, fileName, sealFlag, formats, fileType));
} }
} }

View File

@ -57,4 +57,6 @@ public interface OssService {
InputStream getFileStream(FileSourceInfo info); InputStream getFileStream(FileSourceInfo info);
byte[] getFileBytes(FileSourceInfo info);
} }

View File

@ -121,5 +121,10 @@ public class AliServiceImpl implements OssService {
return object.getObjectContent(); return object.getObjectContent();
} }
@Override
public byte[] getFileBytes(FileSourceInfo info) {
return new byte[0];
}
} }

View File

@ -217,4 +217,18 @@ public class MinioServiceImpl implements OssService {
throw new ResultException("获取文件失败"); throw new ResultException("获取文件失败");
} }
} }
@Override
public byte[] getFileBytes(FileSourceInfo info) {
try {
GetObjectResponse object = minio.getObject(GetObjectArgs.builder()
.bucket(config.getBucketName())
.object(info.getPath())
.build());
return object.readAllBytes();
} catch (Exception e) {
log.error("获取minio object error", e);
throw new ResultException("获取文件失败");
}
}
} }