开发者社区> 问答> 正文

OSS通过javaAPI分段下载的这个异常怎么解决啊?

尝试通过GetObjectRequest获取Object,代码这样的

                ObjectMetadata meta = client.getObjectMetadata(DEFAULT_BUCKET_NAME,"test1");
                Long fileLength = meta.getContentLength();
                OSSObject object;
                GetObjectRequest getObjectRequest = new GetObjectRequest(DEFAULT_BUCKET_NAME, "test1");
                long count = fileLength / 1000   1;
                long start = 0;
                long end = 1000;
                for (long i = 0; i < count; i  ) {
                        start = i * 1000;
                        end = (i   1) * 1000;
                        // 获取0~1000字节范围内的数据
                        getObjectRequest.setRange(start, end);
                        // 获取Object,返回结果为OSSObject对象
                        System.out.println("prepare to get 1000: "   i);
                        object = client.getObject(getObjectRequest);

                        InputStream content = object.getObjectContent();
                        System.out.println("prepare to write 1000: "   i);
                        try {
                                FileOutputStream writeFile = new FileOutputStream("D:\\temp\\down\\Mlog.txt", true);
                                OutputStreamWriter out = new OutputStreamWriter(writeFile);
                                out.write(content.read());
                                System.out.println("write 1000: "   i);
                                content.close();
                                out.close();
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                  e.printStackTrace();
                 }



运行到红色这一行,就一直连接不上



信息: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed
2014-2-7 13:25:13 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
Exception in thread "main" com.aliyun.openservices.ClientException: 网络连接错误,详细信息:Software caused connection abort: recv failed         
          at com.aliyun.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:198)
        at com.aliyun.common.comm.ServiceClient.sendRequest(ServiceClient.java:138)
        at com.aliyun.openservices.oss.internal.OSSOperation.send(OSSOperation.java:59)
        at com.aliyun.openservices.oss.internal.OSSObjectOperation.getObject(OSSObjectOperation.java:174)
        at com.aliyun.openservices.oss.OSSClient.getObject(OSSClient.java:336)
        at test.OssDownload.getBreakpoint(OssDownload.java:84)
        at test.OssDownload.main(OssDownload.java:48)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
        at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:111)
        at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
        at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:282)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
        at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:216)
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:647)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
        at com.aliyun.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:51)
        at com.aliyun.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:175)
        ... 6 more



直接用OSSObject object = client.getObject(DEFAULT_BUCKET_NAME, "test1");  是没有问题的

展开
收起
飞天猪 2014-02-07 15:35:10 12760 0
2 条回答
写回答
取消 提交回答
  • ReOSS通过javaAPI分段下载的这个异常怎么解决啊?
    分段读是可以的呀,但是代码中几个问题:
    1.  long count = fileLength / 1000  1; 这个可能会出问题,如果fileLength能被1000整除, 1 已经越界
    2. range应该在0-(fileLength-1)之间,end = (i 1)*1000有可能大于fileLength-1
    3. out.write(content.read());只输出一个字符到out,没有读全object 内容
    4.  content.close(); 出错,out.close(); 不能执行到
    2014-02-11 16:44:14
    赞同 展开评论 打赏
  • ReOSS通过javaAPI分段下载的这个异常怎么解决啊?
    今天直接用了HttpURLConnection,一旦设置了Range,也是异常
    我是这样写的 setRequestProperty("Range", "bytes=0-100")
    异常是
    java.net.SocketException: Software caused connection abort: recv failed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
        at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:695)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:640)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:660)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)

    -------------------------

    ReOSS通过javaAPI分段下载的这个异常怎么解决啊?
    这个 因为我是写个testcase,所以用的文件我知道不会整除的,就忽略细节了
    content.read() 是写错了,不过这些都在出异常的位置之后,应该不会有影响的
    2014-02-08 14:44:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载