阿里雲團隊努力不懈,力求將最新的技術內容更快地以您最熟悉的語言呈現。本文由簡體中文內容自動轉碼而成,過程無人工干預。阿里雲不保證此自動轉碼的準確性、完整性及時效性。因轉碼造成的任何內容錯誤及因此可能帶來的損失,阿里雲概不負責,敬請見諒。本文内容請以簡體中文版本為準。
全部產品
Search
文件中心

常見問題

更新時間: Oct 19, 2018

包衝突

錯誤原因

使用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返回錯誤的排查,請參見常見錯誤及排查