Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字节[]转换为数据URI的Base64字符串_Java_Html_Bytearray_Base64 - Fatal编程技术网

Java 将字节[]转换为数据URI的Base64字符串

Java 将字节[]转换为数据URI的Base64字符串,java,html,bytearray,base64,Java,Html,Bytearray,Base64,我知道这个问题可能被问了10000次,但是,我似乎找不到一个直接的答案 我有一个LOB存储在我的db中,它代表一个图像;我从数据库中得到了这个图像,我想通过HTMLIMG标签在网页上显示它。这不是我首选的解决方案,但在我找到更好的解决方案之前,这是一个权宜之计 我正试图通过以下方式使用Apache Commons编解码器将字节[]转换为Base64: String base64String = Base64.encodeBase64String({my byte[]}); 然后,我尝试在我的页

我知道这个问题可能被问了10000次,但是,我似乎找不到一个直接的答案

我有一个LOB存储在我的db中,它代表一个图像;我从数据库中得到了这个图像,我想通过HTMLIMG标签在网页上显示它。这不是我首选的解决方案,但在我找到更好的解决方案之前,这是一个权宜之计

我正试图通过以下方式使用Apache Commons编解码器将字节[]转换为Base64:

String base64String = Base64.encodeBase64String({my byte[]});
然后,我尝试在我的页面上显示我的图像,如下所示:

<img src="data:image/jpg;base64,{base64String from above}"/>

它将显示浏览器默认的“我找不到此图像”图像

有人有什么想法吗


谢谢。

根据官方文件,这应该是您要找的


JPG的mime类型也是
image/jpeg

这是正确的语法。可能是您的web浏览器不支持数据URI方案。看


pj> MIME类型是“代码>图像/JPEG。

,您也可以考虑将图像流出到浏览器中,而不是在页面本身上编码。” 下面是一个通过servlet将文件中包含的图像流式传输到浏览器的示例,可以很容易地采用servlet来流式传输BLOB的内容,而不是文件:

  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 byte[BUF_SIZE];
      for (int count = 0; (count = in.read(buf)) >= 0;) {
        out.write(buf, 0, count);
      }
    }
    finally {
      in.close();
    }
  }

如果不希望从servlet流式传输,则将文件保存到webroot中的目录,然后创建指向该位置的src。这样,web服务器就完成了为文件提供服务的工作。如果您觉得自己特别聪明,那么可以通过timestamp/inode/crc32检查现有文件,只有在数据库中发生更改时才将其写出,这样可以提高性能。此文件方法还将自动支持ETag和if modified-since-headers,以便浏览器可以正确缓存文件。

我使用了此方法,效果良好(与公认的答案相反,该答案使用了不推荐用于此场景的格式):


建议使用+和/可以。是否有人尝试过使用Base64.encodeBase64URLSafeString()的输出来查看它是否在浏览器中工作?encodeBase64URLSafeString()似乎无法生成有效/标准的base64编码。要使base64编码的字符串URI兼容,应该对其应用标准URI字符转义。我不认为上面的函数是你想要的。这个答案实际上并没有为我生成正确的字符串。Hugo给出的答案包含了使其正常工作所缺少的编码。我使用的是SpringWebMVC,整个页面都是使用tiles等构建的。。。我真的不想经历创建另一个控制器并分别调用每个图像的PITA过程,但是谢谢!你能发布完整的代码吗…我需要它作为参考…我也有同样的问题…但我不知道代码将如何…这是正确的答案,没有UTF8编码,浏览器会给你带来麻烦。你使用了什么导入?IIRC,Base64类是来自apache commons的,虽然我不再确定(也不再有权访问该项目)。StringUtils没有定义。@Hill它来自Apache Commons。看看吧,你可能会在每个项目中使用它。
StringBuilder sb = new StringBuilder();
sb.append("data:image/png;base64,");
sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(imageByteArray, false)));
contourChart = sb.toString();