Доступ к загруженным файлам из задачи Camunda

Последняя задача процесса Camunda должна записать файлы, загруженные пользователем, в определенную папку. Итак, я создал следующую «служебную задачу» как последнюю из процесса:

Заключительное задание

Затем из проекта Java я добавил класс FinishArchiveDelegate со следующим кодом:

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());
    }
}

Когда я проверяю журналы, я вижу, что вижу имена документов, например:

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

Проблема в том, что он показывает только имя файла, и мне нужно было бы запросить его из базы данных Камунды, чтобы скопировать его в другую папку. Любое предложение или идея?

Спасибо!


person Fel    schedule 22.11.2019    source источник


Ответы (1)


После некоторых тестов я понял, что могу получить не только имя, но и все содержимое загруженных файлов с помощью execution.getVariable(DOCUMENT_VARIABLE_NAME). Вот что я сделал:

// 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();

Надеюсь, это кому-то поможет, ура!

person Fel    schedule 26.11.2019