It was working, just that I was using Chrome's extension POSTman.
Download image from SVG with Jersey
-
04-07-2023 - |
Question
I have some SVG string from highcharts and I want to download it as an image (PNG). The conversion is done using 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();
}
but what I'm getting is a bunch of text, something like
�PNG IHDR�@��E� cHRMz&��...
Tried with @Produces("image/*")
but didn't work...
Can I not save it as an image (real file) and allow user to download it? Trying to avoid unnecessary effort..
Thanks!
Solution 2
OTHER TIPS
You need to set the correct mime type for the response something like
response.setContentType("image/png");
where response is a ServletResponse object.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow