Home > Net >  Spring boot api to download svg file
Spring boot api to download svg file

Time:10-29

I wanted to provide a simple spring boot API, where I have an svg file as a String. Whenever my get api (/downloadsvg) is called I should be able to download the svg file.

I have tried as below, • I formed an svg string • I tried converting svg string to inputstream

InputStream stream = new ByteArrayInputStream(AUSTRALIAN_BADGE.getBytes(StandardCharsets.UTF_8));
used produces = MediaType.APPLICATION_OCTET_STREAM_VALUE

Somehow I wasn't able to download it and ended up with an error. Would be great if some pointers were given.

My get API call looks like this (don't mind about the code standard, wanted to improve it once I finish the logic)

@GetMapping(value = "/downloadsvg",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<InputStream> downloadSvgImage() {
    final String AUSTRALIAN_BADGE= "<svg x=\"0\" y=\"0\" width=\"230\" height=\"84\" overflow=\"hidden\" preserveAspectRatio=\"xMidYMid\" xml:space=\"default\" viewbox=\"0 0 230 84\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" version=\"1.1\">\n"  
            "  <rect x=\"1\" y=\"1\" width=\"228px\" height=\"82px\" rx=\"5\" ry=\"5\" stroke=\"#125430\" stroke-width=\"2\" xml:space=\"default\" style=\"fill:#FFFFFF;\" />\n"  
            "  <text id=\"FirstLineBadge\" xml:space=\"default\" x=\"66px\" y=\"18.18182px\" style=\"fill:Color [A=255, R=58, G=71, B=78];\" font-family=\"Fresh Sans Md\" font-size=\"14\">Made in Australia from</text>\n"  
            "  <text id=\"SecondLineBadge\" xml:space=\"default\" x=\"66px\" y=\"36.36364px\" style=\"fill:Color [A=255, R=58, G=71, B=78];\" font-family=\"Fresh Sans Md\" font-size=\"14\">at least 98% Australian</text>\n"  
            "  <text id=\"ThirdLineBadge\" xml:space=\"default\" x=\"66px\" y=\"54.54546px\" style=\"fill:Color [A=255, R=58, G=71, B=78];\" font-family=\"Fresh Sans Md\" font-size=\"14\">ingredients</text>\n"  
            "  <svg id=\"kangaroo\" xml:space=\"default\" width=\"48\" height=\"42\" viewbox=\"0 0 48 42\" x=\"10\" y=\"10\">\n"  
            "    <path d=\"M47.7 39.1 L25.6 1 C25.3 0.4 24.6 0 23.9 0 C23.2 0 22.6 0.4 22.2 1 L0.1999989 39.2 C0.1 39.5 0 39.8 0 40.1 C0 41.1 0.9 42 1.9 42 L46.2 42 C47.2 42 48.1 41.1 48.1 40.1 C48.1 39.8 48 39.4 47.7 39.1 L47.7 39.1 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#F7A30A;\" />\n"  
            "    <path d=\"M47.7 39.1 L25.6 1 C25.3 0.4 24.6 0 23.9 0 C23.2 0 22.6 0.4 22.2 1 L6.7 28 C11.6 28.3 14 26 16.3 23.9 C18.3 22 20.5 20 24 20 C28.6 20 30.9 24.2 31 24.4 C31.3 25.1 31 25.8 30.4 26.1 C29.8 26.4 29 26.1 28.7 25.5 C28.6 25.3 26.8 22.5 24 22.5 C21.5 22.5 20 23.9 18.1 25.7 C14.8 28.9 10.2 30.4 5.700001 29.7 L0.3000007 39.1 C0.1 39.5 0 39.8 0 40.1 C0 41.1 0.9 42 1.9 42 L24.2 42 L21.3 37.8 C20.6 36.7 20.6 35.2 21.4 34.1 L24.7 28.9 C24.9 28.6 25.2 28.4 25.5 28.3 C25.8 28.2 26.2 28.3 26.5 28.5 C27.1 28.9 27.3 29.7 26.9 30.3 L23.6 35.6 C23.4 36 23.3 36.1 23.5 36.5 C24.1 37.5 27.1 41.9 27.2 42 L46.2 42 C47.2 42 48.1 41.1 48.1 40.1 C48.1 39.8 48 39.4 47.7 39.1 L47.7 39.1 z M38.8 28.6 C38.5 29 38 29.1 37.6 28.8 C37 28.5 36.3 28.3 35.6 28.3 C34.4 28.3 33.2 28.8 32.3 29.7 L27.8 35.3 C27.6 35.5 27.4 35.6 27.1 35.6 C26.9 35.6 26.6 35.5 26.5 35.3 C26.4 35.1 26.3 34.9 26.3 34.7 C26.3 34.5 26.4 34.4 26.4 34.2 L29.4 29.2 C30.5 27.2 32.5 25.9 34.8 25.8 L34 22.8 C33.9 22.4 34 22 34.3 21.8 C34.7 21.5 35.2 21.6 35.4 22 C35.4 22 35.4 22 35.4 22.1 L38.9 27.5 C39 27.9 39 28.3 38.8 28.6 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#125430;\" />\n"  
            "    <path d=\"M35.4 22.2 C35.2 21.8 34.7 21.7 34.3 21.9 C34.3 21.9 34.3 21.9 34.2 21.9 C33.90001 22.1 33.8 22.5 33.90001 22.9 L34.7 25.9 C32.40001 26 30.3 27.3 29.3 29.3 L26.3 34.3 C26.2 34.5 26.2 34.6 26.2 34.8 C26.2 35 26.3 35.2 26.40001 35.4 C26.60001 35.6 26.8 35.7 27.00001 35.7 C27.3 35.7 27.50001 35.6 27.70001 35.4 L32.2 29.8 C33 28.9 34.2 28.3 35.5 28.4 C36.2 28.4 36.90001 28.6 37.5 28.9 C37.90001 29.1 38.40001 29 38.7 28.7 C39 28.4 39 27.9 38.8 27.6 L35.4 22.2 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#F7A30A;\" />\n"  
            "    <path d=\"M6.6 28 L5.6 29.8 L5.6 29.8 L6.6 28 L6.6 28 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#F7A30A;\" />\n"  
            "    <path d=\"M18 25.8 C19.9 24 21.4 22.6 23.9 22.6 C26.7 22.6 28.4 25.4 28.6 25.6 C28.9 26.2 29.7 26.5 30.4 26.1 C31 25.8 31.2 25 31 24.4 C30.9 24.2 28.6 20 24 20 C20.5 20 18.4 22 16.3 23.9 C14.1 26.1 11.7 28.3 6.699999 28 L5.699999 29.8 C10.2 30.5 14.7 29 18 25.8 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#F7A30A;\" />\n"  
            "    <polygon xml:space=\"default\" class=\"logo\" points=\"24.2,42 27.1,42 27.1,42 24.2,42 \" style=\"fill:#F7A30A;\" />\n"  
            "    <path d=\"M23.5 35.6 L26.8 30.4 C27.2 29.8 27 29 26.4 28.6 C26.1 28.4 25.7 28.3 25.4 28.4 C25.1 28.5 24.8 28.7 24.6 29 L21.4 34.2 C20.6 35.3 20.6 36.7 21.3 37.9 L24.3 42.1 L27.2 42.1 C27.1 42 24.2 37.6 23.5 36.6 C23.2 36.1 23.3 36 23.5 35.6 z\" xml:space=\"default\" class=\"logo\" style=\"fill:#F7A30A;\" />\n"  
            "  </svg>\n"  
            "</svg>";
    HttpHeaders header = new HttpHeaders();
    header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=img.svg");
    header.add("Cache-Control", "no-cache, no-store, must-revalidate");
    header.add("Pragma", "no-cache");
    header.add("Expires", "0");

    InputStream stream = new ByteArrayInputStream(AUSTRALIAN_BADGE.getBytes(StandardCharsets.UTF_8));
    return ResponseEntity.ok()
            .headers(header)
            .contentLength(AUSTRALIAN_BADGE.length())
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(stream);
} 

 

CodePudding user response:

Just return the byte[] array with a proper header:

@GetMapping(value = "/downloadsvg")
    public ResponseEntity<byte[]> downloadSvgImage() {
        final String AUSTRALIAN_BADGE= "<svg x=\"0\" y=\"0
                          ...
                          ...</svg>";
        HttpHeaders header = new HttpHeaders();
        header.add("Content-Type","image/svg xml");

        return ResponseEntity.ok()
                .headers(header)
                .body(AUSTRALIAN_BADGE.getBytes());

The image I get when I try this code is: [1]: https://i.stack.imgur.com/B5w0l.png

  • Related