Java中如何用socket传送图象?

解决方案 »

  1.   

    http://topic.csdn.net/t/20060126/11/4535530.html 试试跟帖的人的方法
      

  2.   

    用流发送即可,例子如下
    客户端
    import java.io.*;
    import java.net.*;
    public class Client {
    public static void main(String[] args) {
    Socket s=null;
    ObjectOutputStream oos=null;
    try {
    s=new Socket("192.168.1.102",8888);
    oos=new ObjectOutputStream(s.getOutputStream());
    oos.writeObject(new File("f:\\asd.jpg"));
    oos.flush();
    } catch (Exception e) {
    e.printStackTrace();
    } finally{
    if(oos!=null)
    try {
    oos.close();
    } catch (IOException e1) {

    if(s!=null)try {s.close();} catch (IOException e) {}
    } }}服务器端
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;public class Server {
    public static void main(String[] args) {
    ServerSocket ss=null;
    Socket s=null;
    FileOutputStream fos=null;
    ObjectInputStream ois=null;
    FileInputStream fis=null;
    try {
    ss=new ServerSocket(8888);

    while (true) {
    s = ss.accept();
    ois = new ObjectInputStream(s.getInputStream());
    File a=(File)ois.readObject();
    fos = new FileOutputStream(new File("f:\\dsa.jpg"));
    fis=new FileInputStream(a);
    while(fis.available()>0){
    fos.write(fis.read());
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    if(fos!=null)try {fos.close();} catch (IOException e1) {}
    if(fis!=null)try{fis.close();}catch (IOException e) {}
    if(s!=null)try {s.close();} catch (IOException e) {}
    if(ss!=null)try {ss.close();} catch (IOException e) {}
    } }}
      

  3.   

    四楼正解,不光是图片。声音、视频等任何文件都可以通过SOCKET传送……
      

  4.   

    各位,用流传输不嫌慢啊。试试用数据报传输吧。
    http://topic.csdn.net/u/20110510/18/71c17d42-5796-46f7-aab3-955c132f4e92.html
      

  5.   

     
    想问下,如果服务器端停止运行,此时客户端的Socket已不能使用,该怎么处理。