Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法通过套接字接收图像_Java_Sockets - Fatal编程技术网

Java 无法通过套接字接收图像

Java 无法通过套接字接收图像,java,sockets,Java,Sockets,我试图通过套接字发送一个图像,发送方部分(android)看起来很短很好,但接收方部分(由java编写)应该重命名图像并将其保存在C:/。但是我在那里什么也得不到,也找不到任何问题 这是我的服务器代码: public void start() throws InterruptedException { keepGoing = true; try { // the socket used by the server ServerSocket

我试图通过套接字发送一个图像,发送方部分(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();
        }
    }
public void start()引发InterruptedException{
持续=正确;
尝试
{
//服务器使用的套接字
ServerSocket ServerSocket=新的ServerSocket(端口);
//无限循环等待连接
同时(继续)
{
//格式化表示我们正在等待的消息
显示(“服务器在端口“+端口+”上等待客户端”);
Socket Socket=serverSocket.accept();//接受连接
//如果有人叫我停下来
如果(!继续)
打破
ClientThread t=newclientthread(socket);//创建一个线程
jobdone=false;
al.add(t);//保存在ArrayList中
t、 start();
}
//我被要求停下来
试一试{
serverSocket.close();
对于(int i=0;i=0)当前+=字节读;
}while(bytesRead>-1);
试一试{
写入(mybytearray2,0,当前);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
bos.flush();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
long end=System.currentTimeMillis();
//系统输出打印项次(结束-开始);
试一试{
bos.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

我做错了什么吗伙计们?谢谢你们提前阅读

文件路径应该是
C://
而不是
C://

尝试使用其他路径而不是直接使用“C:\”。这可能是权限问题。路径
C://
应该是
C://
谢谢大家,我看看它是否解决了