文件功能修改

This commit is contained in:
mashuai 2025-01-17 18:57:46 +08:00
parent 8b4e9a9184
commit bfde5717d3
5 changed files with 150 additions and 59 deletions

View File

@ -1,7 +1,10 @@
package com.bonus.common.biz.domain; package com.bonus.common.biz.domain;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* 文件信息 * 文件信息
* @Author ma_sh * @Author ma_sh
@ -10,7 +13,28 @@ import lombok.Data;
@Data @Data
public class FileInfo { public class FileInfo {
/**
* 文件名称
*/
private String fileName; private String fileName;
/**
* 文件路径
*/
private String filePath; private String filePath;
/**
* 修改人
*/
private String updateBy;
/**
* 修改时间
*/
private Date updateTime;
/**
* 文件类型
*/
private String docType;
} }

View File

@ -0,0 +1,22 @@
package com.bonus.common.biz.domain;
import lombok.Data;
/**
* 返回http信息
* @Author ma_sh
* @create 2025/1/17 17:07
*/
@Data
public class HttpMsg {
/**
* 返回信息
*/
private String msg;
/**
* 返回编码
*/
private Integer code;
}

View File

@ -176,9 +176,9 @@ public class ArchivesController extends BaseController {
@PreventRepeatSubmit @PreventRepeatSubmit
//@RequiresPermissions("archives:type:download") //@RequiresPermissions("archives:type:download")
@GetMapping("/download") @GetMapping("/download")
public void download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response) public AjaxResult download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response)
{ {
archivesService.download(archivesVo, request, response); return archivesService.download(archivesVo, request, response);
} }

View File

@ -81,7 +81,7 @@ public interface ArchivesService {
* @param response * @param response
* @return * @return
*/ */
void download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response); AjaxResult download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response);
/** /**
* 更新电子档案电子签名 * 更新电子档案电子签名

View File

@ -4,9 +4,11 @@ import cn.hutool.core.collection.CollectionUtil;
import com.bonus.common.biz.config.DateTimeHelper; import com.bonus.common.biz.config.DateTimeHelper;
import com.bonus.common.biz.config.FileCompressor; import com.bonus.common.biz.config.FileCompressor;
import com.bonus.common.biz.domain.FileInfo; import com.bonus.common.biz.domain.FileInfo;
import com.bonus.common.biz.domain.HttpMsg;
import com.bonus.common.biz.domain.TreeBuild; import com.bonus.common.biz.domain.TreeBuild;
import com.bonus.common.biz.domain.TreeNode; import com.bonus.common.biz.domain.TreeNode;
import com.bonus.common.biz.enums.HttpCodeEnum; import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils; import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils; import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult; import com.bonus.common.core.web.domain.AjaxResult;
@ -29,11 +31,13 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -154,23 +158,25 @@ public class ArchivesServiceImpl implements ArchivesService {
List<ArchivesDetails> list = archivesMapper.selectDetailsList(archivesDetails); List<ArchivesDetails> list = archivesMapper.selectDetailsList(archivesDetails);
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
for (ArchivesDetails details : list) { for (ArchivesDetails details : list) {
if ((StringUtils.isNotBlank(details.getDocUrl()) && details.getDocUrl().startsWith("http")) && StringUtils.isNotBlank(details.getDocName())) { if ((StringUtils.isNotBlank(details.getDocUrl())) && StringUtils.isNotBlank(details.getDocName())) {
String originalPath = details.getDocName(); if (details.getDocUrl().startsWith("http") && details.getUpdateBy() == null) {
// 找到最后一个 '/' 字符的位置 String originalPath = details.getDocName();
int lastSlashIndex = originalPath.lastIndexOf('/'); // 找到最后一个 '/' 字符的位置
// 从最后一个 '/' 字符之后提取文件名 int lastSlashIndex = originalPath.lastIndexOf('/');
String fileName = originalPath.substring(lastSlashIndex + 1); // 从最后一个 '/' 字符之后提取文件名
// 找到 '.png' 的位置 String fileName = originalPath.substring(lastSlashIndex + 1);
int dotIndex = fileName.lastIndexOf('.'); // 找到 '.png' 的位置
// 找到倒数第二个 '_' 的位置 int dotIndex = fileName.lastIndexOf('.');
int underscoreIndex = fileName.lastIndexOf('_', dotIndex - 1); // 找到倒数第二个 '_' 的位置
// 截取文件名部分不包含后缀和多余部分 int underscoreIndex = fileName.lastIndexOf('_', dotIndex - 1);
String namePart = fileName.substring(0, underscoreIndex); // 截取文件名部分不包含后缀和多余部分
// 截取后缀部分 String namePart = fileName.substring(0, underscoreIndex);
String suffix = fileName.substring(dotIndex); // 截取后缀部分
// 拼接最终的文件名 String suffix = fileName.substring(dotIndex);
String extractedFileName = namePart + suffix; // 拼接最终的文件名
details.setDocName(extractedFileName); String extractedFileName = namePart + suffix;
details.setDocName(extractedFileName);
}
} }
} }
} }
@ -298,65 +304,93 @@ public class ArchivesServiceImpl implements ArchivesService {
* @return * @return
*/ */
@Override @Override
public void download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response) { public AjaxResult download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response) {
String zipSavePath = "D:/" + DateTimeHelper.getNowDate() + ".zip"; String zipSavePath = "D:/" + DateTimeHelper.getNowDate() + ".zip";
List<Long> idList = new ArrayList<>(); List<Long> idList = new ArrayList<>();
String[] arr = archivesVo.getIds().split(","); String[] arr = archivesVo.getIds().split(",");
Integer size = arr.length; for (String s : arr) {
for(int i = 0; i<size; i++) { idList.add(Long.valueOf(s));
idList.add(Long.valueOf(arr[i]));
} }
archivesVo.setDetailsIdList(idList); archivesVo.setDetailsIdList(idList);
// 根据id查询详情
List<ArchivesDetails> list = archivesMapper.selectDetails(archivesVo.getDetailsIdList());
if (CollectionUtils.isNotEmpty(list)) {
for (ArchivesDetails archivesDetails : list) {
if ("文件夹".equals(archivesDetails.getDocType())) {
HttpMsg httpMsg = new HttpMsg();
httpMsg.setCode(500);
httpMsg.setMsg("所选文件中包含文件夹,文件夹不能下载!");
return AjaxResult.error("所选文件中包含文件夹,文件夹不能下载!", httpMsg);
}
}
}
try { try {
File file=new File(zipSavePath); File file=new File(zipSavePath);
if(!file.isDirectory() && !file.exists()){ if(!file.isDirectory() && !file.exists()) {
file.createNewFile(); file.createNewFile();
} }
// 根据id查询详情
List<ArchivesDetails> list = archivesMapper.selectDetails(archivesVo.getDetailsIdList());
// 提取文件信息 // 提取文件信息
List<FileInfo> fileInfos = extractFileInfos(list); List<FileInfo> fileInfos = extractFileInfos(list);
if (fileInfos.isEmpty()) { if (fileInfos.isEmpty()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No files found to download."); response.sendError(HttpServletResponse.SC_NOT_FOUND, "No files found to download.");
return; return AjaxResult.error("未查询到文件信息,请检查后重新提交!");
} }
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipSavePath)); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipSavePath));
for (FileInfo fileInfo : fileInfos) { for (FileInfo fileInfo : fileInfos) {
String fileUrl = fileInfo.getFilePath(); String fileUrl = fileInfo.getFilePath();
String originalPath = fileInfo.getFileName(); String originalPath = fileInfo.getFileName();
// 找到最后一个 '/' 字符的位置 if (fileInfo.getUpdateBy() == null) {
int lastSlashIndex = originalPath.lastIndexOf('/'); // 找到最后一个 '/' 字符的位置
// 从最后一个 '/' 字符之后提取文件名 int lastSlashIndex = originalPath.lastIndexOf('/');
String fileName = originalPath.substring(lastSlashIndex + 1); // 从最后一个 '/' 字符之后提取文件名
// 找到 '.png' 的位置 String fileName = originalPath.substring(lastSlashIndex + 1);
int dotIndex = fileName.lastIndexOf('.'); // 找到 '.png' 的位置
// 找到倒数第二个 '_' 的位置 int dotIndex = fileName.lastIndexOf('.');
int underscoreIndex = fileName.lastIndexOf('_', dotIndex - 1); // 找到倒数第二个 '_' 的位置
// 截取文件名部分不包含后缀和多余部分 int underscoreIndex = fileName.lastIndexOf('_', dotIndex - 1);
String namePart = fileName.substring(0, underscoreIndex); // 截取文件名部分不包含后缀和多余部分
// 截取后缀部分 String namePart = fileName.substring(0, underscoreIndex);
String suffix = fileName.substring(dotIndex); // 截取后缀部分
// 拼接最终的文件名 String suffix = fileName.substring(dotIndex);
String extractedFileName = namePart + suffix; // 拼接最终的文件名
String savePath = "D:/" + extractedFileName; originalPath = namePart + suffix;
// 检查文件保存路径是否可写 }
if (!isValidFileExtension(originalPath)) {
// 可以根据业务需求添加默认文件类型或进行其他处理
if (originalPath.lastIndexOf('.') == -1) {
originalPath = originalPath + "." + fileInfo.getDocType();
} else {
originalPath = originalPath + fileInfo.getDocType();
}
}
String savePath = "D:/" + originalPath;
// 获取 Path 对象
Path path = Paths.get(savePath); Path path = Paths.get(savePath);
if (Files.isWritable(path.getParent())) { try {
// 下载文件并保存到本地 // 确保父目录存在如果不存在则创建
downloadFile(fileUrl, savePath); if (!Files.exists(path.getParent())) {
// 将文件添加到压缩包 Files.createDirectories(path.getParent());
fileToZip(savePath, fileName, zipOut); }
// 将临时文件删除 // 检查文件保存路径是否可写
new File(savePath).delete(); if (Files.isWritable(path.getParent())) {
} else { // 下载文件并保存到本地
System.err.println("保存文件的路径不可写: " + savePath); downloadFile(fileUrl, savePath);
// 将文件添加到压缩包
fileToZip(savePath, zipOut);
// 将临时文件删除
new File(savePath).delete();
} else {
System.err.println("保存文件的路径不可写: " + savePath);
}
} catch (IOException e) {
System.err.println("文件操作过程中出现异常: " + e.getMessage());
} }
} }
// 压缩完成后,关闭压缩流 // 压缩完成后,关闭压缩流
zipOut.close(); zipOut.close();
//拼接下载默认名称并转为ISO-8859-1格式 //拼接下载默认名称并转为ISO-8859-1格式
String fileName = new String((DateTimeHelper.getNowDate() + "下载文件.zip").getBytes(),"ISO-8859-1"); String fileName = new String((DateTimeHelper.getNowDate() + "下载文件.zip").getBytes(),"ISO-8859-1");
response.setHeader("Content-Disposition", "attchment;filename="+fileName); response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭 //该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
ServletOutputStream outputStream = response.getOutputStream(); ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(zipSavePath); FileInputStream inputStream = new FileInputStream(zipSavePath);
@ -367,11 +401,23 @@ public class ArchivesServiceImpl implements ArchivesService {
//下载完成之后删掉这个zip包 //下载完成之后删掉这个zip包
File fileTempZip = new File(zipSavePath); File fileTempZip = new File(zipSavePath);
fileTempZip.delete(); fileTempZip.delete();
return AjaxResult.success("下载成功");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return AjaxResult.error("发生内部错误,请联系管理员!");
} }
} }
/**
* 判断文件扩展名是否在允许的范围内
* @param originalPath
* @return
*/
private boolean isValidFileExtension(String originalPath) {
// 定义一个正则表达式匹配常见的文件扩展名
String regex = ".*\\.(png|pdf|jpg|jpeg|txt|docx|xlsx|xls|ppt|pptx|zip)$";
return Pattern.matches(regex, originalPath);
}
/** /**
* 下载文件 * 下载文件
@ -422,17 +468,13 @@ public class ArchivesServiceImpl implements ArchivesService {
} }
} }
public static void main(String[] args) {
System.out.println(System.getProperty("user.home") + File.separator + "test.txt");
}
/** /**
* 将文件添加到压缩包 * 将文件添加到压缩包
* @param filePath * @param filePath
* @param name
* @param zipOut * @param zipOut
* @throws IOException * @throws IOException
*/ */
public static void fileToZip(String filePath,String name,ZipOutputStream zipOut) throws IOException { public static void fileToZip(String filePath, ZipOutputStream zipOut) throws IOException {
// 需要压缩的文件 // 需要压缩的文件
File file = new File(filePath); File file = new File(filePath);
// 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName // 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName
@ -504,6 +546,9 @@ public class ArchivesServiceImpl implements ArchivesService {
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(archivesDetails.getDocName()); fileInfo.setFileName(archivesDetails.getDocName());
fileInfo.setFilePath(archivesDetails.getDocUrl()); fileInfo.setFilePath(archivesDetails.getDocUrl());
fileInfo.setUpdateBy(archivesDetails.getUpdateBy());
fileInfo.setUpdateTime(archivesDetails.getUpdateTime());
fileInfo.setDocType(archivesDetails.getDocType());
fileInfos.add(fileInfo); fileInfos.add(fileInfo);
} }
} }