Файлы не передаются на сервер - PullRequest
0 голосов
/ 17 мая 2019

Я хочу разработать свой собственный API для передачи файлов с клиента на сервер и обратно, но он не работает.

(Извините за мой английский, я скоро исправлю английский в этом Кодексе.)

Я искал в Интернете решения, но ничего не нашел.

// Upload File function
private void upload (String username, String password, String filePath)
    {
        Path path = Paths.get(filePath);
        if (Files.exists(path))
        {
            try
            {
                String fileName = Files.getFileStore(path).name();
                String fileType = Files.getFileStore(path).type();
                byte[] fileBytes = Files.readAllBytes(path);

                StringUtils.sendInformation("a");

                new NetworkClient(ConnectingConfig.IP, ConnectingConfig.PORT, "upload-content : " + username + " : " + password + " : " + fileName + " : " + fileType, fileBytes);
            }
            catch (Exception exception)
            {
                StringUtils.sendInformation("Error!");
                exception.printStackTrace();
            }
        }
        else
        {
            JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "File not found!");
        }
    }

// NetworkClient function

public NetworkClient (String ip, int port, String request, byte[] fileBytes)
    {
        try
        {
            Socket socket = new Socket(ip, port);
            StringUtils.sendInformation("Connected to " + ip + ":" + port + "!");

            byte[] buffer = new byte[fileBytes.length];

            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());

            StringUtils.sendInformation("abc");

            Integer integer = Integer.reverseBytes(buffer.length);

            while (integer == 0)
            {
                StringUtils.sendInformation("integer 0");
                objectOutputStream.write(fileBytes, 0, fileBytes.length);
                integer --;
            }

            boolean state = true;

            while (state)
            {
                StringUtils.sendInformation("state");
                dataOutputStream.writeUTF(request);
                state = false;
            }

            InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String input = bufferedReader.readLine();
            bufferedReader.close();

            if (!input.isEmpty())
            {
                switch (input)
                {
                    case "Invalid Credentials!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Invalid Credentials!");
                        FrameLoginMenu.visibleState = true;
                        break;
                    case "Banned!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You are banned on this Server!");
                        break;
                    case "Invalid Rank!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You have a Invalid Rank!\nPlease contact a Admin!");
                        FrameLoginMenu.visibleState = true;
                        break;
                    case "You have already a open application request!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You have already a open application request!");
                        break;
                    case "Application successfully sent!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Application successfully sent!");
                        break;
                    case "Uploaded-Wait-For-Verify!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Uploaded success!\nYou must be wait that a Admin verify your Application!");
                        break;
                    case "Uploaded!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Uploaded success!");
                        break;
                    case "Failed!":
                        JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Upload failed!");
                        break;
                }
            }

            socket.close();
            dataInputStream.close();
            dataOutputStream.close();

        }
        catch (Exception exception)
        {
            JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Can not connecting to the Server!");
            StringUtils.sendInformation("Error!");
            exception.printStackTrace();
        }
    }

Я не получил ошибку при загрузке файла, который моя Программа замерзла, и ничего не произошло. С моими тестами "а" и этим веществом я обнаружил, что программа зависла на "целое число 0"

Надеюсь, вы поможете мне, ребята

Спасибо за чтение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...