2011-10-13 51 views
0

我的java支持的webscript將存儲庫中的文件複製到臨時文件夾並根據我的需要進行編輯。在工作過程中,會生成一個新內容並將其寫入創建的臨時文件。如何更新Alfresco中的文件內容?

但存在一個問題:下面的第一個或第二個代碼不會更新文件的內容。

ContentWriter contentWriter = this.contentService.getWriter(tempFile, 
           ContentModel.PROP_CONTENT, true); 
contentWriter.putContent(content); 

而第二個:

` 
WritableByteChannel byteChannel = contentWriter.getWritableChannel(); 
ByteBuffer buffer = ByteBuffer.wrap(content.getBytes()); 
byteChannel.write(buffer); 
byteChannel.close(); 
` 

如何更新文件的內容?

回答

4

這個工作對我來說:

ContentWriter contentWriter = contentService.getWriter(noderef, ContentModel.PROP_CONTENT, true); 
     contentWriter.setMimetype("text/csv"); 
     FileChannel fileChannel = contentWriter.getFileChannel(false); 
     ByteBuffer bf = ByteBuffer.wrap(logLine.getBytes()); 
     try { 
      fileChannel.position(contentWriter.getSize()); 
      fileChannel.write(bf); 
      fileChannel.force(false); 
      fileChannel.close(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 

我追加一行到一個現有的文件,這樣的logline是附加的字符串。

+0

我想替換臨時文件上的全部內容。你的代碼不適用於它。 – Alexey

+0

嗨如果你想取代所有的東西,只是不要使用該行:fileChannel.position(contentWriter.getSize()); 然後默認使用0作爲起始位置。 –

+0

它不起作用。我試圖評論該行並設置爲零:fileChannel.position(0)。 Alfresco Web客戶端(/ alfresco)顯示該文件已被修改(日期更改),但其內容不是。 – Alexey