当前位置: 代码迷 >> J2SE >> 小弟我用java写了一个网络接收图片的程序,但为什么图片总是显示为空的?各位大侠帮小弟看看吧
  详细解决方案

小弟我用java写了一个网络接收图片的程序,但为什么图片总是显示为空的?各位大侠帮小弟看看吧

热度:19   发布时间:2016-04-24 00:35:15.0
我用java写了一个网络接收图片的程序,但为什么图片总是显示为空的?各位大侠帮小弟看看吧
Java code
public class jieshou {    /**     * @param args     * @throws SocketException      * @throws IOException      */    public static void main(String[] args) throws SocketException, Exception {        // TODO Auto-generated method stub         DatagramSocket socket;            socket = new DatagramSocket(10000);         byte[] buf = new byte[108421];               OutputStream fos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jieshou.jpg"));         while(true)         {           DatagramPacket packet = new DatagramPacket(buf,108421);                       try{                  socket.receive(packet);               fos.write(packet.getData(), 0, packet.getLength());                }           catch (Exception e1)                {               try{               socket.close();                  fos.flush();                  fos.close();             }catch(Exception iee){System.out.println("close :"+e1);}            }              break;                      }           System.out.println("server_finish");         }    }


------解决方案--------------------
Java code
package com.ssj.test;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.OutputStream;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class Test {    public static void main(String[] args) throws SocketException, Exception {        // TODO Auto-generated method stub        DatagramSocket socket;        socket = new DatagramSocket(10000);        byte[] buf = new byte[108421];        OutputStream fos = new BufferedOutputStream(new FileOutputStream(                "f:/temp/jieshou.jpg"));        while (true) {            DatagramPacket packet = new DatagramPacket(buf, 108421);            try {                socket.receive(packet);                fos.write(packet.getData(), 0, packet.getLength());            } catch (Exception e1) {                try {                    socket.close();                    fos.flush();                    fos.close();                } catch (Exception iee) {                    System.out.println("close :" + e1);                }            }            break;        }        System.out.println("server_finish");    }}
------解决方案--------------------
这个是网络和机器决定的ip包最大包长 
tcp/ip协议为64k 你一下传200K还不挂?
>64K要分包的嘛,我了个去
  相关解决方案