09-22-2016 04:58 PM
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.
09-23-2016 10:36 PM
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();
}
}
09-23-2016 02:33 AM
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.
11-03-2016 04:04 AM
hi , can you tell me a complete examples of demo ? thank you .
09-23-2016 02:44 AM
If you don't want to use sites ,you can use Apache Chemistry OpenCMIS libraries,which is more easier to use than webscript api.
09-23-2016 02:52 AM
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.
09-23-2016 11:56 AM
Thank you, i will look more into this. do you know of any good tutorials?
09-23-2016 10:36 PM
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();
}
}
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.