管理将Android中的多个图像发送到PC Java套接字的线程

时间:2019-03-21 19:25:19

标签: java android serversocket

我已经阅读并尝试了很多Google教程,尤其是在Stackoverflow

现在我遇到错误,例如线程未成功出现错误,

Exception in thread "Thread-5" java.lang.IllegalArgumentException: Illegal Capacity: -1

这是我的代码

Android上的客户端套接字

public class Client extends AsyncTask<Void, Void, Void> {

    private String dstAddress;
    private int dstPort;
    private String response = "";
    private String textResponse;

    public Client(String addr, int port, String textResponse) {
        dstAddress = addr;
        dstPort = port;
        this.textResponse = textResponse;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Socket socket = null;
        try {
            // Create client socket
            socket = new Socket(dstAddress, dstPort);

            // Scanning given path file
            File file = new File(textResponse);
            File[] files = file.listFiles();

            OutputStream os = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);

            dos.writeInt(files.length);
            for (int count = 0; count < files.length; count++) {
                dos.writeUTF(files[count].getName());

            }
            for (int count = 0; count < files.length; count++) {
                int fileSize = (int) files[count].length();
                dos.writeInt(fileSize);
            }

            for (int count = 0; count < files.length; count++) {

                int fileSize = (int) files[count].length();
                byte[] buffer = new byte[fileSize];

                FileInputStream fis = new FileInputStream(files[count].toString());
                BufferedInputStream bis = new BufferedInputStream(fis);

                //Sending file name and file size to the server
                bis.read(buffer, 0, buffer.length); //This line is important
                dos.write(buffer, 0, buffer.length);
                dos.flush();
            }
            //close socket connection
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

PC上的服务器插槽

public class MainWindow extends javax.swing.JFrame {

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Thread starter = new Thread(new ServerStart());
        starter.start();
    }   

    public class ServerStart implements Runnable {

        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(59090);
                textAreaLog.append("Now Server Is Started.... \n");

                while (true) {
                    Socket clientSocket = null;
                    clientSocket = serverSocket.accept();

                    // create each thread
                    Thread listener = new Thread(new ClientHandler(clientSocket));
                    listener.start();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                textAreaLog.append("Error Making A connection \n" + ex.getMessage());
            }
        }
    }

    public class ClientHandler implements Runnable {

        Socket socket;
        int len;
        int smblen;
        int fileSize = 0;
        DataInputStream clientData;
        BufferedInputStream clientBuff;

        public ClientHandler(Socket clientSocket) {
            try {
                socket = clientSocket;
            } catch (Exception ex) {
                textAreaLog.append(ex.getMessage());
            }

        }

        @Override
        public void run() {

            try {

                clientData = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 

                while (true) {

                    fileSize = clientData.read();

                    //store list of filename from client directory
                    ArrayList<File> files = new ArrayList<>(fileSize); 

                    //store file size from client
                    ArrayList<Integer> sizes = new ArrayList<>(fileSize); 

                    //Start to accept those filename from server
                    for (int count = 0; count < fileSize; count++) {
                        File ff = new File(clientData.readUTF());
                        files.add(ff);
                    }

                    for (int count = 0; count < fileSize; count++) {
                        sizes.add(clientData.readInt());
                    }

                    for (int count = 0; count < fileSize; count++) {

                        // My prove
                        len = sizes.get(count);
                        System.out.println("File Size =" + len);


                        output = new FileOutputStream(textFieldDirectory.getText() + File.separator + files.get(count));
                        dos = new DataOutputStream(output);
                        bos = new BufferedOutputStream(output);

                        byte[] buffer = new byte[4092];

                        bos.write(buffer, 0, buffer.length); //This line is important

                        while (len > 0 && (smblen = clientData.read(buffer)) > 0) {
                            dos.write(buffer, 0, smblen);
                            len = len - smblen;
                            dos.flush();
                        }
                    }
                }
            } catch (IOException ex) {
                textAreaLog.append(ex.getMessage());
                ex.printStackTrace();
            }

        }
    }

}

如您所见,服务器中的变量不好;

//store list of filename from client directory
ArrayList<File> files = new ArrayList<>(fileSize); 

//store file size from client
ArrayList<Integer> sizes = new ArrayList<>(fileSize);

有时代码可以正常工作,但有时不能从android上传并保存到PC的目录中。 请帮忙。

0 个答案:

没有答案