gpt4 book ai didi

java - Spring REST/Swagger/Postman - 正在下载损坏/空白文件

转载 作者:行者123 更新时间:2023-11-30 06:50:24 25 4
gpt4 key购买 nike

我关注了this发布功能:客户端现在可以下载文件(即 csv、pdf 和 zip)。

但是我得到的 pdf 是空白的,或者在尝试使用 zip 文件时,它已损坏。只有 CSV 可以正常工作

我检查了标题,一切似乎都符合标准。我什至没有使用“application/octet-stream”,对 pdf 使用“application/pdf”,对 csv 使用“application/csv”,对 zip 使用“application/zip”,只是为了避免客户端出现任何问题。我正在使用 swagger 2.4 来测试我的 api。这是我的代码。

@CrossOrigin
@Controller
public class ReportRestController {


@Autowired
ReportService reportService;

@Value("${report.temp.directory}") // used for storing file in local
private String reportLocation;

@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "Authorization", required = true, dataType = "string", paramType = "header"),
@ApiImplicitParam(name = "Auth-Provider", value = "Auth-Provider", required = true, dataType = "string", paramType = "header"),})
@RequestMapping(value = "/report/{type}/{format}", method = RequestMethod.POST)
public void getList(@RequestHeader(value = "UserId", required = false) Long userId,
@RequestHeader(value = "TeamId", required = false) Long teamId,
@RequestHeader(value = "CustomerId", required = true) Long customerId,
@PathVariable("type") String type, @PathVariable("format") String formate,
@RequestBody ReportRequestObj reportobj, HttpServletResponse response) {

String filename = reportService.getReport(customerId, userId, teamId, type, formate, reportobj);
Path pathfile = Paths.get(reportLocation, filename);
File file = pathfile.toFile();
if (file.exists()) {
String fileExtension = FilenameUtils.getExtension(filename);
if(CommonConstants.CSV.equals(fileExtension)){
response.setContentType("application/csv");
}else if(CommonConstants.PDF.equals(fileExtension)){
response.setContentType("application/pdf");
}else if(CommonConstants.ZIP.equals(fileExtension)){
response.setContentType("application/zip");
}
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentLength((int) file.length());
response.setHeader("Content-Transfer-Encoding", "binary");
try(FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream,response.getOutputStream());
response.getOutputStream().flush();
//response.flushBuffer();
} catch (IOException ex) {
log.error("Error while sending the file:{} for customerId:{} ,userId:{}",
file.getPath(), customerId, userId);
}
}
}

请让我知道我做错了什么或遗漏了什么?

编辑 1:我附上了我得到的响应 header :

{
"date": "Sun, 01 Jan 2017 19:11:13 GMT",
"x-content-type-options": "nosniff",
"access-control-max-age": "3600",
"content-disposition": "attachment; filename=localhost-blob-abcd.pdf",
"content-length": "172962",
"x-xss-protection": "1; mode=block",
"x-application-context": "report-server:8095",
"pragma": "no-cache",
"server": "Apache-Coyote/1.1",
"x-frame-options": "DENY",
"access-control-allow-methods": "POST, PUT, GET, OPTIONS, DELETE",
"content-type": "application/pdf;charset=UTF-8",
"access-control-allow-origin": "*",
"cache-control": "no-cache, no-store, max-age=0, must-revalidate, no-cache",
"access-control-allow-headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, Auth-Provider, UserId, TeamId, Lang, CustomerId",
"expires": "0"
}

最佳答案

好的,这段代码对我有用:

package com.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Controller
public class DownloadController {

@RequestMapping("/download")
public ResponseEntity<InputStreamResource> download() throws FileNotFoundException {
final File file = new File("file.pdf");
return ResponseEntity.ok()
.contentLength(file.length())
.contentType(MediaType.APPLICATION_PDF)
.cacheControl(CacheControl.noCache())
.header("Content-Disposition", "attachment; filename=" + file.getName())
.body(new InputStreamResource(new FileInputStream(file)));
}
}
}

没有什么特定于 Spring Boot,您可以在普通 Spring MVC 中使用。

也许这是一个像这样的 Swagger 问题:File download via content-disposition header corrupts file ,不是服务器端的。您是否尝试过使用其他工具测试此 API 调用?例如curl永远不要让我失望。

关于java - Spring REST/Swagger/Postman - 正在下载损坏/空白文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415602/

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