部分代码如下:
//触发事件
class rfListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ReceiveFile().start();
}
}
//窗口
public class ReceiveFile extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton save = new JButton("保存文件");
private DatagramPacket dp;
private DatagramSocket ds;
private FileOutputStream fos;
private FileInputStream fis;
private String filename;
private byte[] buf = new byte[10240];
public ReceiveFile(){
this.setSize(300,200);
this.setLocation(500, 300);
this.setTitle("接收文件");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(save);
save.addActionListener(this);
this.setVisible(true);
}
public void start(){
try {
ds = new DatagramSocket(4567);
fos = new FileOutputStream("d:\\tem.dat");
while(true){
dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
fos.write(dp.getData());
fos.flush();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
弹出窗口后应该是这样的:
但是结果是这样的: