aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决
upload ClientException Read timed out
com.aliyun.openservices.ClientException: Read timed out
at com.aliyun.common.utils.ExceptionFactory.createNetworkException(ExceptionFactory.java:19)
at com.aliyun.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:59)
at com.aliyun.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:176)
at com.aliyun.common.comm.ServiceClient.sendRequest(ServiceClient.java:136)
at com.aliyun.openservices.oss.internal.OSSOperation.send(OSSOperation.java:60)
at com.aliyun.openservices.oss.internal.OSSObjectOperation.putObject(OSSObjectOperation.java:76)
at com.aliyun.openservices.oss.OSSClient.putObject(OSSClient.java:303)
at com.ccp.ossbridge.Upload.upload(Upload.java:119)
at com.ccp.ossbridge.Upload.upload(Upload.java:73)
at com.ccp.core.controller.UploadController.uploadFile(UploadController.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at com.ccp.util.web.CcpBaseDispatcherServlet.doDispatch(CcpBaseDispatcherServlet.java:19)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:111)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:282)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:216)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:647)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.aliyun.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:57)
... 41 more
public String uploadImg(String bucketName,String key, InputStream resources, long length , String contentType) { ObjectMetadata objectMeta = new ObjectMetadata(); objectMeta.setContentLength(length); objectMeta.setContentType(contentType); PutObjectResult putObject = OSSclient.putObject(bucketName, key, resources, objectMeta); return putObject.getETag(); }
public String saveImgFileToAliyun(File avatarFile){ contentType = "image/jpeg"; FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(avatarFile); byte[] byteArrayFile = IOUtils.toByteArray(fileInputStream); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayFile); String md5 = Md5Util.getMd5String(byteArrayFile); // check if file exists boolean isExists = checkIfFileExists("xxx", md5); if (isExists) { return md5; }else{ String etag = uploadImg("xxx", md5,byteArrayInputStream,avatarFile.length(), contentType); if (etag.equalsIgnoreCase(md5)) { return etag; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static byte[] input2byte(InputStream inStream) throws IOException { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in2b = swapStream.toByteArray(); return in2b; }
当时由于调用 :byte[] byteArrayFile = IOUtils.toByteArray(fileInputStream);
拿到byte数组去算md5的值,所以就调用了 input2byte方法,红色标红部分把文件流 fileInputStream 的offset指针,指到了文件的末尾.
因为我之前上传部分代码写得是: String etag = uploadImg("xxx", md5,fileInputStream,avatarFile.length(), contentType);
文件流指针在末尾,所以当上传时再次读取这个流对象 fileInputStream的时候文件没有读取到内容,直接导致了 read time out!
后来,解决的方法是再次用byte 数组创建一个流对象,并传递到uploadImg 方法中:
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayFile);
这问题昨天困扰我到凌晨1点半,第二天,去公司问同事李航人称航哥之后解决。在这里感谢下航哥!