如何从BLOB中获取图片的相对路径,而不是使用com.mysql.cj.jdbc.Blob@28763983?

huangapple go评论68阅读模式
英文:

How to get relative path of picture from BLOB instead of this com.mysql.cj.jdbc.Blob@28763983?

问题

我上传了一些图片到我的数据库,并想获取它们的路径,但我总是得到类似于这样的东西:com.mysql.cj.jdbc.Blob@28763983,对于每张图片都是如此。这是代码:

public static List<Image> selectImages() {
    ResultSet rs = null;
    try {
        imageList.clear();
        String selectImages = "select * from image";
        PreparedStatement ps = DataBaseConnection.get().prepareStatement(selectImages);
        rs = ps.executeQuery(selectImages);
        while (rs.next()) {
            int idImage = rs.getInt("id");
            Blob viewImage = rs.getBlob("image");
            Image image = new Image(idImage, viewImage);
            imageList.add(image);
        }
        return imageList;

如何获取真实路径而不是那些无意义的内容?

英文:

I uploaded a couple of pictures in my database and i want to get their path, but i always get something like this: com.mysql.cj.jdbc.Blob@28763983 for every picture. This is the code:

  public static List&lt;Image&gt; selectImages() {
    ResultSet rs = null;
    try {
        imageList.clear();
        String selectImages = &quot;select * from image&quot;;
        PreparedStatement ps = DataBaseConnection.get().prepareStatement(selectImages);
        rs = ps.executeQuery(selectImages);
        while (rs.next()) {
            int idImage = rs.getInt(&quot;id&quot;);
            Blob viewImage = rs.getBlob(&quot;image&quot;);
            Image image = new Image(idImage, viewImage);
            imageList.add(image);
        }
        return imageList;

How can i get a real path instead of that gibberish?

答案1

得分: 1

那个“胡言乱语”是来自java.lang的**Object.toString()**的默认输出。
您的代码片段没有显示它,但我假设您正在迭代那个图像列表并打印Blob;类似这样

for (image : selectImages()) {
    System.out.println(image.getViewImage());
}

快速查看mysql jdbc Blob的API文档,似乎没有立即可用的方法来实现您想要的功能。
http://www.docjar.com/docs/api/com/mysql/jdbc/Blob.html

听起来您想要**File.getAbsolutePath()**的输出。

本文演示了如何在“readBlob”方法中从数据库中获取Blob作为文件;https://www.mysqltutorial.org/mysql-jdbc-blob/

或者,将Blob转换为文件;https://www.baeldung.com/convert-input-stream-to-a-file

File targetFile = new File("src/main/resources/targetFile.tmp");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);

您可能注意到这两种方法的共同之处是fileName字符串是外部变量,并未存储在Blob本身内。

所以这里的故事教训是Blob对象只存储在内存中,尚未写入文件。

英文:

That "gibberish" is the default output of Object.toString() from java.lang.
Your code snippet doesn't show it, but I assume you are iterating over that list of Images and printing the Blob; something like this

for (image : selectImages()) {
    System.out.println(image.getViewImage());
}

Taking a quick look at the API docs for the mysql jdbc Blob, there doesn't appear to be any immediately available methods for achieving what you want.
http://www.docjar.com/docs/api/com/mysql/jdbc/Blob.html

It sounds like you want the output of File.getAbsolutePath()

This article demonstrates how to fetch a Blob from the database as a File in the "readBlob" method; https://www.mysqltutorial.org/mysql-jdbc-blob/

Or alternatively, convert the Blob to a File; https://www.baeldung.com/convert-input-stream-to-a-file

File targetFile = new File(&quot;src/main/resources/targetFile.tmp&quot;);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);

What you may notice about both of these approaches, is that the fileName String is an external variable and is not stored within the Blob itself.

So the moral of the story here is that the Blob object is only stored in memory, it isn't written to a file yet.

huangapple
  • 本文由 发表于 2020年8月7日 20:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63301718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定