使用版本在alfresco中保存文件并下载最新版本

我在露天社区4.0工作,我使用这个jar alfresco-web-service-client-4.0.d.jar

使用此代码我可以在露天保存我的test.pdf文件:

File file = new File("C:/test.pdf"); saveAttachement(file,"test.pdf", "/app:company_home/cm:MyFolder", "admin", "admin") public String saveAttachement(File file, String name, String folderName, String userName, String pwd) throws Exception { byte[] contentByte = IOUtils.toByteArray(new FileInputStream(file)); // Start the session AuthenticationUtils.startSession(userName, pwd); try { // Create a reference to the parent where we want to create content Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore"); ParentReference companyHomeParent = new ParentReference(storeRef, null, folderName, Constants.ASSOC_CONTAINS, null); // Assign name companyHomeParent.setChildName("cm:" + name); // Construct CML statement to create content node // Note: Assign "1" as a local id, so we can refer to it in subsequent // CML statements within the same CML block NamedValue[] contentProps = new NamedValue[1]; contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, name); CMLCreate create = new CMLCreate("1", companyHomeParent, null, null, null, Constants.TYPE_CONTENT, contentProps); // Construct CML statement to add titled aspect NamedValue[] titledProps = new NamedValue[2]; titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, name); titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, name); CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1"); // Construct CML Block CML cml = new CML(); cml.setCreate(new CMLCreate[] { create }); cml.setAddAspect(new CMLAddAspect[] { addAspect }); // Issue CML statement via Repository Web Service and retrieve result // Note: Batching of multiple statements into a single web call UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml); Reference content = result[0].getDestination(); // Write some content ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(); //String text = "The quick brown fox jumps over the lazy dog"; ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8"); Content contentRef = contentService.write(content, Constants.PROP_CONTENT, contentByte, contentFormat); System.out.println("Document are created successfully. UID:= " + content.getUuid()); return content.getUuid(); } catch (Throwable e) { System.out.println(e.toString()); } finally { // End the session AuthenticationUtils.endSession(); //System.exit(0); } return null; } 

在alfresco中保存此文件后我有这个id d7633eff-8595-4d6c-8c18-a29a656607ba

稍后将用于下载此文件

这是下载代码:

 ContentResult contentResult = getContentsById("d7633eff-8595-4d6c-8c18-a29a656607ba", "admin", "admin"); public ContentResult getContentsById(String contentId, String userName, String pwd) throws Exception { ContentResult contentResult = new ContentResult(); // Start the session AuthenticationUtils.startSession(userName, pwd); try { Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore"); ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(); Reference contentReference = new Reference(storeRef, contentId, null); Content[] readResult = null; try { readResult = contentService.read(new Predicate(new Reference[] { contentReference }, storeRef, null), Constants.PROP_CONTENT); } catch (ContentFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } if ((readResult != null) && (readResult[0] != null)) { Content content = readResult[0]; ContentFormat cnf = content.getFormat(); Reference ref = content.getNode(); String[] splitedUrl = content.getUrl().split("/"); String name = splitedUrl[splitedUrl.length - 1]; InputStream is = ContentUtils.getContentAsInputStream(content); byte[] contentByte = ConvertUtil.convertInputStreamToByte(is); contentResult.setName(name); contentResult.setMimetype(cnf.getMimetype()); contentResult.setId(ref.getUuid()); contentResult.setUrl(content.getUrl()); contentResult.setPath(ref.getPath()); contentResult.setContentByte(contentByte); System.out.println(" document has been retrieved"); } } catch (Exception e) { System.out.println(e.toString()); } finally { // End the session AuthenticationUtils.endSession(); // System.exit(0); } return contentResult; } 

目前我更改test.pdf文件内容而不更改文件名,我把它放在光盘的另一个位置。

这是新文件位置。

C:/version/test.pdf

所以在磁盘中我有两个同名但内容不同的文件。

我的目标是在露天保存新文件,但使用版本控制。

在我看来,我必须使用在露天保存的旧id: d7633eff-8595-4d6c-8c18-a29a656607ba

所以我应该开发一个方法,将新文件和旧ID作为参数

喜欢这个

  File file = new File("C:/version/test.pdf"); saveWithVersionAttachement(file,"d7633eff-8595-4d6c-8c18-a29a656607ba","test.pdf", "/app:company_home/cm:MyFolder", "admin", "admin",) public String saveWithVersionAttachement(File file,String oldId, String name, String folderName, String userName, String pwd) throws Exception { ............. ............. 

在我看来,这个方法必须在露天中保存新文件,如test_2.pdf或test_v2.pdf ..

因此这两个文件保存在露天并具有相同的ID

在下载方法中我想拥有最新版本。

所以我必须改变方法getContentById ("d7633eff-8595-4d6c-8c18-a29a656607ba", "admin", "admin");

为了返回最新版本test_2.pdf或test_v2.pdf …

我尝试使用alfresco-OpenCMIS-extension-0.7.jar与OpenCMIS合作失败

我在Alfresco \ tomcat \ webapps \ alfresco \ WEB-INF \ classes \ alfresco \ model \ contentModel.xml中添加了这一行

  cm:versionable  

我也添加了这行version.store.enableAutoVersioning=true

Alfresco \ tomcat \ shared \ classes \ alfresco-global.properties中

是否有人可以帮助我解决我的问题

更新 :

我与CMIS合作:

我尝试使用此代码:

 import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.chemistry.opencmis.client.api.Document; import org.apache.chemistry.opencmis.client.api.Repository; import org.apache.chemistry.opencmis.client.api.Session; import org.apache.chemistry.opencmis.client.api.SessionFactory; import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl; import org.apache.chemistry.opencmis.commons.SessionParameter; import org.apache.chemistry.opencmis.commons.data.ContentStream; import org.apache.chemistry.opencmis.commons.enums.BindingType; import org.apache.commons.io.IOUtils; public void saveVersioning(File file, String filename, String userName, String pwd, String docId) throws Exception { SessionFactory factory = SessionFactoryImpl.newInstance(); Map parameters = new HashMap(); // User credentials. parameters.put(SessionParameter.USER,userName); parameters.put(SessionParameter.PASSWORD, pwd); // Connection settings. parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value()); parameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:9080/alfresco/service/cmis"); // URL to your CMIS server. parameters.put(SessionParameter.AUTH_HTTP_BASIC, "true" ); // Create session. // Alfresco only provides one repository. Repository repository = factory.getRepositories(parameters).get(0); Session session = repository.createSession(); // Get the contents of the file Document doc = (Document) session.getObject(docId); ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content if (contentStream != null) { String mimetype = "text/plain; charset=UTF-8"; String content = ""; if (contentStream != null) { filename = contentStream.getFileName(); mimetype = contentStream.getMimeType(); content = getContentAsString(contentStream); System.out.println("file name "+filename); System.out.println("minetype "+mimetype); System.out.println("content "+content); } byte[] buf = IOUtils.toByteArray(new FileInputStream(file)); ByteArrayInputStream input = new ByteArrayInputStream(buf); contentStream = session.getObjectFactory().createContentStream( filename, buf.length, mimetype, input); System.out.println("Document version history"); { List versions = doc.getAllVersions(); for (Document version : versions) { System.out.println("\tname: " + version.getName()); System.out.println("\tversion label: " + version.getVersionLabel()); System.out.println("\tversion series id: " + version.getVersionSeriesId()); System.out.println("\tchecked out by: " + version.getVersionSeriesCheckedOutBy()); System.out.println("\tchecked out id: " + version.getVersionSeriesCheckedOutId()); System.out.println("\tmajor version: " + version.isMajorVersion()); System.out.println("\tlatest version: " + version.isLatestVersion()); System.out.println("\tlatest major version: " + version.isLatestMajorVersion()); System.out.println("\tcheckin comment: " + version.getCheckinComment()); System.out.println("\tcontent length: " + version.getContentStreamLength() + "\n"); } } } } private static String getContentAsString(ContentStream stream) throws IOException { StringBuilder sb = new StringBuilder(); Reader reader = new InputStreamReader(stream.getStream(), "UTF-8"); try { final char[] buffer = new char[4 * 1024]; int b; while (true) { b = reader.read(buffer, 0, buffer.length); if (b > 0) { sb.append(buffer, 0, b); } else if (b == -1) { break; } } } finally { reader.close(); } return sb.toString(); } 

我用这段代码称这个函数:

 File file = new File("C:/version/test.pdf"); saveVersioning(file, "test.pdf","admin","admin","d7633eff-8595-4d6c-8c18-a29a656607ba"); 

我用过这个jar子:

 chemistry-opencmis-client-api-0.13.0.jar chemistry-opencmis-client-bindings-0.13.0.jar chemistry-opencmis-client-impl-0.13.0.jar chemistry-opencmis-commons-api-0.13.0.jar chemistry-opencmis-commons-impl-0.13.0.jar commons-io-1.4.jar slf4j-api-1.7.5.jar stax2-api-3.1.4.jar woodstox-core-asl-4.4.0.jar 

但是当我尝试测试时我有这个错误:

 Caused by: java.lang.NoClassDefFoundError: org/apache/chemistry/opencmis/client/api/SessionFactory ............. ............ Caused by: java.lang.ClassNotFoundException: org.apache.chemistry.opencmis.client.api.SessionFactory at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) ... 124 more 

实现这一目标的快速, 最简单的方法是利用RESTfull API

这也将确保与新版露天的兼容性。

请注意,您需要在表单属性updatenoderef中为要更新的文档提供noderef ,并且属性majorversion是一个布尔标志,用于指定新版本是否为文档的次要/主要版本。

下面是一个示例代码,可以帮助您使用您的用例:

  CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost uploadFile = new HttpPost(+"/api/upload?alf_ticket="+); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("username", "", ContentType.TEXT_PLAIN); builder.addTextBody("updatenoderef", , ContentType.TEXT_PLAIN); builder.addTextBody("...", "...", ContentType.TEXT_PLAIN); builder.addBinaryBody("filedata", , ContentType.DEFAULT_BINARY, ); HttpEntity multipart = builder.build(); uploadFile.setEntity(multipart); CloseableHttpResponse response = httpClient.execute(uploadFile); String responseString = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); JSONObject responseJson = new JSONObject(responseString); if (response.getStatusLine().getStatusCode()!=200){ throw new Exception("Couldn't upload file to the repository, webscript response :" + responseString ); } 

注1:您需要将这些tockens <*>替换为您自己的值/ vars

注意2:如果您在检索故障单时遇到问题,请检查此链接或此 链接

如果您已经启用了版本控制,则只需使用新二进制文件和新名称更新文档。 Alfresco将负责版本控制。 get方法将检索最后一个版本。

此链接描述了如何使用CMIS更新文档

我认为你不需要修改get方法。