Mar*_*les 9 java rest cxf binaryfiles
我是使用CXF和Spring创建RESTful Web服务的新手.
这是我的问题:我想创建一个服务,生成"任何"类型的文件(可以是image,document,txt甚至pdf),还有XML.到目前为止我得到了这段代码:
@Path("/download/")
@GET
@Produces({"application/*"})
public CustomXML getFile() throws Exception;
Run Code Online (Sandbox Code Playgroud)
我不确切知道从哪里开始所以请耐心等待.
编辑:
完整的Bryant Luk代码(谢谢!)
@Path("/download/")
@GET
public javax.ws.rs.core.Response getFile() throws Exception {
if (/* want the pdf file */) {
File file = new File("...");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "attachment; filename =" + file.getName())
.build();
}
/* default to xml file */
return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build();
}
Run Code Online (Sandbox Code Playgroud)
Bry*_*Luk 15
如果它将返回任何文件,您可能希望使您的方法更"通用"并返回一个javax.ws.rs.core.Response,您可以通过编程方式设置Content-Type标头:
@Path("/download/")
@GET
public javax.ws.rs.core.Response getFile() throws Exception {
if (/* want the pdf file */) {
return Response.ok(new File(/*...*/)).type("application/pdf").build();
}
/* default to xml file */
return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16585 次 |
| 最近记录: |