模板下载公共方法
This commit is contained in:
parent
a39fd4c68a
commit
7e671ad55e
|
|
@ -0,0 +1,32 @@
|
|||
package com.bonus.web.controller.common;
|
||||
|
||||
import com.bonus.web.service.common.CommonDownloadService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @className:CommonDownloadModel
|
||||
* @author:cwchen
|
||||
* @date:2025-10-31-14:19
|
||||
* @version:1.0
|
||||
* @description:下载模板的公共方法
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/commonDownload")
|
||||
public class CommonDownloadModel {
|
||||
|
||||
@Resource(name = "CommonDownloadService")
|
||||
private CommonDownloadService service;
|
||||
|
||||
@ApiOperation(value = "下载工器具模板", notes = "下载工器具模板")
|
||||
@GetMapping("downLoadToolModel")
|
||||
public void downLoadToolModel(HttpServletRequest request, HttpServletResponse response) {
|
||||
service.downLoadToolModel(request,response);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package com.bonus.web.service.common;
|
||||
|
||||
import com.bonus.common.constant.ModelConstants;
|
||||
import com.bonus.mainDataBase.service.impl.DownloadModelService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @className:CommonDownloadService
|
||||
* @author:cwchen
|
||||
* @date:2025-10-31-14:21
|
||||
* @version:1.0
|
||||
* @description:下载模板公共方法-业务层
|
||||
*/
|
||||
@Service("CommonDownloadService")
|
||||
@Slf4j
|
||||
public class CommonDownloadService {
|
||||
|
||||
@Resource(name = "DownloadModelService")
|
||||
private DownloadModelService downloadModelService;
|
||||
|
||||
/**
|
||||
* 下载工器具模板
|
||||
* @param request
|
||||
* @param response
|
||||
* @return void
|
||||
* @author cwchen
|
||||
* @date 2025/10/31 14:25
|
||||
*/
|
||||
public void downLoadToolModel(HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadModelService.downLoadToolModel(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.bonus.common.constant;
|
||||
|
||||
/**
|
||||
* @className:ModelConstants
|
||||
* @author:cwchen
|
||||
* @date:2025-10-31-14:27
|
||||
* @version:1.0
|
||||
* @description:模板常量
|
||||
*/
|
||||
public class ModelConstants {
|
||||
|
||||
/**工器具模板*/
|
||||
public static final String TOOL_MODEL = "download/tool_model.xlsx";
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
package com.bonus.common.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @className:DownloadModelUtil
|
||||
* @author:cwchen
|
||||
* @date:2025-10-31-15:39
|
||||
* @version:1.0
|
||||
* @description:
|
||||
*/
|
||||
@Component(value = "DownloadModelUtil")
|
||||
@Slf4j
|
||||
public class DownloadModelUtil {
|
||||
|
||||
public void commonDownLoadExcelModel(HttpServletResponse response, String fileName) {
|
||||
// 参数校验
|
||||
if (response == null || fileName == null || fileName.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("Response and fileName must not be null or empty");
|
||||
}
|
||||
|
||||
String path = fileName.trim();
|
||||
|
||||
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
|
||||
ServletOutputStream servletOutputStream = response.getOutputStream()) {
|
||||
|
||||
// 检查文件是否存在
|
||||
if (inputStream == null) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
log.error("File not found: {}", path);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
setupResponseHeaders(response, fileName);
|
||||
|
||||
// 拷贝数据
|
||||
IOUtils.copy(inputStream, servletOutputStream);
|
||||
response.flushBuffer();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("File download failed: {}", e.getMessage(), e);
|
||||
handleDownloadException(response, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置下载响应头
|
||||
*/
|
||||
private void setupResponseHeaders(HttpServletResponse response, String fileName) throws IOException {
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
|
||||
// 缓存控制
|
||||
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
response.setHeader("Pragma", "no-cache");
|
||||
response.setDateHeader("Expires", 0);
|
||||
|
||||
// 文件名编码
|
||||
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())
|
||||
.replaceAll("\\+", "%20"); // 替换+号为%20,确保兼容性
|
||||
|
||||
// 内容处置头
|
||||
String contentDisposition = String.format("attachment; filename=\"%s\"; filename*=utf-8''%s",
|
||||
encodedFileName, encodedFileName);
|
||||
response.setHeader("Content-Disposition", contentDisposition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理下载异常
|
||||
*/
|
||||
private void handleDownloadException(HttpServletResponse response, Exception e) {
|
||||
try {
|
||||
if (!response.isCommitted()) {
|
||||
response.reset();
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
response.setContentType("application/json");
|
||||
response.getWriter().write("{\"error\": \"文件下载失败\"}");
|
||||
}
|
||||
} catch (IOException ioException) {
|
||||
log.error("Error sending error response: {}", ioException.getMessage(), ioException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,5 @@
|
|||
<groupId>com.bonus</groupId>
|
||||
<artifactId>bonus-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.bonus.mainDataBase.service.impl;
|
||||
|
||||
import com.bonus.common.constant.ModelConstants;
|
||||
import com.bonus.common.utils.DownloadModelUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @className:DownloadModelService
|
||||
* @author:cwchen
|
||||
* @date:2025-10-31-14:24
|
||||
* @version:1.0
|
||||
* @description:下载模板的具体业务逻辑层
|
||||
*/
|
||||
@Service(value = "DownloadModelService")
|
||||
@Slf4j
|
||||
public class DownloadModelService {
|
||||
|
||||
@Resource(name = "DownloadModelUtil")
|
||||
private DownloadModelUtil downloadModelUtil;
|
||||
|
||||
/**
|
||||
* 下载工器具模板
|
||||
* @param request
|
||||
* @param response
|
||||
* @return void
|
||||
* @author cwchen
|
||||
* @date 2025/10/31 14:28
|
||||
*/
|
||||
public void downLoadToolModel(HttpServletRequest request, HttpServletResponse response) {
|
||||
downloadModelUtil.commonDownLoadExcelModel(response, ModelConstants.TOOL_MODEL);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in New Issue