2014-01-15 47 views
3

我們已經爲移動應用程序構建了基於Java的REST Webservices以與Sharepoint服務器進行交互。我們在weblogic服務器上託管web服務。文本數據以JSON格式傳輸,文件資產作爲二進制流傳輸到iPad應用程序。我們使用HTTP GET從共享點檢索文件資產。在嘗試檢索大於20MB的文件資產時,我們一直在注意問題,並且僅在生產環境中檢索。java.net.SocketException:在讀取大文件時重置連接

對於大於20MB的文件,我們一直注意到java.net.SocketException:Connection reset或java.net.SocketException:套接字關閉,具體取決於套接字何時關閉。

我們使用Apache HTTPClient 4.2作爲http客戶端和apache commons IO庫來複制輸出流。

下面是代碼 -

public org.apache.http.HttpEntity getAssetEntity(final String uri) { 


    DefaultHttpClient client = new DefaultHttpClient(); 
    client.getParams().setParameter("http.connection.stalecheck", new Boolean(true)); 
    authProvider.addAuthentication(client); 

    // Encode only special chars and not the whole URI 
    String repURI = ""; 
    try { 
     URL url = new URL(uri); 
     URI httpURI = new URI(url.getProtocol(), url.getUserInfo(), 
       url.getHost(), url.getPort(), url.getPath(), 
       url.getQuery(), url.getRef()); 
     repURI = httpURI.toString(); 
    } catch (Exception e) { 
     throw new SharePointClientException(e); 
    } 

    LOGGER.debug("Requesting OutputStream from URL:" + repURI); 
    HttpGet httpget = new HttpGet(repURI); 
    HttpResponse response = null; 
    try { 
     response = client.execute(httpget); 
     org.apache.http.HttpEntity ent = response.getEntity(); 
     return ent; 
    } catch (IOException e) { 
     throw new SharePointClientException(e); 
    } 

} 

protected StreamingOutputDetails getStreamingOutputForChapterAsset(final String assetURL) throws AuthorizationException { 
    final HttpEntity assetEntity = getClient().getAssetEntity(assetURL); 
    final StreamingOutputDetails streamingOutputDetails = new StreamingOutputDetails(); 
    streamingOutputDetails.setOutputSize((int) assetEntity 
      .getContentLength()); 
    streamingOutputDetails.setStreamingOutput(new StreamingOutput() { 
     @Override 
     public void write(OutputStream output) throws IOException { 
      try { 
       getClient().streamFileAsset(assetEntity, output); 
      } catch (SharePointClientException e) { 
       // since write() throws IOException, we need to throw a 
       // checked exception, 
       // so wrap the current exception in an IOException 
       throw new IOException(e); 
      } catch (SharePointResourceException e) { 
       // since write() throws IOException, we need to throw a 
       // checked exception, 
       // so wrap the current exception in an IOException 
       throw new IOException(e); 
      } catch (AuthorizationException e) { 
       throw new IOException(e); 
      } 
     } 
    }); 
    return streamingOutputDetails; 
} 

    @Override 
public void streamFileAsset(org.apache.http.HttpEntity assetEntity, 
     OutputStream output) { 

    InputStream contentStream = null; 
    CloseShieldInputStream closeShieldInputStream = null; 
    int bytes = 0; 
    try { 

     contentStream = assetEntity.getContent(); 
     closeShieldInputStream = new CloseShieldInputStream(contentStream); 
     bytes = IOUtils.copy(closeShieldInputStream, output); 
     EntityUtils.consume(assetEntity); 

    } catch (IOException e) { 
     throw new SharePointClientException(e); 
    } finally { 

     LOGGER.debug("bytes copied to output stream:" + bytes); 
     if(null != closeShieldInputStream) { 
      IOUtils.closeQuietly(closeShieldInputStream); 
     } 
     if (null != contentStream) { 
      IOUtils.closeQuietly(contentStream); 
     } 
    } 

} 

這是隻在生產和UAT在這裏我們可以沒有安裝進一步調試這個Wireshark的環境發生。已驗證sharepoint設置和weblogic設置,它們與其他環境相同。

下面是錯誤的堆棧跟蹤 -

Caused by: java.net.SocketException: Connection reset 
at java.net.SocketInputStream.read(SocketInputStream.java:168) 
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:204) 
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:182) 
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:204) 
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:155) 
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792) 
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769) 
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744) 
at com.test.client.SharePointServerListClient.streamFileAsset(SharePointServerListClient.java:217) 

謝謝!

+0

重置之前連接持續了多長時間? – Typo

回答

2

對方關閉連接,然後你寫信給它,然後它發出重置,然後你嘗試了一個閱讀。這是某種應用程序協議錯誤。可能你發送了一些無效的東西讓它關閉。

+0

>可能你發送了一些無效的信息讓它關閉:很可能是最大請求長度上限 – oleg

相關問題