2011-01-13 2 views
12

다음 코드를 사용하여 "rose.gif"를 roseNode에 삽입합니다. 하지만 저장소에서 파일을 검색하려면 어떻게해야합니까? 노드와의 파일의 내용 "JCR : 데이터를"속성을에 :JCR 파일 노드에서 파일 가져 오기

Node roseNode = session.getRootNode().getNode("wiki:encyclopedia/wiki:entry[1]/"); 

    File file = new File("rose.gif"); 
    MimeTable mt = MimeTable.getDefaultTable(); 
    String mimeType = mt.getContentTypeFor(file.getName()); 
    if (mimeType == null) mimeType = "application/octet-stream"; 

    Node fileNode = roseNode.addNode(file.getName(), "nt:file"); 

    System.out.println(fileNode.getName()); 

    Node resNode = fileNode.addNode("jcr:content", "nt:resource"); 
    resNode.setProperty("jcr:mimeType", mimeType); 
    resNode.setProperty("jcr:encoding", ""); 
    resNode.setProperty("jcr:data", new FileInputStream(file)); 
    Calendar lastModified = Calendar.getInstance(); 
    lastModified.setTimeInMillis(file.lastModified()); 
    resNode.setProperty("jcr:lastModified", lastModified); 

    //retrieve file and output as rose-out.gif 
    File outputFile = new File("rose-out.gif"); 
    FileOutputStream out = new FileOutputStream(outputFile); 

답변

16

당신이 정말로해야 할 유일한 것은 "파일 NT '의 이름에서 파일의 이름을 얻을 수있다 "jcr : content"자식 노드.

JCR 1.0 및 2.0은 이진 "jcr : data"속성 값에 대한 스트림을 얻는 방법에 약간 다릅니다. 당신은 JCR 1.0을 사용하는 경우, 다음 코드는 다음과 같습니다

Node fileNode = // find this somehow 
Node jcrContent = fileNode.getNode("jcr:content"); 
String fileName = fileNode.getName(); 
InputStream content = jcrContent.getProperty("jcr:data").getStream(); 

당신은 JCR 2.0을 사용하는 경우는, 마지막 줄은 조금 다르다 먼저 속성에서 이진 개체를 얻을 수 있기 때문에 값 :

InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream(); 

그러면 표준 Java 스트림 유틸리티를 사용하여 'content'스트림의 바이트를 파일에 쓸 수 있습니다.

바이너리 개체가 완료되면 바이너리의 dispose() 메서드를 호출하여 바이너리로 완료되었다는 신호를 알리고 구현이 이진 개체에서 얻은 모든 리소스를 해제 할 수 있는지 확인해야합니다. 일부 JCR 구현은 닫을 때 자동으로 dispose()을 호출하는 스트림을 반환하여 프로그래밍 오류를 잡으려고 시도하지만 항상이 작업을 수행해야합니다.

+0

Hi Randall, 내가 원하는 것은 OutputStream이고 입력 스트림이 아닙니다. 이것이 가능한가? – Steve

+0

나는 그것을 이해했다. 그냥 inputStream을 바이트 단위로 outputStream 바이트로 변환했다. – Steve

+0

2.0의 경우 Binary 객체 (http://www.day.com/specs/jcr/2.0/5_Reading.html)를 사용하면 Binary.dispose()를 호출해야한다고 생각합니다. –