setFileDocumentContent doesnt copy content

0
I have a custom java snippet that takes a list of objects (that inherit the FileDocument type). For each one I want to make a copy and copy the contents from the old object. The content DOESN’T copy, though. There are no errors. Other attributes are set, but the filedocument back in mendix shows HasContent=true, Size=0 and when I download the new object, indeed the size is 0. Does anyone know why the “newobject” content doesn’t seem to copy? My testfiles are only about 40K in size.   for(int a = 0;a<length;a++) { InputStream inputStream; TestFile myobject = this.Parameter.get(a); inputStream = Core.getFileDocumentContent(getContext(), __Parameter.get(a)); String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); myobject.setStringBody(result); myobject.commit(); IMendixObject newobject = Core.instantiate(getContext(), "MyModule.TestFile"); Core.storeFileDocumentContent(getContext(), newobject, "TEST.pdf", inputStream); Core.commit(getContext(), newobject); // this should copy it // the following code just converts the content to string and copies to a String field just to double check the contents InputStream inputStream2; inputStream2 = Core.getFileDocumentContent(getContext(), newobject); String result2 = IOUtils.toString(inputStream2, StandardCharsets.UTF_8); newobject.setValue(getContext(), "StringBody", result2); Core.commit(getContext(), newobject); }  
asked
1 answers
2

It looks like you are using the InputStream to create a String then trying to reuse it as part of the storeFileDocument. The InputStream won’t have any content at this point as it’s already been read. You also need to make sure it is closed if it isn’t already. I suggest calling Core.getFileDocumentContent() again to get a fresh InputStream to use when you call Core.storeFileDocumentContent().      

answered