gpt4 book ai didi

java - 从 Datahandler 写入文件

转载 作者:行者123 更新时间:2023-11-30 09:36:36 24 4
gpt4 key购买 nike

我使用 CXF/MTOM 创建了一个用于传输大文件(超过 700Mo)的 Web 服务,我设法将文件传输到服务器,现在我的问题是优化磁盘中的数据写入,我将举例说明:

DataHandler handler = fichier.getFichier();

InputStream is = handler.getInputStream();

OutputStream os = new FileOutputStream(new File("myFile"));


byte[] buffer = new byte[BUFFER];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer,0,bytesRead);
}

使用字节会导致内​​存不足,所以我宁愿使用这个:

DataHandler handler = fichier.getFichier();

handler.writeTo(os);

上传 700Mo 需要 2 分钟。

还有哪些有效的方法?

谢谢

最佳答案

我建议你使用 Apache Commons IOIOUtilshttps://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/input/package-summary.html

QN: org.apache.commons.io.IOUtils

DataHandler handler = docClient.getContent(sid, docId);

InputStream is = handler.getInputStream();
OutputStream os = new FileOutputStream(new File("C:/tmp/myFile.raw"));

// This will copy the file from the two streams
IOUtils.copy(is, os);

// This will close two streams catching exception
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(is);

关于java - 从 Datahandler 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10686979/

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