Copy document to filesystem

How can I copy a document to file system?

I tried using Groovy script:

String source = reportPDF.getUrl ()
String destination = '\\\\192.168.1.9\\Documents\\Clients\\Demo Client\\Reports\\'

File.copy (source, destination)

But .getUrl () doesn't have the full path of the document.

The idea is that once the document is validated with a human task, it is copied to the clients' folders automatically.

Hi,

I wondered why this was not part of a standard example in the documentation as well when i needed that info. So i came up with this that works in a groovy script, just replace [document_name] with the name of your document in the process.

 

import org.bonitasoft.engine.bpm.document.Document
import org.bonitasoft.engine.bpm.document.DocumentValue
import org.slf4j.Logger
import org.slf4j.LoggerFactory;

try {
        
    logger.info("Export du fichier attaché au process : " +[document_name].getContentFileName());
    DocumentValue content = new DocumentValue(apiAccessor.getProcessAPI().getDocumentContent([document_name].getContentStorageId()), [document_name].getContentMimeType(), [document_name].getContentFileName())
    String fichierDestination = "c:\\Temp\\"+[document_name].getContentFileName();
    logger.info("Vers la destination : "+fichierDestination);
    FileOutputStream fos = new FileOutputStream(fichierDestination);
    fos.write(content.getContent());
    fos.close();
 
}

catch(Exception ex)
{

    logger.error(ex.toString());
    logger.error(ex.getMessage());
    logger.error(ex.getStackTrace());
}

Maybe there was a simpler way to do that.

 

Hi Walo,

Keep in mind that Bonita is a server application. It means that the network drive you are trying to copy the file int must be mounted on the server where Bonita runtime is running (and that the Bonita process has the permissions to write into that network folder).

Other than that, to retrieve the document content, you can just use the ProcessAPI like this:

import java.nio.file.Files import java.nio.file.Paths

def content = apiAccessor.getProcessAPI().getDocumentContent(myDocument.contentStorageId)
def outputFilePath = Paths.get(System.getProperty(“user.home”),“myDocument.pdf”)
Files.deleteIfExists(outputFilePath)
outputFilePath.toFile().withOutputStream { stream →
stream.write(content)
}

HTH
Romain

Hi Romain.

Works perfect!

Thank you so much!