如何在Java中将zip文件移动到blob列? [英] How do I move zip file to blob column in Java?

查看:378
本文介绍了如何在Java中将zip文件移动到blob列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java程序,它创建了许多xml文件,然后将它们压缩并将它们保存到文件系统中。稍后在程序中我想将相同的zip文件放入我的oracle数据库的blob列中。问题是我不知道该怎么做。我不需要阅读它或对数据做任何事情,只需将其移动到数据库以进行持久的中央存储。

I have a Java program that creates a number of xml file and then zips them up and saves them to the file system. Later in the program I want to put that same zip file into a blob column of my oracle database. Problem is I'm not sure how to do that. I don't need to read it or do anything with the data, just move it to the database for persistent, central storage.

谢谢!

推荐答案

有几种方法可以做到这一点,但 PreparedStatement.setBinaryStream 可能是最好的方法。

There are several ways you can do this, but PreparedStatement.setBinaryStream is probably the best way.

public void saveFileToDatabase(File file) {
  InputStream inputStream = new FileInputStream(file);

  Connection conn = ...;
  PreparedStatement pS = conn.prepareStatement(...);
  ...
  pS.setBinaryStream(index, inputStream, (int) file.length());
  ...
  pS.executeUpdate();
}

(请注意,为简单起见,我没有包含任何必要的尝试/捕获关闭连接 PreparedStatement InputStream 的东西,但你需要这样做。)

(Note that for simplicity I didn't include any of the necessary try/catch stuff for closing the Connection, PreparedStatement and InputStream, but you would need to do that.)

通过这种方式,数据将从文件流式传输到数据库,而无需一次性加载到内存中。

Done this way, the data will be streamed from the file to the database without having to be loaded in to memory all at once.

这篇关于如何在Java中将zip文件移动到blob列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆