简体   繁体   English

从服务器(在Java中)读取/下载文件的准确方法是什么?

[英]What is the accurate way to read/download a file from the server (in Java)?

In my client-server application I have used a command ( GET filename )to download a file into the client side. 在我的客户端服务器应用程序中,我使用了命令( GET filename )将文件下载到客户端。 I have used the build in read() method to read the file. 我已使用read()方法中的内置方法来读取文件。 My teachers said it's not a very good practice to implement this read method. 我的老师说,实施这种read方法不是一个很好的做法。 The reason is either it doesn't tell how exactly the file is reading from the server or it somehow is not able to download dynamic (large) file size. 原因是它无法说明从服务器读取文件的确切方式,还是无法下载动态(大)文件大小。 But at the moment I see that it's working fine. 但是目前,我看到它运行良好。 Since I am still in intermediate level in java, I need to learn the best way to do this job. 由于我仍处于Java的中级水平,因此我需要学习完成这项工作的最佳方法。 How it could be improved in coding? 如何在编码中进行改进? That is I want to improve the while looping part in ClientSide. 那就是我想改善ClientSide中的while looping部分。

I have pasted the relevent code: 我粘贴了相关事件代码:

ClientSide: 客户端:

                         ............
                         ............
                        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();
                    }
                       .................
                       .................

ServerSide: 服务器端:

                          .................
                          .................
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();
                }
                           .................
                           .................

You need to consume the rest of the HTTP response headers, by reading until you get a blank line, if you haven't already done that. 您需要消耗其余的HTTP响应标头,方法是读取直到获得空白行(如果尚未完成)。

Apart from that, your code looks fine to me, except that I would use a much bigger buffer than 127, at least 8192, possibly a multiple of that. 除此之外,您的代码对我来说看起来还不错,除了我将使用比127大得多的缓冲区(至少8192,可能是该倍数)。

Ask your teacher what (on earth) he's talking about. 问你的老师在说什么(到底)。

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

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