2026-4-2提交:pdf预览接口

This commit is contained in:
junzhangfm 2026-04-02 15:19:46 +08:00
parent 5366d13f91
commit a3fbcafc24
4 changed files with 55 additions and 1 deletions

View File

@ -8,6 +8,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* 事件导出记录 - Controller层
*
@ -42,4 +44,9 @@ public class DnerEventExportRecordController {
public ResponseEntity<Resource> download(@PathVariable String id) {
return dnerEventExportRecordService.download(id);
}
@GetMapping("/preview/{id}")
public void previewPdf(@PathVariable Long id, HttpServletResponse response) {
dnerEventExportRecordService.previewPdf(id, response);
}
}

View File

@ -6,6 +6,8 @@ import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* 事件导出记录 服务接口
*
@ -29,4 +31,12 @@ public interface IDnerEventExportRecordService extends IService<DnerEventExportR
* @return 响应体
*/
ResponseEntity<Resource> download(String id);
/**
* 预览PDF
*
* @param id 导出记录ID
* @param response 响应体
*/
void previewPdf(Long id, HttpServletResponse response);
}

View File

@ -14,7 +14,13 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
/**
@ -60,4 +66,35 @@ public class DnerEventExportRecordServiceImpl
}
return FileDownloadUtil.downLoad(record.getFilePath(), record.getFileName());
}
@Override
public void previewPdf(Long id, HttpServletResponse response) {
DnerEventExportRecord record = getById(id);
File pdfFile = new File(record.getFilePath());
if (!pdfFile.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
try (FileInputStream fis = new FileInputStream(pdfFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream()) {
// 设置响应头 = 关键告诉浏览器这是PDF直接预览
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"inline;filename=" + URLEncoder.encode(pdfFile.getName(), "UTF-8"));
// 写出文件流
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}

View File

@ -9,7 +9,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/south_power_grid?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: root
password: 123456
password: 123456 # Nfdw@202510
hikari:
connection-timeout: 30000
validation-timeout: 5000