8/7,ybb文件夹和文件都实现了记录Id功能。

This commit is contained in:
ybb 2025-08-08 15:15:26 +08:00
parent e356368b62
commit 3b3aa95437
4 changed files with 90 additions and 39 deletions

View File

@ -4,12 +4,14 @@ import com.dite.znpt.domain.entity.BusinessDataFileEntity;
import com.dite.znpt.domain.entity.BusinessDataEntity; import com.dite.znpt.domain.entity.BusinessDataEntity;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List; import java.util.List;
@ApiOperation("商务资料文件对象") @ApiOperation("商务资料文件对象")
@Mapper
public interface BusinessDataFileMapper { public interface BusinessDataFileMapper {
// 新增文件预览方法 // 新增文件预览方法

View File

@ -37,5 +37,5 @@ public interface BusinessDataFileService {
Result addfile(MultipartFile file, Long folderId); Result addfile(MultipartFile file, Long folderId);
@ApiOperation("预览文件") @ApiOperation("预览文件")
Result preview(Long fileId, HttpServletResponse response); void preview(Long fileId, HttpServletResponse response);
} }

View File

@ -255,67 +255,91 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
@ApiOperation("文件预览") @ApiOperation("文件预览")
public Result preview(Long fileId, HttpServletResponse response) { public void preview(Long fileId, HttpServletResponse response) {
try { try {
// 获取文件路径 // 获取文件路径
String filePath = businessDataFileMapper.getPath(fileId); String filePath = businessDataFileMapper.getPath(fileId);
if (filePath == null || filePath.isEmpty()) { if (filePath == null || filePath.isEmpty()) {
return Result.error("文件不存在"); response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"文件不存在\"}");
return;
} }
File file = new File(filePath); File file = new File(filePath);
if (!file.exists()) { if (!file.exists()) {
return Result.error("文件不存在"); response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"文件不存在\"}");
return;
} }
// 获取文件扩展名 // 获取文件扩展名
String extension = getFileExtension(filePath); String extension = getFileExtension(filePath);
// ... 后续 switch 语句
// 根据文件类型处理预览 // 根据文件类型处理预览
switch (extension) { switch (extension) {
case "pdf": case "pdf":
return previewPdf(file, response); previewPdf(file, response);
break;
case "jpg": case "jpg":
case "jpeg": case "jpeg":
case "png": case "png":
case "gif": case "gif":
case "bmp": case "bmp":
case "webp": case "webp":
return previewImage(file, response); previewImage(file, response);
break;
case "txt": case "txt":
case "md": case "md":
case "log": case "log":
return previewText(file, response); previewText(file, response);
break;
case "doc": case "doc":
case "docx": case "docx":
case "xls": case "xls":
case "xlsx": case "xlsx":
case "ppt": case "ppt":
case "pptx": case "pptx":
return previewOffice(file, response); previewOffice(file, response);
break;
case "mp4": case "mp4":
case "avi": case "avi":
case "mov": case "mov":
case "wmv": case "wmv":
case "flv": case "flv":
return previewVideo(file, response); previewVideo(file, response);
break;
case "mp3": case "mp3":
case "wav": case "wav":
case "flac": case "flac":
case "aac": case "aac":
return previewAudio(file, response); previewAudio(file, response);
break;
default: default:
return Result.error("暂不支持该文件类型的预览"); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"暂不支持该文件类型的预览\"}");
} catch (IOException e) {
e.printStackTrace();
}
break;
} }
} catch (Exception e) { } catch (Exception e) {
return Result.error("文件预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"文件预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }
private String getFileExtension(String fileName) { private String getFileExtension(String filePath) {
if (fileName == null || fileName.isEmpty()) { if (filePath == null || filePath.isEmpty()) {
return ""; return "";
} }
// 从路径中提取文件名
String fileName = new File(filePath).getName();
int lastDotIndex = fileName.lastIndexOf('.'); int lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex == -1) { if (lastDotIndex == -1) {
return ""; return "";
@ -323,10 +347,10 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
return fileName.substring(lastDotIndex + 1).toLowerCase(); return fileName.substring(lastDotIndex + 1).toLowerCase();
} }
private Result previewPdf(File file, HttpServletResponse response) { private void previewPdf(File file, HttpServletResponse response) {
try { try {
response.setContentType("application/pdf"); response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + file.getName()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) { OutputStream os = response.getOutputStream()) {
@ -336,17 +360,21 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
os.write(buffer, 0, bytesRead); os.write(buffer, 0, bytesRead);
} }
} }
return Result.ok("PDF预览成功");
} catch (Exception e) { } catch (Exception e) {
return Result.error("PDF预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"PDF预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }
private Result previewImage(File file, HttpServletResponse response) { private void previewImage(File file, HttpServletResponse response) {
try { try {
String contentType = getImageContentType(file.getName()); String contentType = getImageContentType(file.getName());
response.setContentType(contentType); response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline; filename=" + file.getName()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) { OutputStream os = response.getOutputStream()) {
@ -356,16 +384,20 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
os.write(buffer, 0, bytesRead); os.write(buffer, 0, bytesRead);
} }
} }
return Result.ok("图片预览成功");
} catch (Exception e) { } catch (Exception e) {
return Result.error("图片预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"图片预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }
private Result previewText(File file, HttpServletResponse response) { private void previewText(File file, HttpServletResponse response) {
try { try {
response.setContentType("text/plain; charset=UTF-8"); response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Content-Disposition", "inline; filename=" + file.getName()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) { OutputStream os = response.getOutputStream()) {
@ -375,22 +407,30 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
os.write(buffer, 0, bytesRead); os.write(buffer, 0, bytesRead);
} }
} }
return Result.ok("文本预览成功");
} catch (Exception e) { } catch (Exception e) {
return Result.error("文本预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"文本预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }
private Result previewOffice(File file, HttpServletResponse response) { private void previewOffice(File file, HttpServletResponse response) {
// Office文件预览比较复杂这里返回提示信息 try {
return Result.error("Office文件预览功能暂未实现请下载后查看"); response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"Office文件预览功能暂未实现请下载后查看\"}");
} catch (IOException e) {
e.printStackTrace();
}
} }
private Result previewVideo(File file, HttpServletResponse response) { private void previewVideo(File file, HttpServletResponse response) {
try { try {
String contentType = getVideoContentType(file.getName()); String contentType = getVideoContentType(file.getName());
response.setContentType(contentType); response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline; filename=" + file.getName()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) { OutputStream os = response.getOutputStream()) {
@ -400,17 +440,21 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
os.write(buffer, 0, bytesRead); os.write(buffer, 0, bytesRead);
} }
} }
return Result.ok("视频预览成功");
} catch (Exception e) { } catch (Exception e) {
return Result.error("视频预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"视频预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }
private Result previewAudio(File file, HttpServletResponse response) { private void previewAudio(File file, HttpServletResponse response) {
try { try {
String contentType = getAudioContentType(file.getName()); String contentType = getAudioContentType(file.getName());
response.setContentType(contentType); response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline; filename=" + file.getName()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
try (FileInputStream fis = new FileInputStream(file); try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) { OutputStream os = response.getOutputStream()) {
@ -420,9 +464,13 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
os.write(buffer, 0, bytesRead); os.write(buffer, 0, bytesRead);
} }
} }
return Result.ok("音频预览成功");
} catch (Exception e) { } catch (Exception e) {
return Result.error("音频预览失败: " + e.getMessage()); try {
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write("{\"status\":500,\"msg\":\"音频预览失败: " + e.getMessage() + "\"}");
} catch (IOException ex) {
ex.printStackTrace();
}
} }
} }

View File

@ -79,10 +79,11 @@ public class BusinessDataFileController {
return businessDataFileService.reName(fileId, newFileName); return businessDataFileService.reName(fileId, newFileName);
} }
@ApiOperation(value = "文件预览") @ApiOperation(value = "文件预览")
@GetMapping("/preview") @GetMapping("/preview")
public Result preview(@RequestParam("fileId") Long fileId, HttpServletResponse response) { public void preview(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
return businessDataFileService.preview(fileId, response); businessDataFileService.preview(fileId, response);
} }
} }