Java实现的网络文件传送(二)
");
}
// 数据目的地是另一台主机,通过Socket获取I/O流
if (in != null) {
Socket s = null;
try {
s = new Socket("127.0.0.1", 10001);//这里写好目标机器IP地址和任意一个开放的未被其它程序占用的端口号
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.writeUTF(fileName);
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
os.write(b, 0, len);
}
//System.out.println("message");
DataInputStream fis = new DataInputStream(s.getInputStream());
String message = fis.readUTF();
System.out.println(message);
if (os != null)
os.close();
if (in != null)
in.close();
} catch (Exception e) {
System.out.println("貌似zpc不在线,稍后再传!!");
}
} else {
System.out.println("文件读取失败(in = null)!");
}
}
}