简体   繁体   English

Java:使用套接字从客户端向服务器发送多个图像文件

[英]Java: send more than one image file from client to server using socket

I want to send more than one image file from client to server for this I write the code in my application, but it will send only one image.为此,我想从客户端向服务器发送多个图像文件,我在我的应用程序中编写了代码,但它只会发送一个图像。 In client application one frame is there and in server application also there is a frame to start/stop the server.在客户端应用程序中有一个框架,在服务器应用程序中也有一个框架来启动/停止服务器。

One more problem is there when Client application send the image file then this image file shown on server computer but when I try to open this image file then nothing is there but when I close server application(server frame) then I am able to see the image.还有一个问题是,当客户端应用程序发送图像文件时,该图像文件显示在服务器计算机上,但是当我尝试打开该图像文件时,什么也没有,但是当我关闭服务器应用程序(服务器框架)时,我能够看到图片。

code:代码:

client site:客户网站:

public void sendPhotoToServer(String str){ // str is image location
    try {
        InputStream input = new FileInputStream(str);
        byte[] buffer=new byte[1024];
        int readData;
        while((readData=input.read(buffer))!=-1){
        dos.write(buffer,0,readData); // dos is DataOutputStream
        }
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }       
}

In server side this code is running into thread:在服务器端,这段代码运行到线程中:

public void run() {
while (true) {
            try {
                byte[] buffer = new byte[8192];
                fis = new FileOutputStream("C:\\"+(s1++)+".jpg"); // fis is FileOutputStream
                while ((count = in.read(buffer)) > 0){ //count is a integer and 'in' is InputStream
                fis.write(buffer, 0, count); 
                fis.flush();    
                }
                } catch (Exception e) {}
}
}

Problem:问题:

  1. only 1st image is copying which is send by the client.只有第一张图片正在复制,由客户发送。
  2. I am able to see this Image only when I close the server application.只有当我关闭服务器应用程序时,我才能看到这个图像。

no exception is there and i call sendPhotoToServer method in other class consecutively to send all the image file as:也不例外,我连续调用其他 class 中的sendPhotoToServer方法将所有图像文件发送为:

if (photoSourcePath != null) {
                            clientClass.sendPhotoToServer(photoSourcePath+"\\"+rowData.get(5));
                        }

Your server side should stop the thread when its job is done.你的服务器端应该在它的工作完成后停止线程。 The while loop just keeps running forever and keeps the stream open (that's why you see the image when you shut down the server, the threads only stops then). while循环一直持续运行并保持 stream 处于打开状态(这就是为什么您在关闭服务器时会看到图像,线程只会在那时停止)。

Try changing the server side to this:尝试将服务器端更改为此:

public void run() {
    boolean processing = true;
    while (processing) {
        try {
            byte[] buffer = new byte[8192];
            fis = new FileOutputStream("C:\\" + (s1++) + ".jpg"); // fis is
                                                                  // FileOutputStream
            while ((count = in.read(buffer)) > 0) { // count is a integer
                                                    // and 'in' is
                                                    // InputStream
                fis.write(buffer, 0, count);
                fis.flush();

            }
            processing = false;
        } catch (Exception e) {
            processing = false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM