Writing from Datahandler to file

15,285

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);
Share:
15,285
bazic
Author by

bazic

Updated on June 04, 2022

Comments

  • bazic
    bazic almost 2 years

    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