无法通过套接字接收图像

时间:2014-05-30 11:49:23

标签: java sockets

我试着通过套接字发送图像,发送者部分 - (android) - 看起来简短而且没问题,但接收器部分 - 由java编写 - 应该重命名图像并将其保存在C:/ ......但我什么都没得到,我无法发现任何问题..

这是我的服务器代码:

public void start() throws InterruptedException {
    keepGoing = true;
    try 
    {

       // the socket used by the server
       ServerSocket serverSocket = new ServerSocket(port);

       // infinite loop to wait for connections
       while(keepGoing) 
       {
           // format message saying we are waiting
           display("Server waiting for Clients on port " + port + ".");

           Socket socket = serverSocket.accept(); // accept connection
           // if I was asked to stop

           if(!keepGoing)
            break;
           ClientThread t = new ClientThread(socket); // make a hread of it
           jobdone=false;
           al.add(t); // save it in the ArrayList
           t.start();
        }
        // I was asked to stop
        try {
            serverSocket.close();
            for(int i = 0; i < al.size(); ++i) {
                ClientThread tc = al.get(i);
                try {
                tc.sInput.close();
                tc.sOutput.close();
                tc.socket.close();
                }
                catch(IOException ioE) {
                    // not much I can do
                }
            }
        }
        catch(Exception e) {
            display("Exception closing the server and clients: " + e);
        }
    }
    // something went bad
    catch (IOException e) {
                 String msg = sdf.format(new Date()) + 
                    " Exception on new ServerSocket: " + e + 
                    "\n";
         display(msg);
    }
}       
/*
 * For the GUI to stop the server
 */
protected void stop() {
    keepGoing = false;
    // connect to myself as Client to exit statement 
    // Socket socket = serverSocket.accept();
    try {
        new Socket("192.168.1.2", 1500);
    }
    catch(Exception e) {
        // nothing I can really do
    }
}
/*
 * Display an event (not a message) to the console or the GUI
 */
private void display(String msg) {
    String time = sdf.format(new Date()) + " " + msg;
    if(sg == null)
        System.out.println(time);
    else
        sg.appendEvent(time + "\n");
}

    // create a server object and start it



 public static void shutdown() {
jobdone = true;


 }
/** One instance of this thread will run for each client */

   class ClientThread extends Thread {
    // the socket where to listen/talk
    String Type;
    Socket socket;
    InputStream sInput;
    ObjectOutputStream sOutput;
    // my unique id (easier for deconnection)
    int id;



    // Constructore
    ClientThread(Socket socket) throws InterruptedException {
       // a unique id
       id = ++uniqueId;
       this.socket = socket;
       /* Creating both Data Stream */
       System.out.println("Thread trying to create Object I/O Streams");

       // create output first
       int bytesRead = 0;
               int current = 0;
               int filesize=65383; 
               byte [] mybytearray2  = new byte [filesize];
               InputStream is = null;
        try {
            is = socket.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("C:/IMG-20130112-WA0011.jpeg");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // destination path and name of file
        //FileOutputStream fos = new FileOutputStream("C:/");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        try {
            bytesRead = is.read(mybytearray2,0,mybytearray2.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        current = bytesRead;


        do {
           try {
       bytesRead =
       is.read(mybytearray2, current, (mybytearray2.length-current));
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
            if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
        try {
            bos.write(mybytearray2, 0 , current);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            bos.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        //System.out.println(end-start);
        try {
            bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
我做错了什么事吗?感谢提前阅读

1 个答案:

答案 0 :(得分:1)

文件路径应为C://,而不是C:/