Java Servlets和ImageIO错误

时间:2013-03-15 12:27:55

标签: servlets thread-safety javax.imageio dicom dcm4che

我正在尝试编写一个查看某些DICOM文件的WebApplication。我的想法是通过调用我的servlet将DICOM文件动态转换为JPEG。 我使用dcm4che 2.0.27来转换文件。

servlet被称为

<img src="pathToServlet/PathToDICOMFile">.

问题:当我有很多标签(当然是寻址不同的DICOM文件)时,有时会得到java.util.ConcurrentModificationException类型的异常

这是方法,我的servlet正在调用:

void convertFile(String path, OutputStream to) throws IOException {
    File myDicomFile = new File(path);

    BufferedImage myJpegImage = null;
    ImageIO.scanForPlugins();
    Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");

    ImageReader reader = (ImageReader) iter.next();

    DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();

    ImageInputStream iis = ImageIO.createImageInputStream(myDicomFile);
    reader.setInput(iis, false);
    myJpegImage = reader.read(0, param);
    iis.close();
    ImageIO.write(myJpegImage, "JPEG", to);

    to.close();

}

“path”是dicom文件的绝对路径,“out”只是response.getOutputStream()。

可以在3个地方抛出异常:

  1. ImageIO.getImageReadersByFormatName( “DICOM”);
  2. myJpegImage = reader.read(0,param);
  3. ImageIO.write(myJpegImage,“JPEG”,to);
  4. 这是一个堆栈跟踪,它在调用ImageIO.getImageReadersByFormatName(“DICOM”)时被抛出:

    java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806)
    at java.util.HashMap$ValueIterator.next(HashMap.java:835)
    at javax.imageio.spi.PartialOrderIterator.<init>(PartiallyOrderedSet.java:177)
    at javax.imageio.spi.PartiallyOrderedSet.iterator(PartiallyOrderedSet.java:85)
    at javax.imageio.spi.SubRegistry.getServiceProviders(ServiceRegistry.java:759)
    at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:451)
    at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:507)
    at javax.imageio.ImageIO.getImageReadersByFormatName(ImageIO.java:708)
    at example.project.dicomtest.myDicomConverter.ConvertHelper.convertFile(ConvertHelper.java:32)
    at example.project.dicomtest.myDicomConverter.GetImage.doGet(GetImage.java:40)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEnginateValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    

    是因为ImageIO不是线程安全的吗?我该怎么做才能解决这个问题? 非常感谢您的帮助和最好的问候!

2 个答案:

答案 0 :(得分:6)

这两行:

ImageIO.scanForPlugins();
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");

只需在应用程序加载或初始化servlet时执行一次。特别是,第一行ImageIO.scanForPlugins()确实在内部改变了ImageIO类使用的共享数据。这可能是您的并发修改异常的原因。考虑将这两行移动到servlet过滤器或servlet的init(ServletConfig)方法。

答案 1 :(得分:1)

使用synchronized方法来防止并发修改可能会有所帮助。阅读这个问题

stackoverflow.com/questions/9884148/use-of-synchronized-method-in-affable-bean-shopping-cart