[英]Software caused connection abort: socket write error while writing to DataOutputStream
private DataOutputStream output = null;
private int send(String str) throws IOException {
int response = 0;
output.writeBytes(str);
output.flush();
return response;
} }
While writing String to writeBytes it is throwing error like java.net.SocketException: Software caused connection abort: socket write error java.lang.ArrayIndexOutOfBoundsException: 512 将String写入writeBytes时会引发类似java.net.SocketException的错误:软件导致连接中止:套接字写入错误java.lang.ArrayIndexOutOfBoundsException:512
DEBUG McaFee:123 - Exception java.net.SocketException: Software caused connection abort: socket write error McaFeeC:83 - Request Header **** GET http://localhost:8080/appApp \\filepath\\ACH_IMAGE_FIVE_TWO_FIVE_zip HTTP/1.1 调试McaFee:123-异常java.net.SocketException:软件导致连接中止:套接字写入错误McaFeeC:83-请求标头**** GET http:// localhost:8080 / appApp \\ filepath \\ ACH_IMAGE_FIVE_TWO_FIVE_zip HTTP / 1.1
Your question misses the part how you initialize the DataOutputStream
but the error message lets me assume that you open an HttpUrlConnection
to a server and call getOutputStream
on it. 您的问题错过了如何初始化
DataOutputStream
的部分,但是错误消息让我假设您打开了到服务器的HttpUrlConnection
并在其上调用getOutputStream
。
Your question also misses the part where you create the String
that is passed to the send
method and if you indeed create a ZIP-file using a String
to contain that binary data will most likely lead to corrupted data at some point. 您的问题还错过了创建传递给
send
方法的String
的部分,如果您确实使用String
包含二进制数据来创建ZIP文件,则很可能会在某些时候导致数据损坏。
My answer is based on the following code I assume is working here: 我的答案基于我认为在此处工作的以下代码:
HttpUrlConnection conn = new URL("www.example.com/upload").openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
String dataToSend = createDataToSend();
dos.writeBytes(dataToSend.getBytes("8859_1"));
What can happen here is that the HttpUrlConnection
is opening the connection to the server and then the connections sits idle until you've finished creating the ZIP. 此处可能发生的情况是
HttpUrlConnection
正在打开与服务器的连接,然后连接处于空闲状态,直到完成创建ZIP为止。 If it takes too long the server will run into a timeout and will close the connection. 如果花费的时间太长,则服务器将超时并关闭连接。 Trying to send data to a closed socket lead to the error message you've seen.
尝试将数据发送到封闭的套接字会导致您看到错误消息。
The reason why it works up to a given number of files is that the generation is then quick enough to stay within the timeout. 它可以处理给定数量的文件的原因是,生成速度足够快,可以保持在超时范围内。
There are different solutions for this: 有不同的解决方案:
Which one is better depends. 哪个更好取决于。 If you can be sure that there will be no error while creating the ZIP it would be my preferred way.
如果可以确定在创建ZIP时没有任何错误,那将是我的首选方式。 The former would prevent the sending of incomplete data in case of an error.
前者可以防止发生错误时发送不完整的数据。 Note that the latter needs Chunked File-Transfer-Encoding, otherwise the
HttpUrlConnection
will buffer everything to evaluate the value for the Content-Length
header that would otherwise be needed. 请注意,后者需要Chunked File-Transfer-Encoding,否则
HttpUrlConnection
将缓冲所有内容以评估原本需要的Content-Length
标头的值。 So if you transfer data to a server that is not supporting this, you end up with variant 1. 因此,如果将数据传输到不支持此功能的服务器,则最终会出现变体1。
Here is an example for the latter: 这是后者的示例:
private int doSend(File baseDir) throws IOException {
File[] files = baseDir.listFiles();
if (files.length == 0) {
return -1;
}
HttpURLConnection conn = (HttpURLConnection) new URL("www.example.com/upload").openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setChunkedStreamingMode(250);
OutputStream os = conn.getOutputStream();
ZipOutputStream zos = new ZipOutputStream(os);
for (int i = 0; i < files.length; i++) {
ZipEntry ze = new ZipEntry(files[i].getName());
zos.putNextEntry(ze);
try (FileInputStream fis = new FileInputStream(files[i])) {
byte[] buf = new byte[4096];
int read;
while ((read = fis.read(buf)) != -1) {
zos.write(buf, 0, read);
}
}
}
zos.flush();
return conn.getResponseCode();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.