-2
レスポンスのExcelバイト[]をブラウザに使用しました。しかし、私は問題があります。なぜなら、Springブートはファイルをエンコードして、ブラウザからダウンロードするよりも悪いファイルを取得したからです。私は私の問題を解決しブラウザでファイルをダウンロードしたときにファイル内のエンコーディングが変わったとき、私が春の起動時にこの問題を解決する方法
//This method returned response on controller
public ResponseEntity<ByteArrayResource>
returnAllTransactionAsExcel(TransactionSearchFilter
transactionSearchFilter) throws IOException {
List<Transaction> transactions =
getAllTransactions(transactionSearchFilter);
byte[] ourFile=writeIntoExcel(transactions);
//headers
HttpHeaders headers = new HttpHeaders();
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Content-Disposition", "attachment;
filename=list_transactions.xls");
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel"));
return
ResponseEntity
.ok()
.headers(headers)
.contentLength(ourFile.length)
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.body(new ByteArrayResource(ourFile));
}
//This method on controller
@ApiOperation(value = "Retrieve all transactions in Excel")
@RequestMapping(value = "/allExcel", method = RequestMethod.POST,
produces "application/vnd.ms-excel")
public ResponseEntity<ByteArrayResource>
getAllTransactionsAsExcel(@RequestBody TransactionSearchFilter
transactionSearchFilter) throws IOException {
return returnAllTransactionAsExcel(transactionSearchFilter);
}