public boolean FileToByteArray(DatagramSocket socket, InetAddress dst_addr, int dst_port) throws Exception{
File file = new File("input.txt");
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int) length];
int offset = 0, n = 0;
DatagramPacket out_pkt = null;
while (offset < bytes.length && (n = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += n;
out_pkt = new DatagramPacket(bytes, pkt_size, dst_addr, dst_port);
packetCount++;
System.out.println("*****************" + packetCount);
System.out.println(out_pkt.getLength());
socket.send(out_pkt);
}
is.close();
String s = new String(bytes);
System.out.println(s + " " + s.length()) ;
return true;
}
大家好,我正在处理一项需要我通过UDP发送大文本文件的作业。 我能够读取10000字节但是当我将pkt_size设置为1000时,它只传送1000字节,这是1KB的数据。如何将大文件分解为小包发送?