【问题标题】:Upload File with OpenCMIS has no content使用 OpenCMIS 上传文件没有内容
【发布时间】:2013-04-18 15:42:32
【问题描述】:

我正在尝试使用 Apache Chemistry OpenCMIS 将文件上传到我的 Alfresco 存储库。 文件已创建且属性正确但没有内容,文件为 0 字节。我已经仔细检查过,源文件没有任何问题。 这是我的 Java 代码:

File content = new File(somepath);
try{
                    String mimeType = new  MimetypesFileTypeMap().getContentType(content);
                    logger.debug("mimetype: " + mimeType);
                    logger.debug("file: " + content.getAbsolutePath());
                    Map<String, Object> properties = new HashMap<String, Object>();
                    properties.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value());
                    properties.put(PropertyIds.NAME, content.getName());


                    FileInputStream fis = new FileInputStream(content);
                    DataInputStream dis = new DataInputStream(fis);
                    byte[] bytes = new byte[(int) content.length()];
                    dis.readFully(bytes);

                    ContentStream cs = new ContentStreamImpl(content.getName(), BigInteger.valueOf(bytes.length), mimeType, dis);
                    Folder folder = (Folder) session.getObjectByPath("/myfolder");
                    Document doc = folder.createDocument(properties, cs, VersioningState.MAJOR);
                    return doc.getId();
                }catch(CmisBaseException e){
                    logger.error("error uploading file: "+ e.getMessage(), e);
                }

没有捕获到异常。

【问题讨论】:

    标签: java file-upload alfresco opencmis


    【解决方案1】:

    我认为您传递的内容流有问题。

    尝试用这个替换你的代码

    String docText = "This is a sample document";
    byte[] content = docText.getBytes();
    InputStream stream = new ByteArrayInputStream(content);
    ContentStream contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);
    

    您可以在下一步中使用新文件中的内容逐步更改此简单文本。

    【讨论】:

      【解决方案2】:

      我正在寻找有关此的示例,并找到了您的问题, 如果我没记错的话,原来的问题是你是 预先读取缓冲区,因此指针在它的末尾时 你把它传递给内容流,我正在做一个小课来测试 我想稍后在程序中实现的一些功能和这个块 适用于您的初始方法。

      File content = new File("C:\\\\asdf.asdf");
          try{
              String mimeType = new  MimetypesFileTypeMap().getContentType(content);
              System.out.println("mimetype: " + mimeType);
              System.out.println("file: " + content.getAbsolutePath());
              Map<String, Object> properties = new HashMap<String, Object>();
              properties.put(PropertyIds.OBJECT_TYPE_ID,BaseTypeId.CMIS_DOCUMENT.value()+",P:cm:titled");
              properties.put("cm:description", "upload desde código");
              properties.put(PropertyIds.NAME, content.getName());
              FileInputStream fis = new FileInputStream(content); 
              DataInputStream dis = new DataInputStream(fis);                    
              ContentStream cs = new ContentStreamImpl(content.getName(),BigInteger.valueOf(content.length()), mimeType, dis);
              Document doc = newFolder.createDocument(properties, cs, VersioningState.MAJOR);
          }catch(CmisBaseException ex){
              Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
          } catch (FileNotFoundException ex) {
              Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
          }
      

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 1970-01-01
        • 2019-11-25
        • 2020-11-25
        • 1970-01-01
        • 2015-07-21
        • 1970-01-01
        • 2023-03-04
        • 2013-12-04
        相关资源
        最近更新 更多