Writing from Datahandler to file

bazic picture bazic · May 21, 2012 · Viewed 14.7k times · Source

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 :

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 :

DataHandler handler = fichier.getFichier();

handler.writeTo(os);

this take 2 minutes for uploading 700Mo.

what are other efficient ways ?

thanks

Answer

giuliannax picture giuliannax · Apr 22, 2016

I suggest you to use the class IOUtils of Apache Commons IO https://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);