报告下载接口
This commit is contained in:
parent
7079de93ff
commit
b37752f4c4
@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@ -54,11 +55,11 @@ public class ReportController {
|
||||
|
||||
|
||||
@PostMapping(path="/downLoadReport")
|
||||
public CommonResult<ResponseEntity<String>> downLoadReport(@RequestBody @NotNull @Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
||||
public CommonResult<ResponseEntity<String>> downLoadReport(@RequestBody @NotNull @Valid BaseIdReq req) throws Exception {
|
||||
|
||||
ResponseEntity<String> fileInputStream = reportService.downLoadReport(req);
|
||||
|
||||
return CommonResult.success(fileInputStream);
|
||||
return CommonResult.success(fileInputStream);
|
||||
}
|
||||
|
||||
|
||||
|
@ -8,6 +8,7 @@ import cd.casic.framework.commons.pojo.PageResult;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@ -24,5 +25,5 @@ public interface ReportService{
|
||||
|
||||
ReportResp deleteReport(@Valid ReportDeleteReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
|
||||
|
||||
ResponseEntity<String> downLoadReport(@Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
|
||||
ResponseEntity<String> downLoadReport(@Valid BaseIdReq req) throws Exception;
|
||||
}
|
||||
|
@ -11,11 +11,18 @@ import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
|
||||
import cd.casic.framework.commons.pojo.PageResult;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import jakarta.activation.MimeType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.tika.Tika;
|
||||
import org.apache.tika.metadata.Metadata;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@ -94,11 +101,12 @@ public class ReportServiceImpl implements ReportService {
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseEntity<String> downLoadReport(BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
||||
public ResponseEntity<String> downLoadReport(BaseIdReq req) throws Exception {
|
||||
RestTemplate restTemplate = getRestTemplateWithoutSANCheck();
|
||||
String reportDeleteUrl = ConstantContextHolder.getScaIp() + PATH;
|
||||
String body = buildDownloadRequestBody(req);
|
||||
HttpHeaders headers = createHeaders();
|
||||
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
|
||||
|
||||
@ -111,10 +119,14 @@ public class ReportServiceImpl implements ReportService {
|
||||
byte[] responseBody = responseEntity.getBody();
|
||||
String base64ResponseBody = Base64.getEncoder().encodeToString(responseBody);
|
||||
// 构建返回给前端的 ResponseEntity<byte[]>
|
||||
|
||||
// 返回MIME类型:ml-citation{ref="5" data="citationList"}
|
||||
Optional<FileType> detect = detect(responseBody);
|
||||
FileType fileType = detect.get();
|
||||
System.out.println(fileType.extension);
|
||||
// 可选:转换为扩展名
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=report.zip")
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=report."+fileType.extension)
|
||||
.body(base64ResponseBody);
|
||||
}else {
|
||||
log.error("===============报告下载接口失败=================");
|
||||
@ -201,6 +213,45 @@ public class ReportServiceImpl implements ReportService {
|
||||
reportResp.setSuccess(data.getInteger("success"));
|
||||
reportResp.setXmTraceId(xmTraceId);
|
||||
}
|
||||
public enum FileType {
|
||||
// 文档类
|
||||
XLSX("xlsx", "Excel", new byte[]{0x50, 0x4B, 0x03, 0x04}),
|
||||
DOCX("docx", "Word", new byte[]{0x50, 0x4B, 0x03, 0x04}),
|
||||
PDF("pdf", "PDF", new byte[]{0x25, 0x50, 0x44, 0x46}),
|
||||
|
||||
// 图片类
|
||||
PNG("png", "PNG", new byte[]{-119, 0x50, 0x4E, 0x47}),
|
||||
JPEG("jpg", "JPEG", new byte[]{-1, -40, -1, -32}),
|
||||
|
||||
// 压缩包
|
||||
ZIP("zip", "ZIP", new byte[]{0x50, 0x4B, 0x03, 0x04}),
|
||||
RAR("rar", "RAR", new byte[]{0x52, 0x61, 0x72, 0x21});
|
||||
|
||||
private final String extension;
|
||||
private final String description;
|
||||
private final byte[] magicNumber;
|
||||
|
||||
FileType(String ext, String desc, byte[] magic) {
|
||||
this.extension = ext;
|
||||
this.description = desc;
|
||||
this.magicNumber = magic;
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<FileType> detect(byte[] header) throws Exception {
|
||||
for (FileType type : FileType.values()) {
|
||||
if (header.length >= type.magicNumber.length) {
|
||||
boolean match = true;
|
||||
for (int i = 0; i < type.magicNumber.length; i++) {
|
||||
if (header[i] != type.magicNumber[i]) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (match) return Optional.of(type);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
9
ops-server/src/test/java/cd/casic/server/ReportTest.java
Normal file
9
ops-server/src/test/java/cd/casic/server/ReportTest.java
Normal file
@ -0,0 +1,9 @@
|
||||
package cd.casic.server;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
@SpringBootTest(classes = {OpsServerApplication.class})
|
||||
@ActiveProfiles("local")
|
||||
public class ReportTest {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user