Che*_*tan 0 excel response download apache-poi dropwizard
我正在将dropwizard用于我的其余api开发,该开发将返回json响应。我现在遇到了一个要求,我必须在调用一个特定api时返回一个excel文件。我发现apache poi是很好的库。但是我该如何返回Excel文件作为响应。然后,浏览器应显示下载选项。
将Content-Type设置为,application/octet-stream
并添加Content-Disposition
响应标头即可。有点像Content-Disposition: attachment; filename="file.xsl"
。例如
@Path("/file")
public class FileResource {
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() throws Exception {
InputStream is = new FileInputStream("file.txt");
return Response.ok(is)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"file.txt\"")
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
除了InputStream
,您还可以返回File
,a byte[]
,a StreamingOutput
,您可以选择。