【发布时间】:2016-11-19 09:57:19
【问题描述】:
我在计算机上有服务器,它是客户端(Android 应用程序)。客户端向服务器发送一个大文件(2934822 字节)。这是客户端的代码:
this.socket_out = this.socket.getOutputStream();
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
FileInputStream mFileInputStream = new FileInputStream(mFile);
while (true) {
byte[] i1 = new byte[65536];
int i2 = mFileInputStream.read(i1, 0, 65536);
Log.v("", "read=" + i2);
if (i2 < 0) {
mByteArrayOutputStream.close();
mFileInputStream.close();
break;
} else {
mByteArrayOutputStream.write(i1, 0, i2);
mByteArrayOutputStream.flush();
}
}
mFile.delete();
byte[] i1 = mByteArrayOutputStream.toByteArray();
Log.v("", "sent=" + i1.length);
this.socket_out.write(i1);
this.socket_out.flush();
还有日志:
读取=65536
读取=65536
...
读取=65536
读取=51238
发送=2934822
这是服务器代码:
this.in = new DataInputStream(this.socket.getInputStream());
while (
byte[] i1 = new byte[65536];
int i2 = this.in.read(i1, 0, 65536);
if (i2 > -1) {
System.out.print(i2);
...
} else {
break;
}
}
标准输出:
12974 1440 1440 11520 1440 1440 1440 7200 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 11520 1440 1440 59040 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 1440 17280 ...
为什么DataInputStream 没有完全读取 65536 字节?我怎样才能让它读取字节数组b 我在DataInputStream.read(byte[] b, int off, int len) 中的参数len 中设置的最大计数?谢谢。
【问题讨论】:
-
可能是因为流来自网络,网络数据传输速度慢
-
您的标题与您的问题无关。
read(buffer, offset, len)的结果不可能大于len。 -
@EJP,我的英语不太好,抱歉。如果可以,将其更改为更正方差。
-
放下它。您肯定知道足够的英语来知道“大于”和“小于”之间的区别吗?
标签: java android sockets stream