代码如下:
// 上传文件
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]