java 从数据处理程序写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10686979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing from Datahandler to file
提问by bazic
I created a web service using CXF/MTOM for transfering large files (over 700Mo), i managed to transfer the file to the server , now my question is to optimze writing data in disk, i will give examples :
我使用 CXF/MTOM 创建了一个用于传输大文件(超过 700Mo)的网络服务,我设法将文件传输到服务器,现在我的问题是优化在磁盘中写入数据,我将举例:
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);
}
Using bytes can lead me to an OutOfMemory, so i'd rather use this one :
使用字节会导致我出现 OutOfMemory,所以我宁愿使用这个:
DataHandler handler = fichier.getFichier();
handler.writeTo(os);
this take 2 minutes for uploading 700Mo.
上传 700Mo 需要 2 分钟。
what are other efficient ways ?
什么是其他有效的方法?
thanks
谢谢
回答by giuliannax
I suggest you to use the class IOUtilsof Apache Commons IOhttps://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/input/package-summary.html
我建议你使用类IOUtils的阿帕奇百科全书IO https://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/input/package-摘要.html
QN: org.apache.commons.io.IOUtils
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);