A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范贞亮 中级黑马   /  2012-10-26 14:09  /  4240 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在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 端:


  1. import java.awt.Image;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.DatagramPacket;
  7. import java.net.DatagramSocket;
  8. import java.net.InetAddress;
  9. import java.net.SocketException;

  10. import javax.swing.ImageIcon;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14.   
  15. public class Client {  
  16.     private int port = 1220;  
  17.     private String filePath = "";  
  18.     private DatagramSocket socket;  
  19.     public static File image = null;
  20.     public Client() throws SocketException {  
  21.         socket = new DatagramSocket();  
  22.         //System.out.println("服务器启动成功");  
  23.     }  
  24.     public void service() throws IOException{  
  25.              image = new File("images/2.gif");
  26.         InputStream is = new FileInputStream(image);
  27.         byte[] buffer = new byte[8192];  
  28.         int len = 0;  
  29.         while((len = is.read(buffer)) != -1){  
  30.             System.out.println(len);  
  31.             DatagramPacket packet = new DatagramPacket(buffer, len,InetAddress.getByName("localhost"),port);  
  32.             socket.send(packet);  
  33.         }  
  34.         socket.close();  
  35.     }  
  36.     public static void main(String[] args) throws SocketException, IOException {  
  37.         new Client().service();  
  38.       
  39.         
  40.     }  
  41.    
  42. }  
复制代码

评分

参与人数 1技术分 +1 收起 理由
韩军博 + 1

查看全部评分

3 个回复

倒序浏览
楼主解决了没有? 我运行了下你的代码,好像数据没有发出去,发送端那个循环没转起来。
回复 使用道具 举报
还没有啊 , 我这边能收到图片 , 就是图片收到不是很完整, 你确定的eclipce项目文件夹下 有 images/2.gif这个文件
回复 使用道具 举报
范贞亮 发表于 2012-10-26 15:01
还没有啊 , 我这边能收到图片 , 就是图片收到不是很完整, 你确定的eclipce项目文件夹下 有 images/2.gif ...

有啊,我指定的文件在d盘,测试了下,还是没读到数据,蛋疼了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马