cancel
Showing results for 
Search instead for 
Did you mean: 

How can i upload a file into alfresco use java code ?

zengqingyi12
Champ in-the-making
Champ in-the-making
I try to use httpclient to upload a file into alfresco, but failed.
which web script should i use ?
any sample code ?
Thanks in advance ~~~
2 REPLIES 2

jpotts
World-Class Innovator
World-Class Innovator
Shameless plug from the Alfresco Developer Guide:

These examples show you how to create a node using the Foundation API (Java), JavaScript, and Web Services:

Foundation

ChildAssociationRef association = nodeService.createNode(companyHome,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX,
name),
ContentModel.TYPE_CONTENT,
contentProps);

JavaScript

var newNode = space.createNode(nodeName, contentType);

Web Services

// Construct CML statement to create content node
CMLCreate createDoc = new CMLCreate("ref1", docParent, null, null, null, Constants.createQNameString(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, getContentType()), contentProps);
// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {createDoc});
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);

These examples show how to write content to the node once it is created:

Foundation

ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
String text = "The quick brown fox jumps over the lazy dog";
writer.putContent(text);

JavaScript

var newDoc = targetFolder.createFile(filename);
newDoc.properties.content.write(content);
newDoc.properties.content.mimetype = mimetype;
newDoc.save();

Web Services

ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
String docText = "This is a sample " + getContentType() + " document called " + getContentName();
Content docContentRef = contentService.write(docRef, Constants.PROP_CONTENT, docText.getBytes(), contentFormat);

As you mentioned, you can also create new nodes in a RESTful way via web scripts. A few examples using curl to post against CMIS web scripts to create new content are at http://ecmarchitect.com/archives/2009/04/10/959.

Jeff

timsiddle
Champ in-the-making
Champ in-the-making
Shameless plug from the Alfresco Developer Guide:


Plug or no plug - I'll vouch for the book. It's quality and a MUST if you are going to start developing for alfresco. To be honest, even an experienced developer would find going through the wiki's to find the required data a little confusing. The book is a handy help up - it'll give you the basics and you'll be flying before you know it.