2017-03-09 39 views
0

您好我正在致力於Adobe體驗經理(AEM)CQ5。現在我的任務是我必須從存儲庫中計算未使用的大壩圖像的大小。在迭代它的時候,我得到了每個文件的字節大小,但是我無法統計一次循環中所有圖像的字節數。這裏是代碼示例如何計算CQ5中while循環中所有圖像的總字節數

while (iterator.hasNext()) { 
    Node node = session.getNode(iterator.nextNode().getPaath()); 
    Resource res = resourceResolver = resourceResolver.getResource(node.getPath()); 
    byte[] result = null; 
    try { 
     Node ntFileNode = getSession().getNode("Path"); 
     Node ntResourceNode = ntFileNode.getNode("jcr:content"); 
     InputStream is = ntResourceNode.getProperty("jcr:data").getBinary().getStream(); 
     BufferedInputStream bin = new BufferedInputStream(is); 
     result = IOUtils.toByteArray(bin); 
     bin.close(); 
     is.close(); 
     System.out.print("Bytes of Images " + result); // here I am getting bytes of images but in individual. I want to get the total count of bytes of all images. 
    } // while loop ends 

任何人都可以建議我在哪裏必須在上面的代碼進行更改?

回答

1

嘗試以下.. 您可以重構代碼如下..但我只是集中於總字節數:

int totalBytes = 0; 
while(iterator.hasNext()){ 
    Node node = session.getNode(iterator.nextNode().getPaath()); 
    Resource res = resourceResolver = resourceResolver.getResource(node.getPath()); 
    byte[] result = null; 
    try { 
     Node ntFileNode = getSession().getNode("Path"); 
     Node ntResourceNode = ntFileNode.getNode("jcr:content"); 
     InputStream is = ntResourceNode.getProperty("jcr:data").getBinary().getStream(); 
     BufferedInputStream bin = new BufferedInputStream(is); 
     result = IOUtils.toByteArray(bin); 

     totalBytes = totalBytes + result; 

     bin.close(); 
     is.close(); 
     System.out.print("Bytes of Image "+ result); // here I am getting bytes of images but in individual. I want to get the totoal count of bytes of all images. 

     result=null; //optional - just to be sure. 
    } // while loop ends 

    System.out.print("Total bytes of all images= "+ totalBytes);