Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:82)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:95)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:104)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:62)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:572)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:174)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:125)
at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultServiceClient.java:237)
at com.aliyun.oss.common.comm.DefaultServiceClient.<init>(DefaultServiceClient.java:78)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
at OSSManagerImpl.upload(OSSManagerImpl.java:42)
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:
解决方案:包冲突有以下两种解决方法。
使用统一版本。如果您的工程使用与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时,报类似如下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:76)
Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/protocol/HttpContext
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:76)
Caused by: java.lang.ClassNotFoundException: org.apache.http.protocol.HttpContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
at com.aliyun.oss.internal.ResponseParsers.getXmlRootElement(ResponseParsers.java:645)
at … …
at com.aliyun.oss.OSSClient.doesBucketExist(OSSClient.java:471)
at com.aliyun.oss.OSSClient.doesBucketExist(OSSClient.java:465)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:82)
Caused by: java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 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中,请参考安装中的安装方式二:在Eclipse项目中导入工程依赖的包。
如果您的工程是Ant。请把OSS Java SDK依赖的包,放入工程lib目录;
如果您直接使用javac/java,请使用-classpath/-cp指定OSS Java SDK依赖的包路径,或把OSS Java SDK依赖的包放入classpath路经下。
运行OSS Java SDK程序抛出如下异常:
com.aliyun.oss.ClientException: SocketException
at com.aliyun.oss.common.utils.ExceptionFactory.createNetworkException(ExceptionFactory.java:71)
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:116)
at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:121)
at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:67)
at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:92)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:140)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:111)
at com.aliyun.oss.internal.OSSBucketOperation.getBucketInfo(OSSBucketOperation.java:1152)
at com.aliyun.oss.OSSClient.getBucketInfo(OSSClient.java:1220)
at com.aliyun.oss.OSSClient.getBucketInfo(OSSClient.java:1214)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:94)
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
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
... 9 more
问题原因:Endpoint错误或者网络不通。
解决方案:解决网络问题后重试。
org.apache.http.NoHttpResponseException: The target server failed to respond
运行OSS Java SDK程序抛出如下异常:
问题原因:使用过期的连接会导致上述错误。该错误仅在Java SDK 2.1.2前的版本出现。
解决方案:请升级OSS Java SDK到2.1.2及以后版本。
调用OSS Java SDK不响应
调用OSS Java SDK不响应,通过jstack -l <pid>
查看堆栈,问题出现在如下位置:
"main" prio=6 tid=0x000000000291e000 nid=0xc40 waiting on condition [0x0000000002dae000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d85697f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:138)
at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306)
at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192)
at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185)
at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:123)
at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:68)
at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:94)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:146)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:113)
at com.aliyun.oss.internal.OSSObjectOperation.getObject(OSSObjectOperation.java:229)
at com.aliyun.oss.OSSClient.getObject(OSSClient.java:629)
at com.aliyun.oss.OSSClient.getObject(OSSClient.java:617)
at samples.HelloOSS.main(HelloOSS.java:49)
问题原因:连接池中连接泄漏,可能是使用OSSClient.getObject
,但是没有关闭。
解决方案:请检查您的程序,确保没有连接泄漏。关闭方法如下:
// 读取文件
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
// OSS操作
// 关闭ossObject
ossObject.close();
您在使用OSSClient.getObject时,报类似如下错误:
Exception in thread "main" org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 11990526; received: 202880
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:200)
at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:103)
at org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:128)
at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228)
at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:174)
at java.io.FilterInputStream.close(FilterInputStream.java:181)
at java.io.FilterInputStream.close(FilterInputStream.java:181)
at com.aliyun.oss.event.ProgressInputStream.close(ProgressInputStream.java:147)
at java.io.FilterInputStream.close(FilterInputStream.java:181)
at samples.HelloOSS.main(HelloOSS.java:39)
问题原因:两次读取数据间隔时间超过1分钟。OSS会关闭超过1分钟没有发送/接收数据的连接。如果您的应用模式是,读取部分数据—处理数据—读取数据—处理数据,且处理数据的时间不固定。
解决方案:如果您每次读取部分数据进行处理,且处理数据的时间不固定,建议指定范围读取,避免数据读取时连接关闭。
调调用OSS Java SDK的程序,运行一段时间(根据业务量,几小时到几天不等)后内存泄露。
问题原因:请使用内存分析工具,分析内存使用情况。推荐使用Eclipse Memory Analyzer(MAT),使用方法请参见使用MAT进行堆转储文件分析。
如果分析结果类似下图所示(PoolingHttpClientConnectionManager占96%的内存),原因是程序中可能多次执行new OSSClient,但是没有调用ossClient.shutdown,造成内存泄漏。
解决方案:new OSSClient一定要和ossClient.shutdown成对使用。
调用OSSClient.shutdown抛异常InterruptedException
调用OSSClient.shutdown抛如下异常:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.aliyun.oss.common.comm.IdleConnectionReaper.run(IdleConnectionReaper:76)
问题原因:OSSClient后台线程IdleConnectionReaper,定时关闭闲置连接。IdleConnectionReaper线程在sleep时,调用shutdown,就会抛出上面的异常。
解决方案:
OSS Java SDK 2.3.0已经修复该问题,请升级您的SDK版本。
OSS Java SDK 2.3.0之前的版本,请使用如下代码,忽略该异常:
try {
serviceClient.shutdown();
} catch(Exception e) {