如何从 URL 检索图像并将其存储为 Java 中的 Blob(谷歌应用引擎)

发布于 2024-08-25 03:49:39 字数 270 浏览 5 评论 0 原文

我了解如何获取 URL 文本页面并循环结果

URL url = new URL(this.url);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
    ....

我该如何执行此操作来获取图像并将其存储为 Blob?

I understand how to fetch a URL text page and loop over the results

URL url = new URL(this.url);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
    ....

How would I do this to fetch an image and store it as a Blob?

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

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

发布评论

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

评论(3

马蹄踏│碎落叶 2024-09-01 03:49:39

只需将其直接作为 InputStream 并使用 PreparedStatement#setBinaryStream() 来存储它。它是二进制数据,而不是字符数据,因此 Reader 只会把事情弄乱,您不希望这样。

简而言之:

InputStream input = imageUrl.openStream();

// ...

statement = connection.prepareStatement("INSERT INTO image (content) VALUES (?)");
statement.setBinaryStream(1, input);
statement.executeUpdate();

PreparedStatement 非常有用。它不仅可以帮助您避免 SQL 注入,而且还可以轻松设置像 InputStream。您可以在 PreparedStatement 的更多信息>JDBC教程

Just get it straight as InputStream and use PreparedStatement#setBinaryStream() to store it. It's binary data, not character data, so a Reader would only messup things, you don't want to have that.

In a nutshell:

InputStream input = imageUrl.openStream();

// ...

statement = connection.prepareStatement("INSERT INTO image (content) VALUES (?)");
statement.setBinaryStream(1, input);
statement.executeUpdate();

A PreparedStatement is really useful. It not only saves you from SQL injections, but it also eases setting fullworthy Java objects like InputStream in a SQL statement. You can learn more about PreparedStatement at the JDBC tutorial.

晨曦÷微暖 2024-09-01 03:49:39

我存储二进制数据(例如图像)的方法非常简单。就我而言,我处理发布到 servlet 的上传文件。在 servlet 内部,我使用 request.getInputStream() 获取发布主体上的 InputStream。然而,这适用于任何类型的InputStreawm,包括基于URL 的InputStreawm。下面的示例代码展示了如何将该 InputStream 转换为 google appeninge Blob,然后您可以将其持久保存在数据存储中。

...
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.Blob;
...

public void InputStreamToBlob(InputStream i) throws IOException {

    ...

    Blob content = new Blob(IOUtils.toByteArray(i));
    ...

Well what I do to store binary data such as images, is quite simplictic. In my case I deal with uploaded files which get posted to a servlet. Inside the servlet, I get the InputStream on the posted body with request.getInputStream(). However, this works with any kind of InputStreawm, inlcuding one based on an URL. The sample code below shows how to convert that InputStream into google appeninge Blob, which then you can for instance make persistant in the data store.

...
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.Blob;
...

public void InputStreamToBlob(InputStream i) throws IOException {

    ...

    Blob content = new Blob(IOUtils.toByteArray(i));
    ...
ぃ双果 2024-09-01 03:49:39

对于图像,您不是逐行读取缓冲区,而是将其加载到字节数组中,并将该字节数组保存为 blob。

For an image, instead of reading the buffer line by line, you'll load it in byte array, and save this byte array as a blob.

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