2016-11-09 176 views
0

我有以下代碼嘗試讀取DICOM圖像:WebappClassLoader無法加載資源

public BufferedImage readDicomFile(File dicomFile) { 

    ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName("DICOM").next(); 
    BufferedImage tempImage = null; 
    try { 
     if (!dicomFile.exists()) {   
      return null; 
     } 
     ImageIO.scanForPlugins(); 
     tempImage = ImageIO.read(dicomFile);    
     DicomObject obj = new DicomInputStream(dicomFile).readDicomObject(); 
     String overlayData = obj.getString(1610625024); 
     if ((overlayData != null) && (overlayData.length() > 0)) { 
      tempImage = combineImages(tempImage, 
        OverlayUtils.extractOverlay(obj, 1610625024, reader, 
          "FFFFFF")); 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return tempImage; 
} 

然而,這就是我得到:

SEVERE: java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.dcm4che.data.DcmParserFactory], because it has not yet been started, or was already stopped 
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1401) 
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359) 
at org.dcm4cheri.imageio.plugins.DcmImageReaderSpi.canDecodeInput(DcmImageReaderSpi.java:113) 
at javax.imageio.ImageIO$CanDecodeInputFilter.filter(ImageIO.java:567) 
at javax.imageio.spi.FilterIterator.advance(ServiceRegistry.java:810) 
at javax.imageio.spi.FilterIterator.<init>(ServiceRegistry.java:804) 
at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:508) 
at javax.imageio.ImageIO.getImageReaders(ImageIO.java:646) 
at javax.imageio.ImageIO.read(ImageIO.java:1438) 
at javax.imageio.ImageIO.read(ImageIO.java:1308) 
at edu.kfshrc.dm.services.DicomViewerService.readDicomFile(DicomViewerService.java:565) 
at edu.kfshrc.dm.services.DicomViewerService.extractImageFromDicom(DicomViewerService.java:414) 
at edu.kfshrc.dm.servlet.DicomViewerServlet.doGet(DicomViewerServlet.java:131) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:722) 

SEVERE:  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1401) 
SEVERE:  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359) 
SEVERE:  at org.dcm4cheri.imageio.plugins.DcmImageReaderSpi.canDecodeInput(DcmImageReaderSpi.java:113) 
SEVERE:  at javax.imageio.ImageIO$CanDecodeInputFilter.filter(ImageIO.java:567) 
SEVERE:  at javax.imageio.spi.FilterIterator.advance(ServiceRegistry.java:810) 
SEVERE:  at javax.imageio.spi.FilterIterator.<init>(ServiceRegistry.java:804) 
SEVERE:  at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:508) 
SEVERE:  at javax.imageio.ImageIO.getImageReaders(ImageIO.java:646) 
SEVERE:  at javax.imageio.ImageIO.read(ImageIO.java:1438) 
SEVERE:  at javax.imageio.ImageIO.read(ImageIO.java:1308) 
SEVERE:  at edu.kfshrc.dm.services.DicomViewerService.readDicomFile(DicomViewerService.java:565) 
SEVERE:  at edu.kfshrc.dm.services.DicomViewerService.extractImageFromDicom(DicomViewerService.java:414) 
SEVERE:  at edu.kfshrc.dm.servlet.DicomViewerServlet.doGet(DicomViewerServlet.java:131) 
SEVERE:  at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) 
SEVERE:  at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
SEVERE:  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 
SEVERE:  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
SEVERE:  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
SEVERE:  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
SEVERE:  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
SEVERE:  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
SEVERE:  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
SEVERE:  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
SEVERE:  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
SEVERE:  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 
SEVERE:  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 
SEVERE:  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
SEVERE:  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
SEVERE:  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
SEVERE:  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
SEVERE:  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
SEVERE:  at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
SEVERE:  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
SEVERE:  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
SEVERE:  at java.lang.Thread.run(Thread.java:722) 

我傳遞一個文件指向特定路徑的對象:「c:\ temp2 \ testing.dcm」。當它試圖讀取Dicom文件時,會出現上述錯誤。

你能幫忙嗎?

+1

我用「ImageIO.scanForPlugins()」和它的工作。但是,它始終無法正常工作。這是零星的。此外,使用「ImageIO.read(dicomFile)」讀取圖像也是零星的。有時,閱讀成功,有時它會出現「NullPointerException」或「無法獲取格式」。有時,它不會給出錯誤,但會錯誤地讀取,導致無法寫入的圖像。 –

回答

0

薩拉姆瓦埃勒,

的根本原因的問題是Servlet容器(Tomcat的,..)不能處理登記/去登記本機庫自動

這意味着當dcm4che負載的ImageIO DICOM插件,它如果你想從另一個webapp(Servlet)使用它,你必須再次掃描該插件以執行dcm4che DICOM讀寫器代碼。

我建議自行註銷原生的ImageIO插件每個excution之後,以防止潛在的內存泄漏在未來

 try{ 
ImageIO.scanForPlugins() 
    //Dicom code 
     } 
    catch { 
    //exceptions 
    } 
    finally { 
       try { 
        // De-register any locally registered IIO plugins. Relies on 
        // each web app having its own context class loader. 
        final IIORegistry registry = IIORegistry.getDefaultInstance(); 
        final LocalFilter localFilter = new LocalFilter(Thread.currentThread().getContextClassLoader()); 

        // scanForPlugins uses context class loader 
        Iterator<Class<?>> categories = registry.getCategories(); 

        while (categories.hasNext()) { 
         Class<?> category = categories.next(); 
         Iterator<?> providers = registry.getServiceProviders(category, localFilter, false); 

         // Copy the providers, as de-registering while iterating 
         // over providers will lead to 
         // ConcurrentModificationExceptions. 
         List<Object> providersCopy = new ArrayList<Object>(); 
         while (providers.hasNext()) { 
          providersCopy.add(providers.next()); 
         } 

         for (Object provider : providersCopy) { 
          registry.deregisterServiceProvider(provider); 
          log.debug(String.format("Unregistered locally installed provider class: %s",provider.getClass())); 
         } 
        } 
       } catch (Exception ex) { 
        _logger.error("Exception occured while de-registering locally registered IIO plugins", ex); 
       } 
+0

對不起,延遲迴復。非常感謝。它實際上工作。在那之後,我正面臨一件小事。當我對代碼進行一些更改並編譯時,它在轉換爲JPEG圖像時會出現錯誤。這是錯誤:SEVERE:java.lang.IllegalStateException:無法獲得格式 當我重新啓動我的應用程序服務器,這是glassfish,然後它工作正常,此錯誤消失。你是否知道爲什麼會發生這種情況,以及如何消除重新啓動服務器的需要? –

+0

Salam Wael 這個雲發生的原因有很多,它很難知道沒有看到代碼,也請記住,JPEG插件的JPEG閱讀器和作家使用nativeLib作爲壓縮算法的實現 我推薦到 -have ImageIo jar文件+在全球玻璃魚庫目錄本地的dll不在Web應用程序的WEB-INF \ lib中 -cleanup使用插件每個請求後 型掃描的插件讓讀者和服務員每次 這可以幫助你在一般前,不代碼調查 –

+0

對於遲到的回覆Ahmed不好意思。當我將所有位於WEB-INF \ lib文件夾下的jar文件移動到C:\ opt \ glassfish3 \ glassfish \ lib文件夾時,它工作正常。我試圖做很多次更改,並且無需重新啓動服務器即可正常工作。 –