JAVA 如何读取接受到的byte[]图片?

UDP协议,接受发送方发来的图片,并显示出来 ,我的程序显示不出图片.接受方: import java.awt.*; import java.awt.event.*; import java.net.DatagramPacket; import java.net.DatagramSocket; public class receive extends Frame implements ActionListener { Button bt; DatagramSocket socket; DatagramPacket pack; byte[] buf ; Image im; public receive() { bt = new Button("获取图像"); add(bt,"North"); this.setSize(800, 600); this.setVisible(true); try { buf = new byte[1024]; socket = new DatagramSocket(6112); pack = new DatagramPacket(buf,buf.length); socket.receive(pack); im = this.getToolkit().createImage(pack.getData()); } catch(Exception e) { e.printStackTrace(); } bt.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); } public void actionPerformed(ActionEvent e) { repaint(); } public void paint(Graphics g) { g.drawImage(im,0,0,null); } public static void main(String[] args) { new receive(); } } 发送方: import java.io.FileInputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class send { DatagramSocket socket; DatagramPacket pack; FileInputStream in; byte[] buf ; public send() { try { buf = new byte[1024]; in = new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\1.jpg"); in.read(buf); socket = new DatagramSocket(); pack = new DatagramPacket(buf,buf.length,InetAddress .getLocalHost(),6112); socket.send(pack); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new send(); } }
用文件的输入输告羡毁入派旁流袜备来把byte数组的图片转换成文件。

参考API的FileInputStream与FileOutputStream
同上