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

时间:2014-01-15 17:25:44

标签: java networking weblogic apache-httpclient-4.x

我们为移动应用程序构建了基于Java的REST Web服务,以便与Sharepoint服务器进行交互。我们在weblogic服务器上托管webservices。文本数据以JSON格式传输,文件资产作为二进制流传输到iPad应用程序。我们使用HTTP GET从sharepoint检索文件资产。我们在尝试检索大于20MB且仅在生产环境中的文件资产时一直注意到问题。

对于大于20MB的文件,我们注意到java.net.SocketException:Connection reset或java.net.SocketException:Socket关闭,具体取决于套接字何时关闭。

我们使用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);
        }
    }

}

这只发生在我们无法安装wireshark以进一步调试的生产和uat环境中。验证了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)

谢谢!

1 个答案:

答案 0 :(得分:2)

对等关闭了连接,然后你写了它,然后它发出了一个重置​​,然后你试了一个读。这是某种应用程序协议错误。可能你发了一些无效的东西让它关闭。