文件功能修改
This commit is contained in:
parent
8b4e9a9184
commit
bfde5717d3
|
|
@ -1,7 +1,10 @@
|
|||
package com.bonus.common.biz.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件信息
|
||||
* @Author ma_sh
|
||||
|
|
@ -10,7 +13,28 @@ import lombok.Data;
|
|||
@Data
|
||||
public class FileInfo {
|
||||
|
||||
/**
|
||||
* 文件名称
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String filePath;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private String docType;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -176,9 +176,9 @@ public class ArchivesController extends BaseController {
|
|||
@PreventRepeatSubmit
|
||||
//@RequiresPermissions("archives:type: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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ public interface ArchivesService {
|
|||
* @param response
|
||||
* @return
|
||||
*/
|
||||
void download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response);
|
||||
AjaxResult download(ArchivesVo archivesVo, HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 更新电子档案电子签名
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@ import cn.hutool.core.collection.CollectionUtil;
|
|||
import com.bonus.common.biz.config.DateTimeHelper;
|
||||
import com.bonus.common.biz.config.FileCompressor;
|
||||
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.TreeNode;
|
||||
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.StringUtils;
|
||||
import com.bonus.common.core.web.domain.AjaxResult;
|
||||
|
|
@ -29,11 +31,13 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
|
|
@ -154,7 +158,8 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
List<ArchivesDetails> list = archivesMapper.selectDetailsList(archivesDetails);
|
||||
if (CollectionUtils.isNotEmpty(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())) {
|
||||
if (details.getDocUrl().startsWith("http") && details.getUpdateBy() == null) {
|
||||
String originalPath = details.getDocName();
|
||||
// 找到最后一个 '/' 字符的位置
|
||||
int lastSlashIndex = originalPath.lastIndexOf('/');
|
||||
|
|
@ -174,6 +179,7 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
|
@ -298,32 +304,42 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
* @return
|
||||
*/
|
||||
@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";
|
||||
List<Long> idList = new ArrayList<>();
|
||||
String[] arr = archivesVo.getIds().split(",");
|
||||
Integer size = arr.length;
|
||||
for(int i = 0; i<size; i++) {
|
||||
idList.add(Long.valueOf(arr[i]));
|
||||
for (String s : arr) {
|
||||
idList.add(Long.valueOf(s));
|
||||
}
|
||||
archivesVo.setDetailsIdList(idList);
|
||||
try {
|
||||
File file=new File(zipSavePath);
|
||||
if(!file.isDirectory() && !file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
// 根据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 {
|
||||
File file=new File(zipSavePath);
|
||||
if(!file.isDirectory() && !file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
// 提取文件信息
|
||||
List<FileInfo> fileInfos = extractFileInfos(list);
|
||||
if (fileInfos.isEmpty()) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "No files found to download.");
|
||||
return;
|
||||
return AjaxResult.error("未查询到文件信息,请检查后重新提交!");
|
||||
}
|
||||
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipSavePath));
|
||||
for (FileInfo fileInfo : fileInfos) {
|
||||
String fileUrl = fileInfo.getFilePath();
|
||||
String originalPath = fileInfo.getFileName();
|
||||
if (fileInfo.getUpdateBy() == null) {
|
||||
// 找到最后一个 '/' 字符的位置
|
||||
int lastSlashIndex = originalPath.lastIndexOf('/');
|
||||
// 从最后一个 '/' 字符之后提取文件名
|
||||
|
|
@ -337,26 +353,44 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
// 截取后缀部分
|
||||
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);
|
||||
try {
|
||||
// 确保父目录存在,如果不存在则创建
|
||||
if (!Files.exists(path.getParent())) {
|
||||
Files.createDirectories(path.getParent());
|
||||
}
|
||||
// 检查文件保存路径是否可写
|
||||
if (Files.isWritable(path.getParent())) {
|
||||
// 下载文件并保存到本地
|
||||
downloadFile(fileUrl, savePath);
|
||||
// 将文件添加到压缩包
|
||||
fileToZip(savePath, fileName, zipOut);
|
||||
fileToZip(savePath, zipOut);
|
||||
// 将临时文件删除
|
||||
new File(savePath).delete();
|
||||
} else {
|
||||
System.err.println("保存文件的路径不可写: " + savePath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("文件操作过程中出现异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// 压缩完成后,关闭压缩流
|
||||
zipOut.close();
|
||||
//拼接下载默认名称并转为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();
|
||||
FileInputStream inputStream = new FileInputStream(zipSavePath);
|
||||
|
|
@ -367,11 +401,23 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
//下载完成之后,删掉这个zip包
|
||||
File fileTempZip = new File(zipSavePath);
|
||||
fileTempZip.delete();
|
||||
return AjaxResult.success("下载成功");
|
||||
} catch (IOException e) {
|
||||
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 name
|
||||
* @param zipOut
|
||||
* @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);
|
||||
// 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName
|
||||
|
|
@ -504,6 +546,9 @@ public class ArchivesServiceImpl implements ArchivesService {
|
|||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setFileName(archivesDetails.getDocName());
|
||||
fileInfo.setFilePath(archivesDetails.getDocUrl());
|
||||
fileInfo.setUpdateBy(archivesDetails.getUpdateBy());
|
||||
fileInfo.setUpdateTime(archivesDetails.getUpdateTime());
|
||||
fileInfo.setDocType(archivesDetails.getDocType());
|
||||
fileInfos.add(fileInfo);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue