Fel
Fel

Reputation: 4818

Accessing uploaded files from Camunda task

The final task of a Camunda process must write the files uploaded by the user to an specific folder. So, I've created the following 'Service task' as the last one of the process:

Final task

Then, from the Java project, I've added the FinishArchiveDelegate class with the following code:

package com.ower.abpar.agreements;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class FinishArchiveDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("Process finished: "+execution.getVariables());
    }
}

When I check the logs, I see that I can see the document names, like:

document_1 => FileValueImpl [mimeType=image/jpeg, filename=Test_agreement1.jpg, type=file, isTransient=false]

The problem is that it only shows the file name and I'd need to request it from Camunda's database to copy it to another folder. Any suggestion or idea?

Thanks!

Upvotes: 0

Views: 2018

Answers (1)

Fel
Fel

Reputation: 4818

After some tests, I realized that I can get not only the name but all the uploaded files content using execution.getVariable(DOCUMENT_VARIABLE_NAME). So this is what I did:

// Get the uploaded file content
Object fileData = execution.getVariable("filename");

// The following returns a FileValueImpl object with metadata 
// about the uploaded file, such as the name
FileValueImpl fileMetadata = FileValueImpl)execution.getVariableLocalTyped("filename")

// Set the destination file name
String destinationFileName = DEST_FOLDER + fileMetadata.getFilename();
...
// Create an InputStream from the file's content
InputStream in = (ByteArrayInputStream)fileData;
// Create an OutputStream to copy the data to the destination folder
OutputStream out = new FileOutputStream(destinationFileName);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
}
in.close();
out.close();

Hope this helps someone, cheers!

Upvotes: 0

Related Questions