OSS循环上传30多个文件, 有一个文件显示上传超时,其他都成功了

代码如下:

// 上传文件
private static void uploadFile(OSSClient client, String bucketName, String key, File file)
        throws OSSException, ClientException, IOException {

    ObjectMetadata objectMeta = new ObjectMetadata();
    objectMeta.setContentLength(file.length());
    String fileName=key.substring(key.lastIndexOf("."));
    if(fileName.equalsIgnoreCase(".pdf")) {
        objectMeta.setContentType("application/pdf");
    }else if(fileName.equalsIgnoreCase(".zip")){
        objectMeta.setContentType("application/octet-stream");
    }else{
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
    }
    InputStream input = new FileInputStream(file);
   client.putObject(bucketName, key, input, objectMeta);
    input.close();

}

控制台输出如下:

12-Jun-2018 09:18:44.694 警告 [http-nio-8084-exec-7] com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl Unable to execute HTTP request: Read timed out

12-Jun-2018 09:19:37.353 警告 [http-nio-8084-exec-7] com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl Unable to execute HTTP request: Read timed out

2018-06-12 09:19:37.368 | UPLOAD FILE ERROR !
com.aliyun.oss.ClientException: Read timed out

at com.aliyun.oss.common.utils.ExceptionFactory.createNetworkException(ExceptionFactory.java:65) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:62) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:126) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:72) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:92) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:133) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:111) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.internal.OSSObjectOperation.writeObjectInternal(OSSObjectOperation.java:518) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.internal.OSSObjectOperation.putObject(OSSObjectOperation.java:112) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.OSSClient.putObject(OSSClient.java:407) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.aliyun.oss.OSSClient.putObject(OSSClient.java:394) ~[aliyun-sdk-oss-2.0.5.jar:na]
at com.tmbj.api.service.imgupload.UploadService.uploadFile(UploadService.java:104) ~[classes/:na]
at com.tmbj.api.service.imgupload.UploadService.upload(UploadService.java:39) ~[classes/:na]
at com.tmbj.api.service.imgupload.UploadService$$EnhancerByCGLIB$$487d1c8f.CGLIB$upload$0(<generated>) [cglib-nodep-3.1.jar:na]
at com.tmbj.api.service.imgupload.UploadService$$EnhancerByCGLIB$$487d1c8f$$FastClassByCGLIB$$3380697a.invoke(<generated>) [cglib-nodep-3.1.jar:na]
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) [cglib-nodep-3.1.jar:na]
at com.jfinal.aop.Invocation.invoke(Invocation.java:82) [classes/:na]
at com.jfinal.aop.Callback.intercept(Callback.java:96) [classes/:na]
at com.tmbj.api.service.imgupload.UploadService$$EnhancerByCGLIB$$487d1c8f.upload(<generated>) [cglib-nodep-3.1.jar:na]
at com.tmbj.api.controller.TmInsureFenqiController.setContractValue(TmInsureFenqiController.java:1305) [classes/:na]
at com.tmbj.api.controller.TmInsureFenqiController.createLoadContract(TmInsureFenqiController.java:998) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at com.jfinal.aop.Invocation.invoke(Invocation.java:73) [classes/:na]
at com.tmbj.api.session.CorsInterceptor.intercept(CorsInterceptor.java:35) [classes/:na]
at com.jfinal.aop.Invocation.invoke(Invocation.java:67) [classes/:na]
at com.tmbj.api.exception.TmbjExceptionInterceptor.intercept(TmbjExceptionInterceptor.java:28) [classes/:na]
at com.jfinal.aop.Invocation.invoke(Invocation.java:67) [classes/:na]
at com.tmbj.api.session.SessionInterceptor.intercept(SessionInterceptor.java:39) [classes/:na]
at com.jfinal.aop.Invocation.invoke(Invocation.java:67) [classes/:na]
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74) [classes/:na]
at com.jfinal.ext.handler.UrlSkipHandler.handle(UrlSkipHandler.java:45) [classes/:na]
at com.jfinal.plugin.druid.DruidStatViewHandler.handle(DruidStatViewHandler.java:75) [classes/:na]
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72) [classes/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [catalina.jar:8.5.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:8.5.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [catalina.jar:8.5.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:8.5.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [catalina.jar:8.5.31]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) [catalina.jar:8.5.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:8.5.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-coyote.jar:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-coyote.jar:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-coyote.jar:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.31]

at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

2个回答

超时可能是网络原因引起的,这种文件上传的逻辑建议增加重传机制,可以使用生产者-消费者模式,对于上传失败的重新扔回队列,让消费者尝试重新上传。

weixin_36241363
秦艾德 谢谢老哥的热心解答!,再问个问题哈,API项目中49个文件上传花费了82136毫秒,调用API的项目设置了6000毫秒的超时时间,不想改动项目的超时配置,设置重连可以解决问题不?会不会产生脏数据?
一年多之前 回复

重传是可以解决因超时导致失败的问题的,如果你的OSS系统可以覆盖同名文件的话,就不存在脏数据问题了,阿里云的OSS系统是支持覆盖同名文件的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!