我的HTTP协议设计是否在编码中正确实现?

时间:2015-05-15 11:24:29

标签: java http file-io http-headers client-server

这是我非常简单的客户端 - 服务器应用程序。客户端向服务器发送一些命令,服务器将输出返回给客户端。但是,我特别关注的是发送到服务器的GET命令。客户端请求GET filename下载命名文件。该文件最终会通过HTTP响应标头下载到客户端目录中,因为我已经设计了我的协议。

现在我担心我的编码是否准确地遵循了协议。特别是带有换行符的HTTP响应头(在客户端和服务器端)。

协议设计:

客户端:

syntax: GET  namedfile CRLF
CRLF
meaning: downloading the named file from the server
representation: text file

服务器:

syntax: Status: ok CRLF
Length: 20 bytes CRLF
CRLF
File contents
meaning: The file exist in the server and ready to 
download
representation: text file

CODE:

服务器端:

                          .................
                          .................
else if (request.startsWith("GET")) {
                        System.out.println("");
                        String filename = request.substring(4);
                        File file = new File(System.getProperty("user.dir"));
                        File[] files = file.listFiles();

                        if (fileExists(files, filename)) {
                            file = new File(filename);
                            int fileSize = (int) file.length();
                            outputToClient.print("Status OK\r\n"
                                    + "Size " + fileSize + "KB" + "\r\n"
                                    + "\r\n"
                                    + "File " + filename + " Download was successfully\r\n");
                            outputToClient.flush();
                            // reading files
                            fis = new FileInputStream(file);
                            os = socket.getOutputStream();
                            byte[] buffer = new byte[2^7-1];
                            int bytesRead = 0;
                            while ((bytesRead = fis.read(buffer))!= -1) {
                                os.write(buffer, 0, bytesRead);
                            }
                            os.close();
                            fis.close();
                        } else {
                            outputToClient.print("Status 400\r\n"
                                    + "File " + filename + " not found\r\n"
                                    + "\r\n");
                            outputToClient.flush();
                        }
                    }
                    outputToClient.flush();
                }
                           .................
                           .................

客户机侧:

       ............
                 ............
                if (request.startsWith("GET")) {
                File file = new File(request.substring(4));
                is = socket.getInputStream();
                fos = new FileOutputStream(file);

                byte[] buffer = new byte[socket.getReceiveBufferSize()];
                int bytesReceived = 0;

                while ((bytesReceived = is.read(buffer)) >=0) {
                    //while ((bytesReceived = is.read(buffer))>=buffer) {
                    fos.write(buffer, 0, bytesReceived);
                }
                request = "";
                fos.close();
                is.close();
            }
               .................
               .................

1 个答案:

答案 0 :(得分:0)

我不清楚你在问什么,但正如约翰所指出的那样 - 为什么不采用像jetty服务器这样的现有解决方案并根据自己的口味进行调整? 要给出答案:例如,缺少HTTP状态代码。 直接" map"到服务器文件系统不是最佳做法。