I recently stumbled upon the following question on TREX: http://tridion.stackexchange.com/questions/1379/not-able-to-upload-binary-images-using-core-service2011-api-from-java-client/
I had never actually attempted to upload a Binary and create a Multimedia Component using the Core Service. So I was intrigued how to achieve that.
The solution is pretty straightforward, but it does contain two steps:
For more information about the CoreServiceFactory object, have a look at me previous posts relating to Java Core Service clients.
I had never actually attempted to upload a Binary and create a Multimedia Component using the Core Service. So I was intrigued how to achieve that.
The solution is pretty straightforward, but it does contain two steps:
- First, upload the Binary to the TCM;
- Second, create the Multimedia Component referencing the path on TCM where the binary had been uploaded;
The sample code below accomplishes the first step:
private static File uploadFile(File file) {
try {
byte[] fileData = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();
IStreamUpload clientUpload =
CoreServiceFactory.getStreamUploadBasicHttpClientClient();
CoreServiceFactory.getStreamUploadBasicHttpClientClient();
String uploadFile = clientUpload.uploadBinaryByteArray(file.getName(), fileData);
return new File(uploadFile);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The method takes a local file (i.e. local on the client machine) and uploads it to the TCM using the Core Service StreamUploadBasicHttpClient endpoint. It returns a File object representing the location of the uploaded binary on the TCM (e.g. C:\Windows\TEMP\tmp2432.jpg).
Once we have the binary uploaded to the remote TCM, we can now create the Multimedia Component:
ICoreService client = CoreServiceFactory.getBasicHttpClient();
ReadOptions readOptions = new ReadOptions();
ComponentData componentData =
(ComponentData) client.getDefaultData(ItemType.COMPONENT, "tcm:1-1-2", readOptions);
componentData.setTitle("MMC " + System.currentTimeMillis());
LinkToSchemaData linkToSchema = new LinkToSchemaData();
linkToSchema.setIdRef("tcm:1-26-8");
componentData.setSchema(linkToSchema);
File localFile = new File("C:\\Beagle.jpg");
File remoteFile = uploadFile(localFile);
BinaryContentData binaryContentData = new BinaryContentData();
binaryContentData.setFilename(localFile.getName());
binaryContentData.setUploadFromFile(remoteFile.getAbsolutePath());
LinkToMultimediaTypeData
linkToMultimediaType = new LinkToMultimediaTypeData();
linkToMultimediaType.setIdRef("tcm:0-2-65544");
binaryContentData.setMultimediaType(linkToMultimediaType);
componentData.setBinaryContent(binaryContentData);
componentData = (ComponentData)
client.save(componentData, readOptions);
client.checkIn(componentData.getId(), true, null, readOptions);
}
The code is pretty straight forward:
- initial ComponentData is created using client.getDefaultData();
- Multimedia Schema is linked from ComponentData;
- BinaryContentData is created using the remote binary file;
- MultimediaType is identified and specified. This is probably the ugliest part of the entire appraoch. This logic could be written by first reading all available Multimedia Types, then identifying the one we need by matching on the binary mime type;
For more information about the CoreServiceFactory object, have a look at me previous posts relating to Java Core Service clients.
Note that the default Reader Quota for my Java client was 16384 bytes, and I got the following error message:
com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'UploadBinaryByteArray'. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 615.
The solution was to increase the MaxArrayLength reader quota on the WCF service by editing the file [TRIDION_HOME]\webservices\Web.config and to insert the following line below system.ServiceModel / bindings / basicHttpBinding / binding name="StreamUpload_basicHttpBinding":
<readerQuotas maxArrayLength="999999999" />
Comments