我正在尝试通过套接字将数据从android手机传输到服务器。我读到关闭套接字的输入或输出流会关闭另一个流和套接字。我注释了给定的行。但是错误出现在日志中。
UPD 但是,如果我上传的文件最大为3 KB,则上传成功
错误应用程序:
java.net.SocketException: Socket is closed
错误服务器:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
@Override
public void run() {
try {
byte[] request = new byte[4096];
byte[] reply = new byte[8192];
final InputStream inFromClient = sClient.getInputStream();
final OutputStream outToClient = sClient.getOutputStream();
SSLSocket remoteSocket = tlsConnectionService.createSSLSocket(remoteHost, remotePort);
final InputStream inFromServer = remoteSocket.getInputStream();
final OutputStream outToServer = remoteSocket.getOutputStream();
// a new thread for uploading to the server
new Thread() {
public void run() {
int bytes_read;
try {
while ((bytes_read = inFromClient.read(request)) != -1) {
String newReq = new String(request);
outToServer.write(newReq.replace(LOCAL_SOCKET_URL, remoteHost).getBytes(), 0, bytes_read);
outToServer.flush();
}
} catch (IOException e) {
if (!(e instanceof SocketException)) {
Log.e(M.CPP, e.toString());
}
}
// try {
// outToServer.close();
// } catch (IOException e) {
// Log.e(M.CPP, e.toString());
// }
}
}.start();
// current thread manages streams from server to client (DOWNLOAD)
int bytes_read;
try {
while ((bytes_read = inFromServer.read(reply)) != -1) {
outToClient.write(reply, 0, bytes_read);
outToClient.flush();
}
} catch (IOException e) {
Log.e(M.CPP, e.toString());
} finally {
try {
remoteSocket.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
//outToClient.close();
sClient.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}