全部产品
对象存储 OSS

常见问题

更新时间:2017-06-13 11:34:30   分享:   

包冲突

如果您在使用OSS Java SDK时,报如下或类似错误:

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy
  2. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
  3. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
  4. at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:77)
  5. Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy
  6. at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  7. at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  8. at java.security.AccessController.doPrivileged(Native Method)
  9. at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  10. at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  11. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  12. at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  13. ... 3 more

  1. Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
  2. at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
  3. at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
  4. at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
  5. at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:82)
  6. at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:95)
  7. at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:104)
  8. at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:62)
  9. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:572)
  10. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:174)
  11. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
  12. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
  13. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:125)
  14. at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultServiceClient.java:237)
  15. at com.aliyun.oss.common.comm.DefaultServiceClient.<init>(DefaultServiceClient.java:78)
  16. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
  17. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
  18. at OSSManagerImpl.upload(OSSManagerImpl.java:42)
  19. at OSSManagerImpl.main(OSSManagerImpl.java:63)

您的工程里可能有包冲突。原因是,OSS Java SDK使用了Apache httpclient 4.4.1,您的工程使用了与Apache httpclient 4.4.1冲突的Apache httpclient或commons-httpclient。请在您的工程目录下执行“mvn dependency:tree”,查看工程使用的包及版本。如上述发生错误的工程里,使用了Apache httpclient 4.3:

包冲突有以下两种解决方法:

  • 使用统一版本。如果您的工程里使用与Apache httpclient 4.4.1冲突的版本,请您也使用4.4.1版本。在pom.xml去掉其它版本的Apache httpclient依赖。如果您的工程使用了commons-httpclient也可能存在冲突,请去除commons-httpclient。
  • 解除依赖冲突。如果您的工程依赖与多个第三方包,而第三方包又依赖不同版本的Apache httpclient,您的工程里会有依赖冲突,请使用exclusion解除。详细请参考maven guides

OSS Java SDK依赖以下版本的包,冲突解决办法与httpclient类似,不再赘述。

缺少包

编译/运行OSS Java SDK报如下错误:

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
  2. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
  3. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
  4. at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:76)
  5. Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
  6. at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  7. at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  8. at java.security.AccessController.doPrivileged(Native Method)
  9. at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  10. at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  11. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  12. at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  13. ... 3 more

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/protocol/HttpContext
  2. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
  3. at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
  4. at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:76)
  5. Caused by: java.lang.ClassNotFoundException: org.apache.http.protocol.HttpContext
  6. at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  7. at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  8. at java.security.AccessController.doPrivileged(Native Method)
  9. at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  10. at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  11. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  12. at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  13. ... 3 more

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
  2. at com.aliyun.oss.internal.ResponseParsers.getXmlRootElement(ResponseParsers.java:645)
  3. at
  4. at com.aliyun.oss.OSSClient.doesBucketExist(OSSClient.java:471)
  5. at com.aliyun.oss.OSSClient.doesBucketExist(OSSClient.java:465)
  6. at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:82)
  7. Caused by: java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder
  8. at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  9. at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  10. at java.security.AccessController.doPrivileged(Native Method)
  11. at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  12. at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  13. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  14. at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  15. ... 11 more

等类似错误,说明您的工程缺少OSS Java SDK编译或运行必须的包。OSS Java SDK依赖下列包:

  • aliyun-sdk-oss-2.2.1.jar
  • hamcrest-core-1.1.jar
  • jdom-1.1.jar
  • commons-codec-1.9.jar
  • httpclient-4.4.1.jar
  • commons-logging-1.2.jar
  • httpcore-4.4.1.jar
  • log4j-1.2.15.jar

其中,log4j-1.2.15.jar是可选的,需要日志功能的时加入该包。其它包都是必不可少的。

解决办法:您的工程中在加入OSS Java SDK依赖的包,加入方法如下:

  • 如果您的工程是Eclipse。请参考Java-SDK使用手册中的安装方式二:在Eclipse项目中导入工程依赖的包。
  • 如果您的工程是Ant。请把OSS Java SDK依赖的包,放入工程lib目录。
  • 如果您直接使用javac/java,请使用-classpath/-cp指定OSS Java SDK依赖的包路径,或把OSS Java SDK依赖的包放入classpath路经下。

连接超时

运行OSS Java SDK程序抛出如下异常:

  1. com.aliyun.oss.ClientException: SocketException
  2. at com.aliyun.oss.common.utils.ExceptionFactory.createNetworkException(ExceptionFactory.java:71)
  3. at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:116)
  4. at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:121)
  5. at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:67)
  6. at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:92)
  7. at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:140)
  8. at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:111)
  9. at com.aliyun.oss.internal.OSSBucketOperation.getBucketInfo(OSSBucketOperation.java:1152)
  10. at com.aliyun.oss.OSSClient.getBucketInfo(OSSClient.java:1220)
  11. at com.aliyun.oss.OSSClient.getBucketInfo(OSSClient.java:1214)
  12. at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:94)
  13. Caused by: org.apache.http.conn.HttpHostConnectException: Connect to oss-test.oss-cn-hangzhou-internal.aliyuncs.com:80 [oss-test.oss-cn-hangzhou-internal.aliyuncs.com/10.84.135.99] failed: Connection timed out: connect
  14. at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
  15. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
  16. at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
  17. at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
  18. at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
  19. at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
  20. at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
  21. at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
  22. at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
  23. ... 9 more

原因是endpoint错误或者网络不通,如果不能直接找出错误。请使用OSSProbe工具检测,OSSProbe会给出可能的错误原因。

org.apache.http.NoHttpResponseException: The target server failed to respond

运行OSS Java SDK程序抛出如下异常:

原因是:重用连接前没有检测连接是否有效,过期的连接重用会导致上述错误。该问题是Apache httpclient 4.4的bug,详见HTTPCLIENT-1609,4.4.1及以后版本修复。Java SDK 2.1.2前的版本使用的是Apache httpclient 4.4,存在上述问题;Java SDK 2.1.2及以后的版本,使用的是Apache httpclient 4.4.1,修复了该问题。如果发现该问题请升级OSS Java SDK到2.1.2及以后版本

调用OSS Java SDK夯住

调用OSS Java SDK夯住(hang),通过jstack -l <pid>查看堆栈,夯在如下的位置:

  1. "main" prio=6 tid=0x000000000291e000 nid=0xc40 waiting on condition [0x0000000002dae000]
  2. java.lang.Thread.State: WAITING (parking)
  3. at sun.misc.Unsafe.park(Native Method)
  4. - parking to wait for <0x00000007d85697f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
  5. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
  6. at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
  7. at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:138)
  8. at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306)
  9. at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
  10. at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192)
  11. at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185)
  12. at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107)
  13. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276)
  14. at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263)
  15. at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)
  16. at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
  17. at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
  18. at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
  19. at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
  20. at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
  21. at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:123)
  22. at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:68)
  23. at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:94)
  24. at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:146)
  25. at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:113)
  26. at com.aliyun.oss.internal.OSSObjectOperation.getObject(OSSObjectOperation.java:229)
  27. at com.aliyun.oss.OSSClient.getObject(OSSClient.java:629)
  28. at com.aliyun.oss.OSSClient.getObject(OSSClient.java:617)
  29. at samples.HelloOSS.main(HelloOSS.java:49)

原因: 连接池中连接泄漏,可能是使用OSSClient.getObject,但是没有关闭。请检查您的程序,确保没有连接泄漏。关闭方法如下:

  1. // 读取Object
  2. OSSObject ossObject = ossClient.getObject(bucketName, key);
  3. // 使用ObjectContent和ObjectMetadata
  4. // ...
  5. // 使用完毕后,一定要close
  6. ossObject.getObjectContent().close();

OSS Java SDK应用夯住问题排除的详细步骤,请参看 夯住(Hang)问题排查

ConnectionClosedException: Premature end of Content-Length delimited message body …

如果您在使用OSSClient.getObject时,报如下或类似错误:

  1. Exception in thread "main" org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 11990526; received: 202880
  2. at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
  3. at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:200)
  4. at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:103)
  5. at org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:128)
  6. at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228)
  7. at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:174)
  8. at java.io.FilterInputStream.close(FilterInputStream.java:181)
  9. at java.io.FilterInputStream.close(FilterInputStream.java:181)
  10. at com.aliyun.oss.event.ProgressInputStream.close(ProgressInputStream.java:147)
  11. at java.io.FilterInputStream.close(FilterInputStream.java:181)
  12. at samples.HelloOSS.main(HelloOSS.java:39)

原因:两次读取数据间隔时间超过1分钟。OSS会关闭超过1分钟没有发送/接收数据的连接。如果您的应用模式是,读取部分数据——处理数据——读取数据——处理数据,且处理数据的时间不固定。建议使用 指定范围读取,避免数据读取时连接关闭。

内存泄露

调用OSS Java SDK的程序,运行一段时间(看业务量,几小时到几天不等)后内存爆掉。请使用内存分析工具,分析内存使用情况。推荐使用 Eclipse Memory Analyzer(MAT),使用方法见 使用 MAT进行堆转储文件分析。如果分析结果类似下图,PoolingHttpClientConnectionManager占96%的内存。

java-leaker

原因:程序中可能多次new OSSClient,但是没有调用OSSClient.shutdown,造成对象泄漏。提示new OSSClient一定要和OSSClient.shutdown成对使用。

调用OSSClient.shutdown抛异常InterruptedException

调用OSSClient.shutdown抛如下异常:

  1. java.lang.InterruptedException: sleep interrupted
  2. at java.lang.Thread.sleep(Native Method)
  3. at com.aliyun.oss.common.comm.IdleConnectionReaper.run(IdleConnectionReaper:76)

原因:OSSClient后台线程IdleConnectionReaper,定时关闭闲置连接。IdleConnectionReaper线程在sleep时,调用shutdown,就会抛出上面的异常。OSS Java SDK v2.3.0已经修复该问题。您可以使用如下代码,忽略该异常:

  1. try {
  2. serviceClient.shutdown();
  3. } catch(Exception e) {
  4. }

其它错误

其它OSS返回错误的排查,请参看常见错误及排查

本文导读目录
本文导读目录
以上内容是否对您有帮助?