日期:2014-05-20 浏览次数:20786 次
DatagramSocket serverSocket = new DatagramSocket(8888);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
//接收文件信息,包括文件名,文件长度。
serverSocket.receive(receivePacket);
String fileName = new String(receivePacket.getData()).trim();
System.out.println("FileInfor: " + fileName);
fileName = fileName.split("\\.")[0] + "New." + fileName.split("\\.")[1];
System.out.println("NewFileName" + fileName);
FileWriter fileWriter = new FileWriter(fileName);
while(true){
receivePacket.setData(receiveData);
serverSocket.receive(receivePacket);
String filecon = new String(receivePacket.getData());
System.out.println("文件内容:" + filecon);
if(filecon.trim().equals("end")){
fileWriter.close();
break;
}
fileWriter.write(filecon);
fileWriter.flush();
}
serverSocket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}