【发布时间】:2011-12-30 04:23:03
【问题描述】:
您好,我想从 resteasy 服务器返回一个文件。为此,我在客户端有一个链接,它使用 ajax 调用休息服务。我想在休息服务中返回文件。我尝试了这两个代码块,但都没有按我希望的那样工作。
@POST
@Path("/exportContacts")
public Response exportContacts(@Context HttpServletRequest request, @QueryParam("alt") String alt) throws IOException {
String sb = "Sedat BaSAR";
byte[] outputByte = sb.getBytes();
return Response
.ok(outputByte, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = temp.csv")
.build();
}
.
@POST
@Path("/exportContacts")
public Response exportContacts(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("alt") String alt) throws IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=temp.csv");
ServletOutputStream out = response.getOutputStream();
try {
StringBuilder sb = new StringBuilder("Sedat BaSAR");
InputStream in =
new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
byte[] outputByte = sb.getBytes();
//copy binary contect to output stream
while (in.read(outputByte, 0, 4096) != -1) {
out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
}
return null;
}
当我从 firebug 控制台检查时,这两个代码块都写了“Sedat BaSAR”以响应 ajax 调用。但是,我想将“Sedat BaSAR”作为文件返回。我该怎么做?
提前致谢。
【问题讨论】:
-
您最终找到解决方案了吗?
标签: java rest file-io resteasy java-io