我有一个D3
在客户端生成的图表。我将SVG
数据发送到Jersey
REST 服务SVG
,OutputStream
使用Transcoding
. 当我从服务返回响应时,我可以在控制台中看到以二进制流形式返回的数据。
我想要的是一个浏览器下载窗口,一旦服务返回响应就会弹出。
以下是代码片段:
@POST
@Path("downloadSVG")
@Produces("image/png")
public javax.ws.rs.core.Response downloadSVG(@Context HttpServletRequest request, @Context HttpServletResponse httpServletResponse,String values){
LOGGER.info("Inside downloadSVG service.");
javax.ws.rs.core.Response graphImage = null;
JSONObject data = new JSONObject(values);
String svgXML = data.get("svgURL").toString();
try {
OutputStream os = httpServletResponse.getOutputStream();
JPEGTranscoder t = new JPEGTranscoder();
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
InputStream is = new ByteArrayInputStream(svgXML.getBytes());
TranscoderInput input = new TranscoderInput(is);
TranscoderOutput output = new TranscoderOutput(os);
t.transcode(input, output);
LOGGER.info("Graph image generated. Trying to return it to client.");
graphImage = javax.ws.rs.core.Response.ok(os).header("Content-Disposition", "attachment;
filename=graph.png").type("image/png").build();
os.flush();
os.close();
} catch (IOException e1) {
e1.printStackTrace();
} catch (TranscoderException e) {
e.printStackTrace();
}
return graphImage;
}
调用此服务后,我会Exception
在服务器控制台上进行以下操作:
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
Jan 03, 2014 3:21:32 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class org.apache.catalina.connector.CoyoteOutputStream, and Java type class org.apache.catalina.connector.CoyoteOutputStream, and MIME media type image/png was not found
Jan 03, 2014 3:21:32 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
image/* ->
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
.
.
Jan 03, 2014 3:21:32 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class org.apache.catalina.connector.CoyoteOutputStream, and Java type class org.apache.catalina.connector.CoyoteOutputStream, and MIME media type
image/png was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
.
.
.
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class org.apache.catalina.connector.CoyoteOutputStream, and Java type class org.apache.catalina.connector.CoyoteOutputStream, and MIME media type image/png was not found
... 56 more
我想我错过了一些泽西岛的依赖。
任何指针都受到高度赞赏。提前致谢。