【发布时间】:2016-07-12 15:40:19
【问题描述】:
基本上我想要做的是在任何机器上运行一些服务器代码,并且能够访问和下载该机器上的所有文件和文件夹。我已将我的代码附在底部,但这是我所拥有的、我所知道的以及我遇到的问题:
编辑: 有人告诉我我的问题太抽象而且不清楚。我已经弄清楚如何将我的文件放入列表中。现在我需要在“payLoad”字符串中列出它们。
- 我知道我已经对文件浏览器进行了硬编码。它只是帮助我从视觉上开始。它说“一些文件”和“一些文件夹”,因为那是我希望它们去的地方。
- 我已将文件加载到数组中,因为它们是动态的。现在我要让数组列表加载到 HTML 表中。
-
我知道我必须做一些 http 和 URL 的事情,例如 GET 和 POST 才能实际下载文件,但我仍然需要帮助才能开始
import java.net.*; import java.io.*; import java.util.Date; import java.awt.Desktop; import java.net.URI; class Main { public static void main(String[] args) throws Exception { File folder = new File("/Users/DeAndre"); File[] listOfFiles = folder.listFiles(); int c = 0; // Listen for a connection from a client ServerSocket serverSocket = new ServerSocket(1234); if (Desktop.isDesktopSupported()) Desktop.getDesktop().browse(new URI("http://localhost:1234")); else System.out.println("Please direct your browser to http://localhost:1234."); while(true) { Socket clientSocket = serverSocket.accept(); System.out.println("Got a connection!"); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String dateString = (new Date()).toGMTString(); while(c<listOfFiles.length) { String payload = "\t\t<table>\n" + "\t\t<tr><td align=right>Current directory:</td><td>/some/path/</td></tr>\n" + "\t\t<tr><td>\n" + "\t\t<b>Folders:</b><br>\n" + "\t\t<select id=\"folderList\" size=\"15\" style=\"width: 280px\" onchange=\"javascript:location.href=this.value;\">\n" + "\t\t\t<option value=\"index.html?cd=..\">..</option>\n" + "\t\t\t<option value=\"index.html?cd=somefolder\">somefolder</option>\n" + "\t\t\t<option value=\"index.html?cd=anotherfolder\">anotherfolder</option>\n" + "\t\t\t<option value=\"index.html?cd=yetanotherone\">yetanotherfolder</option>\n" + "\t\t</select>\n" + "\t\t</td><td>\n" + "\t\t<b>Files:</b><br>\n" + "\t\t<select id=\"fileList\" size=\"15\" style=\"width: 280px\">\n" + "\t\t\t<option value=\"somefile.txt\">somefile.txt</option>\n" + "\t\t\t<option value=\"somefile.txt\">" + listOfFiles[20].getName() + "</option>\n" + "\t\t\t<option value=\"anotherfile.jpeg\">anotherfile.jpeg</option>\n" + "\t\t</select>\n" + "\t\t</td></tr></table>" + listOfFiles[1]; c++; for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println("Directory " + listOfFiles[i].getName()); } } // Receive the request from the client String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("The client said: " + inputLine); if (inputLine.length() < 2) break; } //File Browser // Send HTTP headers System.out.println("Sending a response..."); out.print("HTTP/1.1 200 OK\r\n"); out.print("Content-Type: text/html\r\n"); out.print("Content-Length: " + Integer.toString(payload.length()) + "\r\n"); out.print("Date: " + dateString + "\r\n"); out.print("Last-Modified: " + dateString + "\r\n"); out.print("Connection: close\r\n"); out.print("\r\n"); // Send the payload out.println(payload); System.out.println("Done."); } } } }
旁注。出于某种原因,人们忘记了帮助这个词的定义,变得非常粗鲁。请。我知道有很多我不知道,这就是我想学习的原因。我被困住了。正确的方向会有所帮助。
【问题讨论】:
-
要获得具体答案,您需要提出具体问题。如果您想要代码审查,还有另一个站点。这个论坛是为明确的编程问题提供明确的答案,这些问题不是基于意见的。这个论坛并不试图为所有人提供一切。
-
@PeterLawrey StackOverflow is not a forum
-
@cricket_007 好点。
-
@PeterLawrey 里面有一个特定的问题。 “基本上我想做的是在任何机器上运行一些服务器代码,并能够访问和下载该机器上的所有文件和文件夹”这是我的目标。我的问题如何将 File ArrayList 中存储的字符串名称放入 HTML 列表中
-
您有一个用于打印文件名的 for 循环。将其更改为附加到字符串。
标签: java html file http server