2 changed files with 120 additions and 0 deletions
-
1src/main/java/com/gaotao/config/ShiroConfig.java
-
119src/main/java/com/gaotao/modules/print/controller/PrinterDownloadController.java
@ -0,0 +1,119 @@ |
|||||
|
package com.gaotao.modules.print.controller; |
||||
|
|
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.*; |
||||
|
import java.net.URLEncoder; |
||||
|
|
||||
|
/** |
||||
|
* CLodop打印组件下载控制器 |
||||
|
* 用于提供CLodop打印组件安装程序的下载功能 |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("/printer") |
||||
|
public class PrinterDownloadController { |
||||
|
|
||||
|
private static final Logger logger = LoggerFactory.getLogger(PrinterDownloadController.class); |
||||
|
|
||||
|
/** |
||||
|
* CLodop安装程序存放目录 |
||||
|
* 可在配置文件中配置,默认为 D:\MES_Application\Printer |
||||
|
*/ |
||||
|
@Value("${clodop.installPath:D:\\MES_Application\\Printer}") |
||||
|
private String clodopInstallPath; |
||||
|
|
||||
|
/** |
||||
|
* CLodop安装程序文件名 |
||||
|
* 可在配置文件中配置,默认为 CLodop_Setup_for_Win32NT.exe |
||||
|
*/ |
||||
|
@Value("${clodop.fileName:CLodop_Setup_for_Win64.exe}") |
||||
|
private String clodopFileName; |
||||
|
|
||||
|
/** |
||||
|
* 下载CLodop打印组件安装程序 |
||||
|
* 前端调用路径: /printer/download |
||||
|
* |
||||
|
* @param response HTTP响应对象 |
||||
|
*/ |
||||
|
@GetMapping("/download") |
||||
|
public void downloadClodop(HttpServletResponse response) { |
||||
|
// 构建完整文件路径 |
||||
|
String filePath = clodopInstallPath + File.separator + clodopFileName; |
||||
|
File file = new File(filePath); |
||||
|
|
||||
|
// 检查文件是否存在 |
||||
|
if (!file.exists()) { |
||||
|
logger.error("CLodop安装程序文件不存在: {}", filePath); |
||||
|
try { |
||||
|
response.setContentType("text/html;charset=UTF-8"); |
||||
|
response.getWriter().write("<h3>错误:CLodop打印组件安装程序文件不存在</h3><p>请联系管理员检查服务器上的文件路径: " + filePath + "</p>"); |
||||
|
} catch (IOException e) { |
||||
|
logger.error("写入错误响应失败", e); |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// 检查文件是否可读 |
||||
|
if (!file.canRead()) { |
||||
|
logger.error("CLodop安装程序文件无法读取: {}", filePath); |
||||
|
try { |
||||
|
response.setContentType("text/html;charset=UTF-8"); |
||||
|
response.getWriter().write("<h3>错误:无法读取CLodop打印组件安装程序文件</h3><p>请联系管理员检查文件权限</p>"); |
||||
|
} catch (IOException e) { |
||||
|
logger.error("写入错误响应失败", e); |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
logger.info("开始下载CLodop安装程序: {}", filePath); |
||||
|
|
||||
|
// 设置响应头 |
||||
|
try { |
||||
|
// 设置内容类型为二进制流,用于文件下载 |
||||
|
response.setContentType("application/octet-stream"); |
||||
|
// 设置文件大小 |
||||
|
response.setContentLengthLong(file.length()); |
||||
|
// 设置文件名,使用URL编码处理中文文件名 |
||||
|
String encodedFileName = URLEncoder.encode(clodopFileName, "UTF-8").replace("+", "%20"); |
||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName); |
||||
|
// 允许跨域下载 |
||||
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
||||
|
} catch (UnsupportedEncodingException e) { |
||||
|
logger.error("文件名编码失败", e); |
||||
|
} |
||||
|
|
||||
|
// 读取文件并写入响应流 |
||||
|
try (FileInputStream fis = new FileInputStream(file); |
||||
|
BufferedInputStream bis = new BufferedInputStream(fis); |
||||
|
OutputStream os = response.getOutputStream()) { |
||||
|
|
||||
|
byte[] buffer = new byte[8192]; // 8KB缓冲区 |
||||
|
int bytesRead; |
||||
|
while ((bytesRead = bis.read(buffer)) != -1) { |
||||
|
os.write(buffer, 0, bytesRead); |
||||
|
} |
||||
|
os.flush(); |
||||
|
logger.info("CLodop安装程序下载完成: {}", filePath); |
||||
|
|
||||
|
} catch (IOException e) { |
||||
|
logger.error("文件下载失败: {}", e.getMessage()); |
||||
|
// 如果还没有开始写入响应体,可以返回错误信息 |
||||
|
if (!response.isCommitted()) { |
||||
|
try { |
||||
|
response.reset(); |
||||
|
response.setContentType("text/html;charset=UTF-8"); |
||||
|
response.getWriter().write("<h3>错误:文件下载失败</h3><p>" + e.getMessage() + "</p>"); |
||||
|
} catch (IOException ex) { |
||||
|
logger.error("写入错误响应失败", ex); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue