0

I am having 1 problem. I save SVG images in the database as binary. Now, I want to download it without converting to base64, is there any way. Thank you.

Lonzak
  • 9,334
  • 5
  • 57
  • 88
gin
  • 11
  • 3
  • Welcome to stackoverflow. There are several problems with your question which makes it really difficult to answer. 1) You don't provide any details: What kind of database do you use? How do yo access your DB (do you use hibernate or direct access)? What version of java do you use? ... 2) What did you already try? You did not provide any sourcecode. 3) don't use irrelevant tags! Why did you tag your question with `Spring` and `PDFBox`? Either provide more details or remove the tags! – Lonzak Jun 24 '21 at 06:20

1 Answers1

0

Basically, that would mean getting the BLOB object from the database. I would follow this approach to show it in directly in the browser:


@RestController
public class ImageController {

    @GetMapping(value = "/img-test", produces = "image/svg+xml")
    public byte[] getImg() throws IOException
    {
        // retrieve your image from the DB
        File imgFile = new File("C:\\Users\\USR\\Desktop\\img.svg");

        InputStream inp = new DataInputStream(new FileInputStream(imgFile));

        return inp.readAllBytes(); // This is a Java 9 specific convertion
    }
}

With this approach, you do not change anything on the BLOB image. You take it and return it as is, an array with bytes. And you can directly show it in a browser or embed it somewhere in your HTML file.

The main thing here is the MIME type : image/svg+xml

If you are using an older version of Java, then check this question for the conversion of the InputStream object to a byte array.

And with this approach you can download the file:

@GetMapping("download-img")
    public ResponseEntity downloadImg() throws IOException
    {
        // Get the file from the DB...
        File imgFile = new File("C:\\Users\\USR\\Desktop\\img.svg");

        InputStream inp = new DataInputStream(new FileInputStream(imgFile));
        //Dynamically change the File Name here
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"img.svg\"")
                .body(inp.readAllBytes());
    }
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • i can get bytes, but my desired output is svg image. – gin Jun 23 '21 at 07:06
  • Do you want to display it in the browser or download it? – Renis1235 Jun 23 '21 at 07:42
  • @gin I updated the answer with the correct implementation regarding displaying it in a browser. I tested it and it works... Good luck! – Renis1235 Jun 23 '21 at 07:53
  • I have to take it out to draw on pdf files with pdfbox. It's really hard for me – gin Jun 23 '21 at 08:55
  • Then please be more clear, I spent time and efforts to bring you something on the table... You said you want an `SVG` image, and you got it... You have to use logic now to put this image into use with pdfbox. You could save the files in a temporary folder and access them from there. Or you could input the images as byte arrays, it should be possible with a pdf library. – Renis1235 Jun 23 '21 at 09:15
  • @gin upvote and accept if my answer helped you.. Good luck! – Renis1235 Jun 24 '21 at 06:05