import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Server {
private int port = 1220;
private DatagramSocket socket;
public static File newfile = null;
public Server() throws SocketException{
socket = new DatagramSocket(port);
socket.setSoTimeout(10000);
}
public void reciveData() throws FileNotFoundException{
newfile = new File("liangliang.gif");
byte[] buf = new byte[8192];
FileOutputStream fos = new FileOutputStream(newfile);
while(true){
DatagramPacket packet = new DatagramPacket(buf, 8192);
try{
socket.receive(packet);
fos.write(packet.getData(), 0, packet.getLength());
}catch(Exception e){
try {
System.out.println("传输结束");
socket.close();
fos.flush();
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
}
}
}
public static void main(String[] args) throws FileNotFoundException, SocketException {
new Server().reciveData();
JFrame jf = new JFrame();
JPanel jp = new JPanel();
Image fd = new ImageIcon(newfile.getAbsolutePath()).getImage();
JLabel jl = new JLabel(new ImageIcon(newfile.getAbsolutePath()));
jf.add(jl);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400 , 300);
jf.setVisible(true);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |