从泽西岛SVG下载图像

时间:2014-04-16 12:29:52

标签: java svg highcharts jersey

我有一些来自highcharts的SVG字符串,我想将其下载为图像(PNG)。 使用PNGTranscoder(Batik)

完成转换
@POST
public Response getChartImage() throws Exception{

    String svg = "<svg xmlns:xlink..."; // can be ignored
    svg = svgDocumentConvertAndRevert(svg); // can be ignored

    TranscoderInput input = new TranscoderInput(new StringReader(svg));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(baos);

    PNGTranscoder transcoder = new PNGTranscoder();
    transcoder.transcode(input, output);

    BufferedImage image = new BufferedImage(300, 500, 1);
    ImageIO.write(image, "png", baos);
    byte[] imageData = baos.toByteArray();

    return Response.ok(imageData).build();

}

但我得到的是一堆文字,比如

�PNG  IHDR�@��E� cHRMz&��...

尝试@Produces("image/*"),但没有工作......

我可以不将其保存为图像(真实文件)并允许用户下载吗?尽量避免不必要的努力..

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要为响应设置正确的mime类型,例如

response.setContentType("image/png");

其中response是ServletResponse对象。

答案 1 :(得分:0)

它正在运行,只是因为我正在使用Chrome的扩展程序POSTman。

相关问题