| 在eclipce项目下有个  images/images/2.gif 文件 , 先启动Service端, 跟着启动Client端发送图片,等十几秒后图片就会显示出来, 但是问题是,图片显示出来不是很完整, 有点小问题 ,希望大侠们看下咯 Service端
 
 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);
 }
 
 }
 
 
 Client 端:
 复制代码
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
  
public class Client {  
    private int port = 1220;  
    private String filePath = "";  
    private DatagramSocket socket;  
    public static File image = null;
    public Client() throws SocketException {  
        socket = new DatagramSocket();  
        //System.out.println("服务器启动成功");  
    }  
    public void service() throws IOException{  
             image = new File("images/2.gif");
        InputStream is = new FileInputStream(image);
        byte[] buffer = new byte[8192];  
        int len = 0;  
        while((len = is.read(buffer)) != -1){  
            System.out.println(len);  
            DatagramPacket packet = new DatagramPacket(buffer, len,InetAddress.getByName("localhost"),port);  
            socket.send(packet);  
        }  
        socket.close();  
    }  
    public static void main(String[] args) throws SocketException, IOException {  
        new Client().service();  
       
        
    }  
    
}  
 |