How to Convert DocumentValue into File (DataType) in Groovy Script

1
0
-1

Hello every body,

I trying post form-data (multipart with some files and some part) during the process.
I know how to convert document into documentValue but i need convert documentValue into File.
How can I do that?

Thanks a lot.
Ali

1 answer

1
+1
-1
This one is the BEST answer!

In Continue i resolved it.

`import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.mime.MultipartEntity
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.content.StringBody
import org.apache.http.impl.client.HttpClientBuilder
import org.bonitasoft.engine.bpm.document.DocumentValue
import org.bonitasoft.engine.io.IOUtil

@SuppressWarnings("deprecation")

// create HTTP Client
def dump = 'Sucess';

MultipartEntity entity = new MultipartEntity();

byte[] content = apiAccessor.getProcessAPI().getDocumentContent(personPic.getId().toString())
String fileName = personPic.getName()
File personPicFile = File.createTempFile("tmp", fileName);
IOUtil.write(personPicFile, content);
entity.addPart("picture", new FileBody(personPicFile));
personPicFile.delete();

return dump;`

If any other way can help please, share it.

Thanks,
Ali

Comments

Submitted by Dibyajit.Roy on Sun, 04/28/2019 - 17:22

Hello

Have you tried FileOutputStream. I use outputstream classes to generate any file type i want. Just pass the correct mime type.

Regards

Submitted by the.shirini on Sun, 04/28/2019 - 17:28

Hi Roy,

In this case i use FileOutputStream :

String encodedContent = passportData.getAt("FaceImageData");
encodedContent.replaceAll("\r", "")
encodedContent.replaceAll("\n", "")
byte[] decodedContent = encodedContent.decodeBase64()

//Convert byte to File
File input = new File("/tmp/img/"+processInstanceId+"_personPic.png");
FileOutputStream fop = new FileOutputStream(input);
fop.write(decodedContent);
fop.flush();
fop.close();

But in above case i have DocumentValue and need Convert into File.

Thanks.
Ali

Submitted by Dibyajit.Roy on Sun, 04/28/2019 - 18:18

Thanks for sharing the code.
I hope that anyone else having similar issues will benefit from it.

Thanks

Notifications