写入图片抛出java.io.IOException: Broken pipe异常

2024-07-29 18:33:27 浏览数 (4)

场景:

代码语言:javascript复制
  最近业务需要,无意中看到开源web文件管理器插件 elfinder,功能还比较不错,想改装一下用在自己的项目里
  在本地测试ok但是部署到linux上时,浏览在线大型图片的时候报如下异常;
  》

java.io.IOException: Broken pipe

at sun.nio.ch.FileDispatcherImpl.writev0(Native Method)

at sun.nio.ch.SocketDispatcher.writev(SocketDispatcher.java:51)

at sun.nio.ch.IOUtil.write(IOUtil.java:148)

at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:504)

at org.xnio.nio.NioSocketConduit.write(NioSocketConduit.java:184)

at io.undertow.server.protocol.http.HttpResponseConduit.write(HttpResponseConduit.java:646)

at io.undertow.conduits.AbstractFixedLengthStreamSinkConduit.write(AbstractFixedLengthStreamSinkConduit.java:148)

at org.xnio.conduits.ConduitStreamSinkChannel.write(ConduitStreamSinkChannel.java:158)

at io.undertow.channels.DetachableStreamSinkChannel.write(DetachableStreamSinkChannel.java:179)

at io.undertow.server.HttpServerExchangeW r i t e D i s p a t c h C h a n n e l . w r i t e ( H t t p S e r v r E x c h a n g e . j a v a : 2059 ) a t o r g . x n i o . c h a n n e l s . C h a n n e l s . w r i t e B l o c k i n g ( C h a n n e l s . j a v a : 152 ) a t i o . u n d e r t o w . s e r v l e t . s p e c . S e r v l e t O u t p u t S t r e a m I m p l . w r i t e T o o L a r g e F o r B u f f e r ( S e r v l e t O u t p u t S t r e a m I m p l . j a v a : 197 ) a t i o . u n d e r t o w . s e r v l e t . s p e c . S e r v l e t O u t p u t S t r e a m I m p l . w r i t e ( S e r v l e t O u t p u t S t r e a m I m p l . j a v a : 145 ) a t o r g . a p a c h e . c o m m o n s . i o . I O U t i l s . c o p y L a r g e ( I O U t i l s . j a v a : 2315 ) a t o r g . a p a c h e . c o m m o n s . i o . I O U t i l s . c o p y ( I O U t i l s . j a v a : 2270 ) a t o r g . a p a c h e . c o m m o n s . i o . I O U t i l s . c o p y L a r g e ( I O U t i l s . j a v a : 2291 ) a t o r g . a p a c h e . c o m m o n s . i o . I O U t i l s . c o p y ( I O U t i l s . j a v a : 2246 ) a t c o m . i t s t y l e . c l o u d . c o m m o n . e l f i n d e r . c o m m a n d . F i l e C o m m a n d . e x e c u t e ( F i l e C o m m a n d . j a v a : 69 ) a t c o m . i t s t y l e . c l o u d . c o m m o n . e l f i n d e r . c o m m a n d . A b s t r a c t C o m m a n d . e x e c u t e ( A b s t r a c t C o m m a n d . j a v a : 109 ) a t c o m . i t s t y l e . c l o u d . w e b . C l o u d D i s k C o n t r o l l e r . c o n n e c t o r ( C l o u d D i s k C o n t r o l l e r . j a v a : 67 ) a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e 0 ( N a t i v e M e t h o d ) a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e ( N a t i v e M e t h o d A c c e s s o r I m p l . j a v a : 62 ) a t s u n . r e f l e c t . D e l e g a t i n g M e t h o d A c c e s s o r I m p l . i n v o k e ( D e l e g a t i n g M e t h o d A c c e s s o r I m p l . j a v a : 43 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( M e t h o d . j a v a : 498 ) a t o r g . s p r i n g f r a m e w o r k . w e b . m e t h o d . s u p p o r t . I n v o c a b l e H a n d l e r M e t h o d . d o I n v o k e ( I n v o c a b l e H a n d l e r M e t h o d . j a v a : 205 ) a t o r g . s p r i n g f r a m e w o r k . w e b . m e t h o d . s u p p o r t . I n v o c a b l e H a n d l e r M e t h o d . i n v o k e F o r R e q u e s t ( I n v o c a b l e H a n d l e r M e t h o d . j a v a : 133 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . S e r v l e t I n v o c a b l e H a n d l e r M e t h o d . i n v o k e A n d H a n d l e ( S e r v l e t I n v o c a b l e H a n d l e r M e t h o d . j a v a : 97 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . R e q u e s t M a p p i n g H a n d l e r A d a p t e r . i n v o k e H a n d l e r M e t h o d ( R e q u e s t M a p p i n g H a n d l e r A d a p t e r . j a v a : 827 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . R e q u e s t M a p p i n g H a n d l e r A d a p t e r . h a n d l e I n t e r n a l ( R e q u e s t M a p p i n g H a n d l e r A d a p t e r . j a v a : 738 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . A b s t r a c t H a n d l e r M e t h o d A d a p t e r . h a n d l e ( A b s t r a c t H a n d l e r M e t h o d A d a p t e r . j a v a : 85 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . D i s p a t c h e r S e r v l e t . d o D i s p a t c h ( D i s p a t c h e r S e r v l e t . j a v a : 967 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . D i s p a t c h e r S e r v l e t . d o S e r v i c e ( D i s p a t c h e r S e r v l e t . j a v a : 901 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . p r o c e s s R e q u e s t ( F r a m e w o r k S e r v l e t . j a v a : 970 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . d o G e t ( F r a m e w o r k S e r v l e t . j a v a : 861 ) a t j a v a x . s e r v l e t . h t t p . H t t p S e r v l e t . s e r v i c e ( H t t p S e r v l e t . j a v a : 687 ) a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . s e r v i c e ( F r a m e w o r k S e r v l e t . j a v a : 846 ) a t j a v a x . s e r v l e t . h t t p . H t t p S e r v l e t . s e r v i c e ( H t t p S e r v l e t . j a v a : 790 ) a t i o . u n d e r t o w . s e r v l e t . h a n d l e r s . S e r v l e t H a n d l e r . h a n d l e R e q u e s t ( S e r v l e t H a n d l e r . j a v a : 74 ) a t i o . u n d e r t o w . s e r v l e t . h a n d l e r s . F i l t e r H a n d l e r WriteDispatchChannel.write(HttpServ rExchange.java:2059) at org.xnio.channels.Channels.writeBlocking(Channels.java:152) at io.undertow.servlet.spec.ServletOutputStreamImpl.writeTooLargeForBuffer(ServletOutputStreamImpl.java:197) at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:145) at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2315) at org.apache.commons.io.IOUtils.copy(IOUtils.java:2270) at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2291) at org.apache.commons.io.IOUtils.copy(IOUtils.java:2246) at com.itstyle.cloud.common.elfinder.command.FileCommand.execute(FileCommand.java:69) at com.itstyle.cloud.common.elfinder.command.AbstractCommand.execute(AbstractCommand.java:109) at com.itstyle.cloud.web.CloudDiskController.connector(CloudDiskController.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74) at io.undertow.servlet.handlers.FilterHandlerWriteDispatchChannel.write(HttpServrExchange.java:2059)atorg.xnio.channels.Channels.writeBlocking(Channels.java:152)atio.undertow.servlet.spec.ServletOutputStreamImpl.writeTooLargeForBuffer(ServletOutputStreamImpl.java:197)atio.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:145)atorg.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2315)atorg.apache.commons.io.IOUtils.copy(IOUtils.java:2270)atorg.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2291)atorg.apache.commons.io.IOUtils.copy(IOUtils.java:2246)atcom.itstyle.cloud.common.elfinder.command.FileCommand.execute(FileCommand.java:69)atcom.itstyle.cloud.common.elfinder.command.AbstractCommand.execute(AbstractCommand.java:109)atcom.itstyle.cloud.web.CloudDiskController.connector(CloudDiskController.java:67)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)atjava.lang.reflect.Method.invoke(Method.java:498)atorg.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)atorg.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)atorg.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)atorg.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)atorg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:687)atorg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:790)atio.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)atio.undertow.servlet.handlers.FilterHandlerFilterChainImpl.doFilter(FilterHandler.java:129)

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)

at io.undertow.servlet.handlers.FilterHandlerF i l t e r C h a i n I m p l . d o F i l t e r ( F i l t e r H a n d l e r . j a v a : 131 ) a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . H t t p P u t F o r m C o n t e n t F i l t e r . d o F i l t e r I n t e r n a l ( H t t p P u t F o r m C o n t e n t F i l t e r . j a v a : 108 ) a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . O n c e P e r R e q u e s t F i l t e r . d o F i l t e r ( O n c e P e r R e q u e s t F i l t e r . j a v a : 107 ) a t i o . u n d e r t o w . s e r v l e t . c o r e . M a n a g e d F i l t e r . d o F i l t e r ( M a n a g e d F i l t e r . j a v a : 61 ) a t i o . u n d e r t o w . s e r v l e t . h a n d l e r s . F i l t e r H a n d l e r FilterChainImpl.doFilter(FilterHandler.java:131) at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) at io.undertow.servlet.handlers.FilterHandlerFilterChainImpl.doFilter(FilterHandler.java:131)atorg.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)atio.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)atio.undertow.servlet.handlers.FilterHandlerFilterChainImpl.doFilter(FilterHandler.java:131)

at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)

at io.undertow.servlet.handlers.FilterHandlerF i l t e r C h a i n I m p l . d o F i l t e r ( F i l t e r H a n d l e r . j a v a : 131 ) a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . C h a r a c t e r E n c o d i n g F i l t e r . d o F i l t e r I n t e r n a l ( C h a r a c t e r E n c o d i n g F i l t e r . j a v a : 197 ) a t o r g . s p r i n g f r a m e w o r k . w e b . f i l t e r . O n c e P e r R e q u e s t F i l t e r . d o F i l t e r ( O n c e P e r R e q u e s t F i l t e r . j a v a : 107 ) a t i o . u n d e r t o w . s e r v l e t . c o r e . M a n a g e d F i l t e r . d o F i l t e r ( M a n a g e d F i l t e r . j a v a : 61 ) a t i o . u n d e r t o w . s e r v l e t . h a n d l e r s . F i l t e r H a n d l e r FilterChainImpl.doFilter(FilterHandler.java:131) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) at io.undertow.servlet.handlers.FilterHandlerFilterChainImpl.doFilter(FilterHandler.java:131)atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)atio.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)atio.undertow.servlet.handlers.FilterHandlerFilterChainImpl.doFilter(FilterHandler.java:131)

at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)

at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)

at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:64)

at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)

at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)

at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)

at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)

at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)

at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)

at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)

at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)

at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)

at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)

at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)

at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)

at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)

at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)

at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)

at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)

at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)

at io.undertow.server.Connectors.executeRootHandler(Connectors.java:336)

at io.undertow.server.HttpServerExchange1. r u n ( H t t p S e r v e r E x c h a n g e . j a v a : 830 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( T h r e a d P o o l E x e c u t o r . j a v a : 1149 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r 1.run(HttpServerExchange.java:830) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor1.run(HttpServerExchange.java:830)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

经过日志分析以及效果重现外加Google得出结论

1》服务器在接受请求执行写操作的时候发生的,客户端请求一直长时间等待关闭了连接,这时候服务器端再向客户端已经断开的连接写数据时就发生了Broken pipe异常,我们这时候就要检查为什么服务端处理时间过长,我们可以根据访问的地址以及日志信息定位出现问题的代码,从而准确的定位到源代码从而分析问题。

解决方案:

主要给出两个方面的解决方案 从服务器端解决 本人的服务器配置已经不够支持当前多个服务,我升级了宽带以及内存,问题解决。 程序端解决 造成问题的根本原因是服务器处理写入时间过长,导致响应过慢。

由于我是使用服务器方面的解决方案,实际上是绕过了问题的根点,以后业务加大依旧还是会出现,不过我的设想是,

1》如果图片存在多次重复打开,那我们是不是可以使用缓存的技术把图片缓存起来, 2》在执行写操作的时候,是不是可以利用多线程技术充分的利用多核的优势。 3》异步写入,即在用户点击上一张的时候,程序就开始异步开始操作下一张图片的写入动作。 以上仅仅是个人的设想,还没来得及考证,等时间空闲出来了弄弄。

1 人点赞