Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Uploading Content
First, it is important to note that the alfresco SDK with all its included jars is missing one. I had to download wss4j-1.5.10.jar from http://www.apache.org/dyn/closer.cgi/ws/wss4j/ . Here is a full list off all the jar files I am using that alfresco needs. activation.jar alfresco-web-service-client-3.4.b.jar axis-1.4.jar commons-discovery-0.2.jar commons-logging-1.1.jar jaxrpc.jar mail.jar opensaml-1.0.1.jar wsdl4j-1.6.2.jar wss4j-1.5.10.jar xalan.jar xercesImpl-2.8.0.jar xmlsec-1.4.1.jar
Java Code
Page 1 of 6
Alfresco
String ASSOC_CONTAINS =
"{http://www.alfresco.org/model/content/1.0}contains";
update(cml); Reference newContentNode = result[0].getDestination(); Content content = contService.write( newContentNode, Constants.PROP_CONTENT, "Just Some Text".getBytes(), contentFormat); } catch (AuthenticationFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } public static void main(String...args) { Properties prop = new Properties(); try { prop.load(new FileInputStream( "properties/alfresco.properties")); UploadContent upCont = new UploadContent(prop); } catch (IOException ex) { ex.printStackTrace(); } } }
try { WebServiceFactory.setEndpointAddress( prop.getProperty("repository.location")); AuthenticationUtils.startSession( prop.getProperty("dev.username"), prop.getProperty("dev.password")); // Get the content service ContentServiceSoapBindingStub contService = WebServiceFactory.getContentService(); // Create a parent reference ParentReference parRef = new ParentReference(STORE, null, "/app:company_home/st:sites" + "/cm:mySite/cm:documentLibrary/" + "cm:Recording/cm:Jan/cm:" + ISO9075.encode("20101130"), ASSOC_CONTAINS, "{" + Constants.NAMESPACE_CONTENT_MODEL + "}" + fileName); // Define the content format for the content //we are adding ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8"); NamedValue[] properties = new NamedValue[]{Utils.createNamedValue( Constants.PROP_NAME, fileName)}; CMLCreate create = new CMLCreate("1", parRef, null, null, null, Constants.TYPE_CONTENT, properties); CML cml = new CML(); cml.setCreate(new CMLCreate[]{create}); UpdateResult[] result = WebServiceFactory.getRepositoryService().
Page 2 of 6
Alfresco And here is the properties file alfresco.properties (enter in your information) repository.location=http://localhost:8080/alfresco/api dev.username=admin dev.password=adminPassword We cant give it a simple path like this as the path it wants is an xPath. The only things to concern yourself with at this point is that company_name requires app: before it, sites requires st: before it and all others require cm: before their names. Also there are some issues with numbers, and I believe a few other things To deal with this you can encode a string to the ISO9075 format using the ISO9075.encode(String_to_encode) method. In this first example I am uploading a simple text file. In fact I am doing it inline without an actual text file.
And here is where the content is uploaded. Instead of reading from a file we are just feeding it the bytes from a normal String. One thing you will notice is that to use this method you need to have the entire file in memory (all the bytes). This is fine for small files but for larger files you need to use a streaming service like UploadContentServlet which is explained somewhat here
http://wiki.alfresco.com/wiki/URL_Addressability#UploadContentServlet [1] and here http://forums.alfresco.com/en/viewtopic.php?f=27&t=24427 [2].
In this example I created a site using the alfresco share interface and I called the site mySite and in the Document Library I created a folder called Month, in that folder I have a folder called Jan, and within that folder I have another folder called 20101130. If this was a normal file directory it would be /company_home/sites/mySite/documentLibrary/Month/Jan/20101130/
Page 3 of 6
Alfresco Running this code will place a text file with the text Just Some Text in the given location on the alfresco share site. Now what if we wanted to upload information from an actual text file?
public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length();
I added this little bit of code to read a file entirely into a byte stream
if (length > Integer.MAX_VALUE) { throw new IOException( "File is too large for this method"); } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException( "Could not completely read file" +file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }
Page 4 of 6
Build and ran the new code and it worked like a charm.
Page 5 of 6
Alfresco
References
[1] UploadContentServlet
http://wiki.alfresco.com/wiki/URL_Addressability#UploadContentServlet
Visited 01/2011 [2] Upload large files using Alfresco, Forum topic http://forums.alfresco.com/en/viewtopic.php?f=27&t=24427 Visited 01/2011
Here are some mime types for different types of content Content type Text Image png Image jpeg PDF MP3 (MPEG Audio) Mime type text/plain image/png image/jpeg application/pdf audio/x-mpeg
I also ran across this page with some code that seems to outline a lot of mime types alfresco uses. http://forge.alfresco.com/plugins/scmsvn/viewcvs.php/trunk/Record.cs ?root=outlook-addin&view=markup
Page 6 of 6