You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地可删除文件但JetBrains Academy测试失败的问题排查

File Server项目删除功能JetBrains测试失败问题

项目描述

  • 获取文件内容
  • 创建文件
  • 删除文件

服务端持续运行直到收到字符串形式的"exit"命令,客户端仅运行一次,完成获取、创建或删除操作后即终止。

问题

删除功能在JetBrains测试中显示文件未找到,完整测试输出如下:

Wrong answer in test #1

You should delete a file from /server/data folder if the user requests it!

Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.


Server started!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > exit
The request was sent.

Server started!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 2
Enter filename: > test_purpose_N6O0XTLO.txt
Enter file content: > IDIDW22
The request was sent.
The response says that the file was created!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 2
Enter filename: > test_purpose_N6O0XTLO.txt
Enter file content: > IDIDW22
The request was sent.
The response says that creating the file was forbidden!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 1
Enter filename: > test_purpose_N6O0XTLO.txt
The request was sent.
The content of the file is: IDIDW22

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 1
Enter filename: > test_purpose_2JK4NCL.txt
The request was sent.
The response says that the file was not found!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 3
Enter filename: > test_purpose_RB3JXD.txt
The request was sent.
The response says that the file was not found!

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): > 3
Enter filename: > test_purpose_N6O0XTLO.txt
The request was sent.
The response says that the file was not found!

本地运行结果

完全复现上述测试场景后,本地机器运行正常,删除功能输出如下:

Enter action (1 - get a file, 2 - create a file, 3 - delete a file): 3
Enter filename: test_purpose_N6O0XTLO.txt
The request was sent.
The response says that the file was successfully deleted!

排查情况

曾怀疑是权限问题,换个人笔记本测试结果相同,但创建文件功能正常,排除权限原因。目前怀疑问题和JetBrains的测试流程有关。

代码片段

服务端代码

// Server
public class Main {
    static final String path = "C:\\Users\\xxx\\xxx\\Repositories\\Hyperskill\\File Server\\File Server\\task\\src\\server\\data\\";

    public static void main(String[] args) throws InterruptedException {

        createDirectory(path);

        String address = "127.0.0.1";
        int port = 23456;

        boolean endServer = false;

        try (ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(address))) {
            System.out.println("\nServer started!");

            do {
                Socket socket = server.accept();
                DataInputStream input = new DataInputStream(socket.getInputStream());
                DataOutputStream output = new DataOutputStream(socket.getOutputStream());

                String message = input.readUTF();

                if (message.equals("exit")) {
                    endServer = true;
                } else {
                    String[] words = message.split(" ", 3);
                    boolean isSuccessfulAction = false;
                    HTTP_COMMANDS c = HTTP_COMMANDS.valueOf(words[0]);
                    switch (c) {
                        case PUT:
                            isSuccessfulAction = createFile(path, words[1], words[2]);
                            break;
                        case GET:
                            isSuccessfulAction = getFile(path, words[1]);
                            break;
                        case DELETE:
                            isSuccessfulAction = deleteFile(path, words[1]);
                    }
                    output.writeUTF(String.valueOf(returnHTTPCode(HTTP_COMMANDS.valueOf(words[0]), isSuccessfulAction)));
                }
            } while (!endServer);
        } catch (IOException e) {
            System.out.println("Could not start sever!");
        }
    }

    public static boolean deleteFile(String path, String fileName) {
        File file = new File(path + fileName);
        return file.delete();
    }

    public static boolean getFile(String path, String fileName) {
        File file = new File(path + fileName);
        return file.exists();
    }

    public static boolean createFile(String path, String fileName, String fileContent) {
        try {
            if (new File(path, fileName).exists()) {
                return false;
            } else {
                File newFile = new File(path + fileName);
                FileWriter writer = new FileWriter(newFile);
                writer.write(fileContent);
                writer.close();
                return true;
            }
        } catch (IOException e) {
            return false;
        }
    }

    public static int returnHTTPCode(HTTP_COMMANDS request, boolean actionSuccessful) {
        if (actionSuccessful) {
            return HTTP_CODES.SUCCESS.getCode();
        } else if (request.equals(HTTP_COMMANDS.PUT)) {
            return HTTP_CODES.FAIL_PUT.getCode();
        } else if (request.equals(HTTP_COMMANDS.GET)) {
            return HTTP_CODES.FAIL_GET.getCode();
        } else {
            return HTTP_CODES.FAIL_DELETE.getCode();
        }
    }

    public static void createDirectory(String path) {
        File fileDirectory = new File(path);
        if (!fileDirectory.exists()) {
            fileDirectory.mkdirs();
        }
    }
}

客户端代码

// Client
public class Client {
    static final String path = "C:\\Users\\xxx\\xxx\\Repositories\\Hyperskill\\File Server\\File Server\\task\\src\\server\\data\\";
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws InterruptedException {

        // Sleep used because it seems JetBrains is executing the client main first instead of the server main.
        // Delayed startup to ensure that server will be running first instead of the client
        TimeUnit.SECONDS.sleep(1);

        String address = "127.0.0.1";
        int port = 23456;

        String command;
        String fileName;

        try (Socket socket = new Socket(InetAddress.getByName(address), port)) {
            //boolean endServer = false;
            //do {
            DataInputStream input = new DataInputStream(socket.getInputStream());
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());

            System.out.print("\nEnter action (1 - get a file, 2 - create a file, 3 - delete a file): ");
            command = scanner.nextLine();

            if (HTTP_COMMANDS.forString(command).equals(HTTP_COMMANDS.EXIT)) {
                System.out.println("The request was sent.");
                output.writeUTF(command);
                //endServer = true;
            } else {
                System.out.print("Enter filename: ");
                fileName = scanner.nextLine();

                switch (HTTP_COMMANDS.forString(command)) {
                    case GET:
                        output.writeUTF(get(fileName));
                        break;
                    case PUT:
                        String message = add(fileName);
                        output.writeUTF(message);
                        break;
                    case DELETE:
                        output.writeUTF(delete(fileName));
                        break;
                }
                System.out.println("The request was sent.");
                String code = input.readUTF();
                printMessage(HTTP_COMMANDS.forString(command), code, fileName);
                input.close();
                output.close();
                socket.close();
            }
            //} while (!endServer);
        } catch (IOException e) {
            System.out.println("Could not connect to server!");
        }

    }

    public static void printMessage(HTTP_COMMANDS request, String code, String fileName) {
        int number = Integer.parseInt(code);
        if (number == HTTP_CODES.SUCCESS.getCode()) {
            if (request.equals(HTTP_COMMANDS.PUT)) {
                System.out.print("The response says that the file was created!");
            } else if (request.equals(HTTP_COMMANDS.GET)) {
                System.out.print("The content of the file is: ");
                printFromFile(fileName);
            } else if (request.equals(HTTP_COMMANDS.DELETE)) {
                System.out.print("The response says that the file was successfully deleted!");
            }
        } else {
            if (request.equals(HTTP_COMMANDS.PUT)) {
                System.out.print("The response says that creating the file was forbidden!");
            } else if (request.equals(HTTP_COMMANDS.DELETE) || request.equals(HTTP_COMMANDS.GET) ) {
                System.out.print("The response says that the file was not found!");
            }
        }
        System.out.print('\n');
    }

    public static void printFromFile(String fileName) {
        try {
            File file = new File(path + fileName);
            Scanner scannerGet = new Scanner(file);
            while (scannerGet.hasNextLine()) {
                System.out.print(scannerGet.nextLine() + " ");
            }
        } catch (FileNotFoundException e) {
            System.out.println(fileName + " not found!");
        }
    }

    public static String add(String fileName) {
        System.out.print("Enter file content: ");
        String fileContent = scanner.nextLine();
        return HTTP_COMMANDS.PUT + " " + fileName + " " + fileContent;
    }

    public static String get(String fileName) {
        return HTTP_COMMANDS.GET + " " + fileName;
    }

    public static String delete(String fileName) {
        return HTTP_COMMANDS.DELETE + " " + fileName;
    }

}

内容的提问来源于stack exchange,提问作者Oranges

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:02:24