cancel
Showing results for 
Search instead for 
Did you mean: 

Upload file programatically to Shared Files

taylorjackson
Champ in-the-making
Champ in-the-making

Hey,

I'm running Alfresco one and need a java / curl solution to upload files from the local machine to the shared files doc area.  I can see there are other solutions out there that mention siteid (curl upload for example).  I have no sites configured nor do I want to use sites.  I just want to specify the folder the file to go into and have it uploaded there!

Any help is much appreciated.

1 ACCEPTED ANSWER

For alfresco cmis implementation document ,please refer to CMIS .

Following is example snippet

private Session getSession(String serverUrl, String username,
String password)

{

SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> params = new HashMap<String, String>();
params.put(SessionParameter.USER, username);
params.put(SessionParameter.PASSWORD, password);
params.put(SessionParameter.ATOMPUB_URL, serverUrl);
params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
params.put(SessionParameter.OBJECT_FACTORY_CLASS,
"org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
List<Repository> repos = sessionFactory.getRepositories(params);
if (repos.isEmpty()) {
throw new RuntimeException("Server has no repositories!");
}

return repos.get(0).createSession();
}

public void createDocument()

{
String serverUrl = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom";
String userName = "admin";
String password = "admin";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");


properties.put(PropertyIds.NAME, "Platform__technical_documentation20140601");
properties.put(PropertyIds.CREATED_BY, "admin");
properties.put("cm:title", "Title x_technical_documentation20140601");
properties.put("cm:description", "Platform_technical_documentation20140601 15M");
Session session = getSession(serverUrl, userName, password);

try {
Folder folder1 = (Folder)session.getObjectByPath("/TESTFOLDER4");
VersioningState vs = VersioningState.MAJOR;

File file = new File("C:\\userguide.pdf");
InputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[(int) file.length()];
dis.readFully(bytes);
ContentStream contentStream = new ContentStreamImpl(file
.getAbsolutePath(), BigInteger.valueOf(bytes.length), "application/pdf",
new ByteArrayInputStream(bytes));

Document newDocument = folder1
.createDocument(properties, contentStream, vs);

System.out.println(newDocument.getId());

} catch (CmisContentAlreadyExistsException ccaee) {
System.out.println("ERROR: Unable to Load - CmisContentAlreadyExistsException: " );
} catch (CmisConstraintException cce) {
System.out.println("ERROR: Unable to Load - CmisConstraintException: " );
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

View answer in original post

6 REPLIES 6

openpj
Elite Collaborator
Elite Collaborator

You have different solutions for this.

Bulk File System Import

If your file system where are stored these files can be directly mounted with the file system where the Alfresco repository is installed, you can consider to use the Bulk File System Import that is the official and supported tool by Alfresco for importing content into the repository:

Importing and transferring files | Alfresco Documentation 

Bash using cURL

$url ='http://admin:passwd@YourAlfrescoServer:8080/alfresco/service/api/upload'; 
$filename = 'yourContent.pdf'; $mimetype = mime_content_type($filename);
$postfields = array(
  'filedata' => '@' . $filename,
  'filename' => $filename,
  'siteid' => 'yourSite',
  'containerid' => 'documentLibrary',
  'uploaddirectory' => 'test',
  'contenttype' => 'cm:content');

Check the documentation of the upload service if you need a different behavior.

WebScripts with transaction none

Another smart approach can be based on implement a WebScript with the transaction setting to none for importing each content in a gradual way. You can implement it using ECMAScript or Java.

Hope this helps. 

luharry
Champ on-the-rise
Champ on-the-rise

hi , can you tell me a complete examples of demo ?   thank you .

kaynezhang
World-Class Innovator
World-Class Innovator

If you don't want to use  sites ,you can use Apache Chemistry OpenCMIS libraries,which is more easier to use than webscript api.

openpj
Elite Collaborator
Elite Collaborator

CMIS ok, but if you have a massive import I suggest to use any other methods because CMIS typically is not the best API for solving this task. I would use CMIS only for progressive upload.

Thank you, i will look more into this.  do you know of any good tutorials?

For alfresco cmis implementation document ,please refer to CMIS .

Following is example snippet

private Session getSession(String serverUrl, String username,
String password)

{

SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> params = new HashMap<String, String>();
params.put(SessionParameter.USER, username);
params.put(SessionParameter.PASSWORD, password);
params.put(SessionParameter.ATOMPUB_URL, serverUrl);
params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
params.put(SessionParameter.OBJECT_FACTORY_CLASS,
"org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
List<Repository> repos = sessionFactory.getRepositories(params);
if (repos.isEmpty()) {
throw new RuntimeException("Server has no repositories!");
}

return repos.get(0).createSession();
}

public void createDocument()

{
String serverUrl = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom";
String userName = "admin";
String password = "admin";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");


properties.put(PropertyIds.NAME, "Platform__technical_documentation20140601");
properties.put(PropertyIds.CREATED_BY, "admin");
properties.put("cm:title", "Title x_technical_documentation20140601");
properties.put("cm:description", "Platform_technical_documentation20140601 15M");
Session session = getSession(serverUrl, userName, password);

try {
Folder folder1 = (Folder)session.getObjectByPath("/TESTFOLDER4");
VersioningState vs = VersioningState.MAJOR;

File file = new File("C:\\userguide.pdf");
InputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[(int) file.length()];
dis.readFully(bytes);
ContentStream contentStream = new ContentStreamImpl(file
.getAbsolutePath(), BigInteger.valueOf(bytes.length), "application/pdf",
new ByteArrayInputStream(bytes));

Document newDocument = folder1
.createDocument(properties, contentStream, vs);

System.out.println(newDocument.getId());

} catch (CmisContentAlreadyExistsException ccaee) {
System.out.println("ERROR: Unable to Load - CmisContentAlreadyExistsException: " );
} catch (CmisConstraintException cce) {
System.out.println("ERROR: Unable to Load - CmisConstraintException: " );
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}