用apache chemistry php client下载文件

时间:2016-02-15 00:39:03

标签: alfresco cmis apache-chemistry

如果我想下载一个cmis:documento,我需要知道自己要做什么,我在我的存储库中使用Apache Chemistry Php客户端。

我在Sitios空间有一个文件“Test.txt”... 我想用浏览器下载Test.txt ...

我的代码:

$client = new CMISService('http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom', 'admin', 'admin');
$path = '/Sitios';
$directorio = $client->getObjectByPath($path);

$objs = $client->getChildren($directorio->id);
foreach ($objs->objectList as $obj)
{
    var_dump ($obj);
}

在$ obj中我已经:

 array (size=32)
      'alfcmis:nodeRef' => string 'workspace://SpacesStore/c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7' (length=60)
      'cmis:isImmutable' => string 'false' (length=5)
      'cmis:versionLabel' => string '1.0' (length=3)
      'cmis:objectTypeId' => string 'cmis:document' (length=13)
      'cmis:description' => string 'descripcion del fichero' (length=23)
      'cmis:createdBy' => string 'admin' (length=5)
      'cmis:checkinComment' => null
      'cmis:creationDate' => string '2016-02-15T01:25:12.76+01:00' (length=28)
      'cmis:isMajorVersion' => string 'true' (length=4)
      'cmis:contentStreamFileName' => string 'Fichero de texto plano' (length=22)
      'cmis:name' => string 'Fichero de texto plano' (length=22)
      'cmis:isLatestVersion' => string 'true' (length=4)
      'cmis:lastModificationDate' => string '2016-02-15T01:25:12.76+01:00' (length=28)
      'cmis:contentStreamLength' => string '21' (length=2)
      'cmis:objectId' => string 'c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7;1.0' (length=40)
      'cmis:lastModifiedBy' => string 'admin' (length=5)
      'cmis:secondaryObjectTypeIds' => string 'P:rn:renditioned' (length=16)
      'cmis:contentStreamId' => string 'store://2016/2/15/1/25/c36a749d-43f1-4e31-b46a-de66b4f5634d.bin' (length=63)
      'cmis:contentStreamMimeType' => string 'text/plain' (length=10)
      'cmis:baseTypeId' => string 'cmis:document' (length=13)
      'cmis:changeToken' => null
      'cmis:isPrivateWorkingCopy' => string 'false' (length=5)
      'cmis:versionSeriesCheckedOutBy' => null
      'cmis:isVersionSeriesCheckedOut' => string 'false' (length=5)
      'cmis:versionSeriesId' => string 'c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7' (length=36)
      'cmis:isLatestMajorVersion' => string 'true' (length=4)
      'cmis:versionSeriesCheckedOutId' => null
      'cm:title' => string 'Titulo del fichero' (length=18)
      'cm:description' => string 'descripcion del fichero' (length=23)
      'app:editInline' => string 'true' (length=4)
      'cm:lastThumbnailModification' => string 'pdf:1455495914744' (length=17)
      '' => string 'Titulo del fichero' (length=18)
  public 'renditions' => 

我要创建一个下载文件的链接? 我不想使用webscript ...

我认为我要使用getContentStream($ obj-> id)¿ 感谢。

1 个答案:

答案 0 :(得分:2)

根据您的文档mime类型以及您愿意使用的内容,它可能略有不同,但如果您使用的是java,则基本上需要this

  // Get the contents of the file
    Document doc = (Document) session.getObject(id);
    ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content
    if (contentStream != null) {
        String content = getContentAsString(contentStream);
        System.out.println("Contents of " + filename + " are: " + content);
    } else {
        System.out.println("No content.");
    }

    ...

    /**
     * Helper method to get the contents of a stream
     * 
     * @param stream
     * @return
     * @throws IOException
     */
    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();
    }

但由于您使用的是PHP,我猜您应该使用php api fuctions复制相同的逻辑,因此基本上$client->getContentStream($objId);应该将文件内容作为字符串返回。