java - 使用 apache IOUtils.copy() 和 setContent() 复制文档内容

标签 java copy apache-commons documentum

我想将存储在一个文档库中的一个对象的内容复制到存储在另一个文档库中的另一个对象。我不想创建文件,因为我有超过 300k 的文件要复制。以下是我的部分代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

IOUtils.copy(source.getContent(), baos);

[...]
targetObj.setContent(baos); // Documentum DFC
targetObj.save(); // Documentum DFC

如果我不调整 JVM,IOUtils.copy(source.getContent(), baos); 给出java.lang.OutOfMemoryError: Java heap空格.

如果我通过设置 Xmx 最大值来调整 JVM,之前的指令没问题,但是 java.lang.OutOfMemoryError: Java heap space 发生在 targetObj.setContent(baos);.

只有 8332175 字节的大内容... (7.94 MB)

知道哪里出了问题吗?从 ByteArrayInputStream 复制到 ByteArrayOutputStream 的更好方法?还有别的吗?


一些 Documentum API

getContent

public ByteArrayInputStream getContent() throws DfException

Copies this object's content from the Documentum server into a ByteArrayInputStream >object.

The following code example demonstrates how to copy an objects content from the >Documentum server into memory:

    IDfSysObject sysObj = (IDfSysObject)session.getObject(new DfId("0900d5bb8001f900"));
    ByteArrayInputStream bais = sysObj.getContent();
    if (bais.available() > 0)
    {
         // Data successfully fetched from the server...
    }

Returns: a ByteArrayInputStream object containing the objects content. Throws: DfException - if a server error occurs.

setContent

public boolean setContent(ByteArrayOutputStream content) throws DfException

Sets new content to an object. Use this method when you want to set data that resides >in working memory.

The following code example demonstrates how to set content residing in memory to a new document:

    IDfSysObject sysObj = (IDfSysObject)sess.newObject("dm_document");
    sysObj.setObjectName("testDoc");
    sysObj.setContentType("crtext");
    byte b[] = {35,36,37,38,39};
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(b, 0, 5);
    sysObj.setContent(out);
    sysObj.save();

Parameters: content - the content as a ByteArrayOutputStream. Throws: DfException - if a server error occurs.

最佳答案

只要您使用 ByteArrayOutputStream,数据就必须适合内存。

我对 Documentum 一无所知,但是否有 targetObj.setContent(File)setContent(InputStream),这样您就可以避免将整个 block 读入一个 byte[]?

(虽然 8MB 并不是那么大,也许您可​​以调整 Java 堆空间。它也可以帮助预先调整 BAOS 使用的缓冲区大小,您可以将初始大小传递给它的构造函数)

更新:您确定 setContent 接受 ByteArray Output 流吗?通常,setter 将从 InputStream 中读取。

关于java - 使用 apache IOUtils.copy() 和 setContent() 复制文档内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706853/

相关文章:

java - java中的时间和小时

java - 如何使用MySQL通过preparedStatement将数据从一张表复制到另一张表?

java - Apache Commons UrlValidator - 配置为允许变音字符

java - 使用 org.apache.commons.httpclient 时可以在命令行上设置代理吗?

java - 从方法返回 null 的最佳方式?

java - FFMpeg - 将多个 rtmp 流输入合并到单个 rtmp 输出

java - 尝试安装 rJava 但错误提示 JDK 不完整

android - 在 Android 中以编程方式将所有安装 apk 文件备份到 sdcard

batch-file - 多次复制文件夹及其内容,然后重命名

java - 如何修复 get/check/put 的非原子使用?