2013-04-18 81 views
2

我想用Apache Chemistry OpenCMIS将文件上传到我的Alfresco仓库。 文件已创建且属性正确,但没有内容,文件为0字节。我仔细检查过,源文件没有错。 这里是我的Java代码:用OpenCMIS上传文件没有内容

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); 
       } 

没有任何异常捕捉。

回答

4

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

尝试与此

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); 

代替你的代码可以逐渐与内容这一简单的文本从你的新文件中的下一步变化。

+0

谢谢,这对我有用! – dumazy

1

我一直在寻找有关这方面的例子,发现你的问题, 与原来的问题,如果我没有记错的话是,你是 预先读取缓冲区使指针是在它结束时 你将它传递给内容流,我正在制作一个小课程来测试 我想在稍后的程序中实现的一些功能,此块 适用于您的初始方法。

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); 
    }