将图像从 MySQL 卸载到磁盘

发布于 2024-08-15 06:31:18 字数 147 浏览 8 评论 0原文

我将图像作为 blob 存储在 MySQL 中(我知道这是错误的)。而且有很多。有没有一种快速的方法可以将它们全部放到磁盘上,例如 SELECT .. INTO OUTFILE ,但可以将多个文件插入一个文件?或者唯一的方法是编写一个可以迭代行并保存图像的脚本?

I have images stored in MySQL as blobs (i know it's wrong). And there are many of them. Is there any fast way to drop them all on disk, like SELECT .. INTO OUTFILE, but to many files insted of one? Or the only way is writing a script that will iterate over rows and save images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你不解释 2024-08-22 06:31:18

由于您希望将它们保存到磁盘上的不同文件中,因此您必须使用脚本。

Since you want them to be saved into different files on the disk you'll have to go for a script.

心作怪 2024-08-22 06:31:18
#!/usr/bin/perl

#Note: it is my habit to name a Query Result $qR.

use strict;
use DBI;
my $dbh = DBI->connect(YOUR_INFO_HERE);

my $i = 0;
my $q = $dbh->prepare('select image from images');
while (my $qR = $q->fetchrow_arrayref) {
    open(FILE,'>',"$i.jpg");
    print FILE $qR[0];
    close FILE;
    $i++;
}
#!/usr/bin/perl

#Note: it is my habit to name a Query Result $qR.

use strict;
use DBI;
my $dbh = DBI->connect(YOUR_INFO_HERE);

my $i = 0;
my $q = $dbh->prepare('select image from images');
while (my $qR = $q->fetchrow_arrayref) {
    open(FILE,'>',"$i.jpg");
    print FILE $qR[0];
    close FILE;
    $i++;
}
赠佳期 2024-08-22 06:31:18

我有一个类似的要求,我发现在我的案例中使用 Java + Hibernate(在其他 Hibernate 变体中可能有类似的任务,但没有尝试过),很快就让我到达了那里。

我设置了一个像这样的映射:

<hibernate-mapping>
  <class name="<com.package.table>" table="table">
    <id column="pk" name="pk" type="int">
    </id>
    <property name="blobfield" type="blob"/>
  </class>
</hibernate-mapping>

一个用于携带数据的Java bean,类似于:

package com.package;
import java.sql.Blob;
...
public class table {
...
public Blob getBlobfield {
...

和一个类似这样的循环:

 ...
 tx = session.beginTransaction();
 Criteria crit = session.createCriteria(table.class);
 crit.setMaxResults(50); // Alter this to suit...
           List<table> rows = crit.list();
           for (table r: rows) {
               ExtractBlob(r.getId(), r.getBlobField);
 }

还有一些东西(我称之为“ExtractBlob”)来提取blob(使用PK来生成一个文件名),像这样:

...

 FileOutputStream fout=new FileOutputStream(<...base output file on PK for example...>
 BufferedOutputStream bos=new BufferedOutputStream(fout);
 InputStream is=blob.getBinaryStream();
 byte[] b=new byte[8192];
 while ( (is.read(b))>0 ) {
       bos.write(b);
 }
 is.close();
 bos.close()

...

如果您认为它可能有用,我可以发布一个更完整的示例 - 但我会从更大的项目中提取代码,否则我会直接发布它。

I had a similar requirement , I found in my case using Java + Hibernate (possibly similar task in other Hibernate variations, but not tried it), got me there quite quickly.

I set up a mapping like this:

<hibernate-mapping>
  <class name="<com.package.table>" table="table">
    <id column="pk" name="pk" type="int">
    </id>
    <property name="blobfield" type="blob"/>
  </class>
</hibernate-mapping>

A Java bean to carry the data, something like:

package com.package;
import java.sql.Blob;
...
public class table {
...
public Blob getBlobfield {
...

And a loop something like this:

 ...
 tx = session.beginTransaction();
 Criteria crit = session.createCriteria(table.class);
 crit.setMaxResults(50); // Alter this to suit...
           List<table> rows = crit.list();
           for (table r: rows) {
               ExtractBlob(r.getId(), r.getBlobField);
 }

And something ("ExtractBlob" is I'm calling this) to extract the blob (using the PK to generate a filename), something like this:

...

 FileOutputStream fout=new FileOutputStream(<...base output file on PK for example...>
 BufferedOutputStream bos=new BufferedOutputStream(fout);
 InputStream is=blob.getBinaryStream();
 byte[] b=new byte[8192];
 while ( (is.read(b))>0 ) {
       bos.write(b);
 }
 is.close();
 bos.close()

;

...

I can post a more complete example if you looks like it might be useful - but I would have extract the code from a bigger project, otherwise I would have just posted it straight up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文