私は機能のためにthisの投稿に従っています:クライアントは今の時点でファイル(すなわちcsv、pdf、zip)をダウンロードできます。Spring REST/Swagger/Postman - ダメージを受けた/空白のファイルがダウロードされました
しかし、私は空白になっているか、またはzipファイルで試してみると、破損しているようになっています。 CSVのみが正常に動作します。
ヘッダーを確認しましたが、すべてが標準に準拠しているようです。私は "application/octet-stream"を使っていなくて、pdfの "application/pdf"、csvの "application/csv"、zipの "application/zip"を使ってクライアントの問題を避けるだけです。私はapiをテストするためにswagger 2.4を使用しています。ここに私のコードです。
@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);
}
}
}
私が間違っているか紛失しているか教えてください。
EDIT 1:私は私が得たレスポンスヘッダを付加しています:
{
"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"
}
迅速な対応をありがとう。しかし、応答は同じです - ブラックPDFと破損したzipファイル。 –
@ balboa_21 ResponseEntity APIを使用してレスポンスコピーにバッファされたファイルを実装するサンプルコードを提供しました。 –
こんにちは@yanys、まだ同じ問題。空のpdf + zipファイルが破損しています。 csvだけがうまくいきます。そして、確かに、私はタイトルを変更する、私は春のブートを書いたので、それ以外の何を置くべきか分からなかった。ありがとうございました。ちなみに、私はテスト目的のためにswaggerを使用しています。 –