java中图片文件的传输及显示(Socket以及ServerSocket演示)


//客户端部分
package testSix;

import java.awt.Graphics;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;


class MyPanel extends JPanel{
    private ImageIcon ii;
    public MyPanel(ImageIcon ii){
        this.ii = ii;
    }
    
    protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.drawImage(ii.getImage(), 0, 0, 300, 400, this);
      }
}

public class SocketDemo extends JFrame{
      private Socket st = null;
      public SocketDemo(){
          try{
              try{
                  //建立客户端
                  st =  new Socket(InetAddress.getLocalHost(), 10000);
                  
                  //返回套接字具有关联的通道的输出流,则得到的输出流会将其所有操作委托给通道
                  OutputStream os = st.getOutputStream();
                  byte[] bt = "哥们儿, 给我来一张背景图片!".getBytes();
                  os.write(bt);
                  
                  //返回套接字具有关联的通道输入流,则得到的输入流会将其所有操作委托给通道
                  InputStream is = st.getInputStream();
                  int len = is.read(bt);
                  System.out.println(new String(bt, 0, len));
                  
                  //将输入流按照下面方式处理, 根据Iterator<ImageReader> itImage是否能
                  //成功的返回一个ImageReader对象确认该流文件是否是一个图片文件!
                  //并ImageReader类中的getFormatName()得到文件的格式!
                  //通过最后可以通过ImageIcon的byte[]构造函数建立ImageIcon对象!
                  //最后将图片显示在面板上!
                  ImageInputStream iis = ImageIO.createImageInputStream(is);
                  Iterator<ImageReader> itImage = ImageIO.getImageReaders(iis);
                  if(itImage.hasNext()){
                      ImageReader reader = itImage.next();
                      byte[] imageByte = new byte[102400*10];
                      iis.read(imageByte);
                      ImageIcon ii = new ImageIcon(imageByte, reader.getFormatName());
                      add(new MyPanel(ii));
                  }
                  
              }finally{
                    st.close();
              }
          }catch(IOException e){
              e.printStackTrace();
          }
          setSize(300, 400);
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      
      public static void main(String[] args){
          new SocketDemo();
      }
}


//服务端部分
package testSix;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketDemo{
    private ServerSocket sst = null;
    private Socket st = null;
    public ServerSocketDemo(){
       try{
            try {
                    //建立服务端
                    sst = new ServerSocket(10000);
                    
                    //侦听并接受到此服务套接字的连接。此方法在进行连接之前一直阻塞。 创建新套接字 
                    st = sst.accept();
                    
                    //得到客户端传输过来的流
                    InputStream is = st.getInputStream();
                    byte[] bt = new byte[1024];
                    int len = is.read(bt);
                    System.out.println(new String(bt, 0, len));
                    OutputStream os  = st.getOutputStream();
                    os.write("好了,知道了!".getBytes());
                    
                    //将图片文件传输过去
                    FileInputStream fis = new FileInputStream(new File("q12.jpg"));
                    while((len = fis.read(bt)) != -1){//先将流文件变成byte[], 然后利用套接字的输出流发送给客户端
                         os.write(bt);
                         os.flush();
                    }
                
            }finally{
                    st.close();
                    sst.close();
            }
       }catch(IOException e){
           e.printStackTrace();
       }
        
    }
    
    public static void main(String[] args){
         new ServerSocketDemo();
    }
}

上一篇:JavaScript实现存储HTML字符串


下一篇:使用VisualSVNServer+花生壳实现远程协同开发版本控制