gpt4 book ai didi

java.net.SocketException : Connection reset while reading large files 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:10 42 4
gpt4 key购买 nike

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

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

我们使用 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 环境中。已验证共享点设置和 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)

谢谢!

最佳答案

对等方关闭连接,然后您向其写入,然后它发出重置,然后您尝试读取。这是某种应用程序协议(protocol)错误。可能您向它发送了无效的内容以使其关闭。

关于java.net.SocketException : Connection reset while reading large files 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21144193/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com