2013-05-20 102 views
0

我想創建一個文件夾在Alfresco下面使用XMSI是我使用的代碼。文件創建insted文件夾在Alfresco

public static void main(String [] args)throws Exception {0} {0} {0}長開始= System.currentTimeMillis();

String host = "127.0.0.1"; 
    int port = 9080; 
    String username = "admin"; 
    String password = "admin"; 
    String parentFolder = "Company%20Home"; 
    String folderName = "sales3"; 
    String description = "sales space3"; 

    RulesRequest request = new RulesRequest(); 
    //request.setRemediationProfileObj(remediationProfile); 
    Gson gs = new Gson(); 
    String json = gs.toJson(request); 
    DefaultHttpClient client = new DefaultHttpClient(); 
    RestClient rstClnt = new RestClient(); 
    String ticket = rstClnt.restClientPost(json, "http://127.0.0.10:9080/alfresco/service/api/login?u=admin&pw=admin", client); 
    //String url = "http://" + host + ":" + port + "/alfresco/service/api/path/workspace/SpacesStore/" + parentFolder + "/children"; 
    String url = "http://localhost:9080/alfresco/service/cmis/s/workspace:SpacesStore/i/078b05c6-14bd-439c-a1ae-db032c5d98fc/children?alf_ticket="+ticket; 

    String contentType = "application/atom+xml;type=entry"; 

    String xml = 
     "<?xml version='1.0' encoding='utf-8'?>\n" + 
     "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://www.cmis.org/2008/05'>\n" + 
     "<title>" + folderName + "</title>\n" + 
     "<summary>" + description + "</summary>\n" + 
     "<cmis:object>\n"+ 
     "<cmis:properties>\n" + 
     "<cmis:propertyString cmis:name='cmis:objectTypeId'>\n" + 
     "<cmis:value>cmis:folder</cmis:value>\n" + 
     "</cmis:propertyString>\n" + 
     "</cmis:properties>\n" + 
     "</cmis:object>\n" + 
     "</entry>\n"; 




    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    httpclient.getCredentialsProvider().setCredentials(
      new AuthScope(host, port), 
      new UsernamePasswordCredentials(username, password)); 

    HttpPost httppost = new HttpPost(url); 
    httppost.setHeader("Content-type", contentType); 

    StringEntity requestEntity = new StringEntity(xml, "UTF-8"); 
    httppost.setEntity(requestEntity); 


    System.out.println("executing request" + httppost.getRequestLine()); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 

    System.out.println("----------------------------------------"); 
    System.out.println(response.getStatusLine()); 
    if (entity != null) { 
     System.out.println("Response content type: " + entity.getContentType()); 
     long contentLength = entity.getContentLength(); 
     System.out.println("Response content length: " 
       + entity.getContentLength()); 

     if (contentLength > 0) { 
      byte [] b = new byte[(int) contentLength]; 
      entity.getContent().read(b); 
      System.out.println("Response content: " + new String(b)); 
     } 

     entity.writeTo(System.out); 
    } 

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure 
    // immediate deallocation of all system resources 
    httpclient.getConnectionManager().shutdown(); 
    long end = System.currentTimeMillis(); 
    System.out.println("Time spend: " + (end-start) + "ms"); 
} 

而不是創建一個文件夾,它是創造大小的文件0

感謝 Garvit

+0

爲什麼人們試圖手動進行CMIS操作? [Apache Chemistry](http://chemistry.apache.org/java/opencmis.html)提供了一個可愛的Java API,它將爲您完成所有工作! – Gagravarr

回答

2

你的財產是錯誤的類型。您正在使用的cmis:propertyString代替cmis:propertyId錯誤請嘗試以下

<cmis:propertyId propertyDefinitionId="cmis:objectTypeId"> 
    <cmis:value>cmis:folder</cmis:value> 
</cmis:propertyId> 

由於@Gagravarr說,像這樣的問題是很容易避免的,如果你可以使用衆所周知的客戶端庫。如果您自己構建HTTP請求,最好有一個很好的理由。

+0

感謝Will,它工作。你能告訴我如何使用cmis請求在戶外創建自定義元數據。 –

+0

嗨請問,我只是在Alfresco中添加了一個方面來添加我的自定義元數據,我只是想知道如何將這個方面添加到所有新創建的文件中。 –

+0

我建議你做一個網絡搜索。如果你找不到答案,你應該在stackoverflow上提出一個新問題。您的後續問題不太可能會在此評論主題中看到。 –

相關問題