我使用 spring boot 创建了球衣休息帖子 api,以返回具有此类文件名的 zip 文件,使用邮递员我可以下载 zip 文件,但不能下载真实的文件名,并且在响应标头中我找不到 Content-disposition key。
我的代码是:
@Context
lateinit var httpServletResponse: HttpServletResponse
@POST
@Path("/getRecord")
@Produces("application/zip", MediaType.APPLICATION_JSON)
fun getRecord(@RequestBody recordIdentification: RecordIdentificationRequest) {
val compositeRecord: CompositeRecord = service.getRecordFromRequest(recordIdentification)
var tmpFile: File? = service.getRecordZip(compositeRecord)
if (tmpFile != null) {
Files.newInputStream(tmpFile.toPath()).transferTo(httpServletResponse.outputStream)
httpServletResponse.status = HttpStatus.OK.value()
httpServletResponse.contentType = "application/zip"
val contentDisposition: ContentDisposition = ContentDisposition.attachment()
.filename(tmpFile.name)
.build()
httpServletResponse.setHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
httpServletResponse.outputStream.flush()
}
}
谢谢!
您可以依赖 Jersey 提供的 Jersey Multipart 功能,而不是直接使用 Servlet 类。这应该会让你的生活更轻松。
该文档描述了两种方法,一种是用于 Jersey 2.x 和 3.0 的 Jersey 专有方法,另一种是 Jakarta REST 3.1 方法,由 Jersey 3.1 实现,具体取决于您所在的 Jersey。