gpt4 book ai didi

java - Spring Boot:如何从返回ResponseEntity 的MicroService下载文件?

转载 作者:行者123 更新时间:2023-12-01 19:18:27 25 4
gpt4 key购买 nike

我正在尝试下载Spring Boot Micro Service返回的文件(PDF或Excel)。
因此,我有一个GWT应用程序(简称为“ A”)和一个微服务。
问题是:微服务创建了所需的文件(完美),但是我的浏览器没有显示“另存为”弹出窗口。没发生什么事。

这是我到目前为止所拥有的:

@GetMapping(value = "/report")
public ResponseEntity<Resource> generateFullReport(HttpServletResponse response, HttpServletRequest request,
@RequestParam("name") String name, @RequestParam("output") String output) throws BirtException {
log.info("Generating full report: " + name + "; format: " + output);
EnumOutputType format = EnumOutputType.from(output);

String filePath = birtReportManagerService.generateMainReport(name, format, response, request);
File file = new File(filePath);

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+file.getName());
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
headers.add(HttpHeaders.PRAGMA, "no-cache");
headers.add(HttpHeaders.EXPIRES, "0");

ByteArrayResource resource = null;
try {
resource = new ByteArrayResource(Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}

ResponseEntity responseEntity = ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.body(resource);
// file.delete();
return responseEntity;
}


和:

@Override
public void generateBirtReport(String reportName, String reportFormat, HashMap<String, String> parameters) {
logger.debug("Entering REST generateBirtReport {}", reportName+"."+reportFormat);

LinkedMultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
for(Map.Entry<String, String> entry : parameters.entrySet()){
multiValueMap.add(entry.getKey(), entry.getValue());
}

URI uri = UriComponentsBuilder.fromUriString(URL_GLOBAL_REST + "/v1/reports/report")
.queryParam("name", reportName)
.queryParam("output", reportFormat)
.queryParams(multiValueMap)
.build()
.encode()
.toUri();
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<String> request = new HttpEntity<>(httpHeaders);
ResponseEntity<Resource> response = restTemplate.exchange(uri, HttpMethod.GET, request, Resource.class);
}


我希望看到该对话框:

I would like to see that dialog box

版本:SpringBoot:2.1.5 / Java:8

最佳答案

@Puh:谢谢。太简单了...只需打开一个新选项卡,它就可以正常工作。

关于java - Spring Boot:如何从返回ResponseEntity <Resource>的MicroService下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com