gpt4 book ai didi

Java大文件保存在数据库中-对象设计

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:35 24 4
gpt4 key购买 nike

我正在尝试找出对象设计来使用 hibernate 在数据库中实现大文件(~600 MB)存储库。请提出正确的方法/设计?

class ModelClass{

String name; //meta data
...

Option 1.
byte[] file; // dont want to load the content of the entire file
// in memory by using this but hibernate recognizes
// this datatype
Option 2.
InputStream inputStream;
OutputStream outputStream;
// I can have the methods to provide the input or output stream
// but i dont think its a clean approach. I am not sure how
// I will be able to work with hibernate with streams
Option 3.
File fileHandle;
}

还有其他选择吗?

我想调用hibernateTemplate的save(Object)方法将对象保存到数据库中。不知道我是否应该只在类中包含元数据并单独处理文件保存和检索。

提前致谢。

另一个可行的解决方案是使用“工作”界面。目的是避免将文件内容加载到内存中。

session.doWork(new Work(){
@Override
public void execute(Connection conn) {
//direct sql queries go here
}

});

最佳答案

我编写了一个 SerializedFile 类,它将数据保存在文件中。当读取对象时,它会创建一个临时文件。这是:

public class SerializableFile implements Serializable {
private static final File TEMP_DIR = getTempDir();

private transient boolean temporary;
private transient String name;
private transient File file;

public SerializableFile() {
}

public SerializableFile(File file) {
this.file = file;
this.name = file.getName();
this.temporary = false;
}

@Override
protected void finalize() throws Throwable {
dispose();
super.finalize();
}

public void dispose() {
if (temporary && file != null) {
file.delete();
file = null;
}
}

public File keep(String name) throws IOException {
if (temporary) {
temporary = false;
} else {
File newFile = new File(TEMP_DIR, name);
keepAs(newFile);
file = newFile;
}
return file;
}

public void keepAs(File outFile) throws IOException {
if ((temporary || file.equals(outFile)) && file.renameTo(outFile)) {
temporary = false;
file = outFile;
} else {
InputStream in = new FileInputStream(file);
try {
OutputStream out = new FileOutputStream(outFile);
try {
byte buf[] = new byte[4096];
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
} finally {
in.close();
}
outFile.setLastModified(file.lastModified());
}
}

public String getName() {
return name;
}

public File getFile() {
return file;
}

public long lastModified() {
return file.lastModified();
}

private void writeObject(ObjectOutputStream out) throws IOException {
int size = (int)file.length();
long date = file.lastModified();
out.writeUTF(name);
out.writeInt(size);
out.writeLong(date);
InputStream in = new FileInputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
in.close();
}
}

private void readObject(ObjectInputStream in) throws IOException {
name = in.readUTF();
int size = in.readInt();
long date = in.readLong();
file = File.createTempFile("tmp", ".tmp", TEMP_DIR);
OutputStream out = new FileOutputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf, 0, size <= buf.length ? size : buf.length);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
out.close();
}
file.setLastModified(date);
temporary = true;
}

private static File getTempDir() {
File dir;
String temp = System.getProperty("com.lagalerie.live.temp-dir");
if (temp != null) {
dir = new File(temp);
} else {
String home = System.getProperty("user.home");
dir = new File(home, "temp");
}
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new RuntimeException("Could not create temp dir " + dir);
}
return dir;
}
}

关于Java大文件保存在数据库中-对象设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7182151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com