2013-04-18 70 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); 
    }