简体   繁体   English

从数据库到Java以及从Java到Javascript图像的Blob图像

[英]Blob image from database to Java and from Java to Javascript Image

I have Blob, which stored in db and i take it from database with java server like this: 我有Blob,它存储在db中,我从带有java服务器的数据库中获取它,如下所示:

Entity.java 实体.java

@Column(name = "img")
private Blob img;

public Blob getImg() {
    return img;
}
public void setImg(Blob img) {
    this.img = img;
}

Repository.java 仓库.java

@Transactional
@Query(value = "SELECT img FROM articles WHERE category = ?", nativeQuery = true)
//Blob findP(String category);

Blob findPic(String category);

Controller.java Controller.java

@RequestMapping(value="/Pic_test")
@ResponseBody
public Blob getPics() throws SQLException, IOException {

    return remindRepository.findPic("Java");
}

Then I receive it with Javascript to image it: 然后我用Javascript接收它以对其进行映像:

    function toDataURL(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onload = function() {
    var reader = new FileReader();
    reader.onloadend = function() {
      callback(reader.result);
    }
    reader.readAsDataURL(xhr.response);
  };
  xhr.open('GET', url);
  xhr.responseType = 'blob';
  xhr.send();
}

toDataURL('http://localhost:8080/articles/Pic_test', function(dataUrl) {
    var display = document.getElementById('display');
  var url = window.URL.createObjectURL(new Blob([dataUrl]));
var img = new Image();
img.src = url;
document.getElementById("demo").innerHTML = img.src;
})

However, if I call my "img" Blob in java code, i have an error in server, but if I call it byte[], my picture is not shown just. 但是,如果我用Java代码调用“ img” Blob,则服务器出错,但如果将其命名为byte [],则我的图片就不会显示出来。

I can't comment the java part since I know nothing about it, but for the javascript one, what you do is... not correct. 我对Java部分一无所知,因为我对此一无所知,但是对于javascript,您所做的是……不正确。

You don't seem to understand what is a data URL, nor what you are doing here. 您似乎不明白什么是数据URL,也不清楚您在这里做什么。

So a data URL is a string, made of an header and of some file content ( data:|mime/type;|file-content ). 因此,数据URL是由标头和某些文件内容( data:|mime/type;|file-content )组成的字符串。
A data URL is an URL that points to itself, useful to embed data that should normally be served from network. 数据URL是指向自身的URL,有助于嵌入通常应从网络提供的数据。
Quite often, the file content part is encoded as base64, because the URI scheme is limited in its set of allowed characters, and that binary data couldn't be represented in this scheme. 通常, 文件内容部分被编码为base64,因为URI方案在其允许的字符集中受到限制,并且该方案无法表示二进制数据。

Now let's see what you are doing here... 现在,让我们看看您在这里做什么...

You are downloading a resource as a Blob. 您正在将资源下载为Blob。 That's good, Blob are perfect objects to deal with binary data. 很好,Blob是处理二进制数据的理想对象。
Then, you read this Blob a data URL. 然后,您为该Blob读取数据URL。 Less good, but I can see the logic, <img> can indeed load images from data URLs. 不太好,但是我可以看到逻辑, <img>确实可以从数据URL加载图像。

But then from this data URL string, you create a new Blob! 但是,然后从该数据URL字符串创建一个新的Blob! This is completely wrong. 这是完全错误的。 The Blob you just created with new Blob([dataUrl]) is a text file , not your image file in any way. 您刚刚使用new Blob([dataUrl])创建的new Blob([dataUrl])是一个文本文件 ,而不是您的图像文件。 So yes, the data is still hidden somewhere in the base64 data which is itself in the data URL, but what your poor <img> will see when accessing the data hooked by the Blob URI is really just text, data:image/png;base64,iVBORw0... and not at all PNG... like its parsing algo can read. 因此,是的,数据仍然隐藏在本身位于数据URL中的base64数据中,但是当访问Blob URI挂钩的数据时,可怜的<img>会看到的实际上只是文本, data:image/png;base64,iVBORw0...根本 PNG...解析算法可以读取一样。

So the solution is quite easy: get rid of the FileReader step. 因此,解决方案非常容易:摆脱FileReader步骤。 You don't need it. 不用了

 var xhr = new XMLHttpRequest(); xhr.open('get', 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'); xhr.responseType = 'blob'; xhr.onload = display; xhr.send(); function display(evt) { // we did set xhr.responseType = "blob" var blob = evt.target.response; // so this is a Blob // hence, no need for anything else than var url = URL.createObjectURL(blob); var img = new Image(); img.src = url; document.body.appendChild(img); } 

And if I may, all your thing could also just be 如果可以的话,你所有的事情也可能只是

document.getElementById('display').src = 'http://localhost:8080/articles/Pic_test';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM