全部产品
阿里云办公

常见问题

更新时间:2018-06-25 19:16:04

包冲突

错误原因

使用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 jar包。要查看工程使用的jar包及版本,请在您的工程目录下执行mvn dependency:tree。如下图所示,您的工程里使用了Apache httpclient 4.3:

java-sdk-question1

解决方法

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

  • 使用统一版本。如果您的工程使用与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类似。

java-sdk-question-2

缺少包

错误原因

使用OSS Java SDK时,报类似如下错误,说明您的工程中可能缺少编译或运行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依赖下列包:

  • 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程序时,报类似如下错误,可能的原因是Endpoint错误或者网络不通。

  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

解决方法

您可以使用OSSProbe工具快速定位错误原因,从而解决问题。

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

错误原因

运行OSS Java SDK程序时,报类似如下错误:

java-sdk-question-3

使用过期的连接会导致上述错误。该错误仅在Java SDK 2.1.2前的版本出现。

解决方法

请升级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)

原因是连接池中连接泄漏,可能是使用ossObject后没有关闭。

解决方法

请检查您的程序,确保没有连接泄漏。关闭方法如下:

  1. // 读取文件
  2. OSSObject ossObject = ossClient.getObject(bucketName, objectName);
  3. // OSS操作
  4. // 关闭ossObject
  5. ossObject.close();

问题排查的详细步骤,请参见OSS Java SDK夯住问题排查

连接关闭

错误原因

如果您在使用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%的内存),原因是程序中可能多次执行new OSSClient,但是没有调用ossClient.shutdown,造成内存泄漏。

java-leaker

解决方法

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时,调用ossClient.shutdown,就会抛出上面的异常。

解决方法

OSS Java SDK 2.3.0已经修复该问题。

OSS Java SDK 2.3.0之前的版本,请使用如下代码,忽略该异常:

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

其它错误

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

本文导读目录