2014-01-15 2 views
3

Мы создали Java-сервисы REST для мобильного приложения для взаимодействия с сервером Sharepoint. Мы размещаем web-сервисы на веб-сервере. Текстовые данные передаются в формате JSON, а файловые ресурсы переносятся в приложение iPad как двоичный поток. Мы используем HTTP GET для извлечения файлового ресурса из sharepoint. Мы замечаем проблемы при попытке получить файловые ресурсы размером более 20 МБ и только в производственной среде.java.net.SocketException: Сброс соединения при чтении больших файлов

Для файлов размером более 20 МБ мы замечаем java.net.SocketException: Сброс соединения или java.net.SocketException: Розетка закрыта в зависимости от того, когда сокет закрыт.

Мы используем Apache HTTPClient 4.2 в качестве http-клиента и apos 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

Смежные вопросы