简体   繁体   English

java.net.SocketException:读取大文件时重置连接

[英]java.net.SocketException: Connection reset while reading large files

We have built java based REST Webservices for the mobile app to interact with Sharepoint server. 我们为移动应用程序构建了基于Java的REST Web服务,以便与Sharepoint服务器进行交互。 We are hosting webservices on weblogic server. 我们在weblogic服务器上托管webservices。 Textual data is transferred in JSON format and file assets are transferred to iPad application as a binary stream. 文本数据以JSON格式传输,文件资产作为二进制流传输到iPad应用程序。 We are using HTTP GET to retrieve the file asset from the sharepoint. 我们使用HTTP GET从sharepoint检索文件资产。 We have been noticing issues while trying to retrieve file assets which are greater than 20MB and only in production environment. 我们在尝试检索大于20MB且仅在生产环境中的文件资产时一直注意到问题。

For files greater than 20MB, we have been noticing java.net.SocketException:Connection reset or java.net.SocketException: Socket closed depending on when the socket is closed. 对于大于20MB的文件,我们注意到java.net.SocketException:Connection reset或java.net.SocketException:Socket关闭,具体取决于套接字何时关闭。

We are using Apache HTTPClient 4.2 as http client and apache commons IO library for copy the output stream. 我们使用Apache HTTPClient 4.2作为http客户端和apache commons IO库来复制输出流。

Below is the code - 以下是代码 -

  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);
        }
    }

}

This is only happening in production and uat environments where we cannot have wireshark installed to debug this further. 这只发生在生产和uat环境中,我们无法安装wireshark来进一步调试。 Have validated the sharepoint setting and weblogic settings and they are same as other environments. 验证了sharepoint设置和weblogic设置,它们与其他环境相同。

Below is the stack trace of the error - 下面是错误的堆栈跟踪 -

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)

Thanks! 谢谢!

The peer closed the connection, then you wrote to it, then it issued a reset, then you tried a read. 对等关闭了连接,然后你写了它,然后它发出了一个重置​​,然后你试了一个读。 This is an application protocol error of some kind. 这是某种应用程序协议错误。 Probably you sent it something invalid to make it close. 可能你发了一些无效的东西让它关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM