public interface IndexApi {
@ApiOperation(value = "Download excel.", response = byte[].class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "The excel file", response = byte[].class),
@ApiResponse(code = 500, message = "Unexpected error", response = String.class)})
@GetMapping(
value = "/api/download",
produces = {"application/vnd.ms-excel"})
byte[] download();
}
@RestController
public class TestController implements Api {
@Override
public byte[] download() {
response.setHeader("Content-Disposition", "attachment; filename=\"somefile.xls\"");
return service.download(symbol);
}
}
当我实现它时,它会下载文件,但文件名将被下载。如何设置标题以便我可以自定义文件名?
答案 0 :(得分:0)
您可以使用headerscset返回ResponseEntity
对象,并将文件流传递给ResponseEntity
答案 1 :(得分:0)
@Override
@ApiOperation(
value = "Download file.",
response = byte[].class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "File download.", response = byte[].class),
@ApiResponse(code = 500, message = "Unexpected error", response = String.class)})
@GetMapping(
value = "/file/{exDate}/download",
produces = {"application/vnd.ms-excel"})
public byte[] download(@ApiParam(value = "Valid date.", required = true) @PathVariable(value = "exDate") String exDate, HttpServletResponse response) {
response.setHeader("Content-disposition", "attachment; filename=" + String.format(FILE_NAME_TEMPLATE, dateArg[0],dateArg[1],dateArg[2]));
return service.download(exDate);
}
必须将produces
参数设置为application/vnd.ms-excel
。