Convert byte[] to Base64 string for data URI?

According to the official documentation Base64. EncodeBase64URLSafeString(byte binaryData) should be what you're looking for Also mime type for JPG is image/jpeg.

According to the official documentation Base64. EncodeBase64URLSafeString(byte binaryData) should be what you're looking for. Also mime type for JPG is image/jpeg.

The PNG example on the Wikipedia article suggests that using + and / is okay. – Daniel Trebbien Jan 29 at 18:07.

That's the correct syntax. It might be that your web browser does not support the data URI scheme. See Which browsers support data URIs and since which version?

Also, the JPEG MIME type is image/jpeg.

You may also want to consider streaming the images out to the browser rather than encoding them on the page itself. Here's an example of streaming an image contained in a file out to the browser via a servlet, which could easily be adopted to stream the contents of your BLOB, rather than a file: public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream sos = resp.getOutputStream(); try { final String someImageName = req. GetParameter(someKey); // encode the image path and write the resulting path to the response File imgFile = new File(someImageName); writeResponse(resp, sos, imgFile); } catch (URISyntaxException e) { throw new ServletException(e); } finally { sos.close(); } } private void writeResponse(HttpServletResponse resp, OutputStream out, File file) throws URISyntaxException, FileNotFoundException, IOException { // Get the MIME type of the file String mimeType = getServletContext().

GetMimeType(file.getAbsolutePath()); if (mimeType == null) { log. Warn("Could not get MIME type of file: " + file.getAbsolutePath()); resp. SetStatus(HttpServletResponse.

SC_INTERNAL_SERVER_ERROR); return; } resp. SetContentType(mimeType); resp. SetContentLength((int)file.length()); writeToFile(out, file); } private void writeToFile(OutputStream out, File file) throws FileNotFoundException, IOException { final int BUF_SIZE = 8192; // write the contents of the file to the output stream FileInputStream in = new FileInputStream(file); try { byte buf = new byteBUF_SIZE; for (int count = 0; (count = in.

Read(buf)) >= 0;) { out. Write(buf, 0, count); } } finally { in.close(); } }.

I'm using Spring Web MVC and the whole page is being built using tiles, etc... I really don't want to go through the PITA of creating another controller and calling each image separately, but thanks! – El Guapo Jan 30 at 1:16.

I don’t know if it is the real problem here, but the MIME type of JPEG is not image/jpg, but image/jpeg.

If you don't want to stream from a servlet, then save the file to a directory in the webroot and then create the src pointing to that location. That way the web server does the work of serving the file. If you are feeling particularly clever, you can check for an existing file by timestamp/inode/crc32 and only write it out if it has changed in the DB which can give you a performance boost.

This file method also will automatically support ETag and if-modified-since headers so that the browser can cache the file properly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions