基本上我要做的是在任何机器上运行一些服务器代码,并能够访问和下载该机器上的所有文件和文件夹。我已将我的代码附加在底部,但这是我拥有的,我所知道的以及我被困在哪里:
编辑: 我被告知我的问题太抽象而且不清楚。我已经弄清楚如何将我的文件列入清单。现在我需要在'payLoad'字符串中列出它们。
我知道我需要做一些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.");
}
}
}
}
旁注。出于某种原因,人们忘记了帮助这个词的定义,只是变得非常粗鲁。请。我知道有很多我不知道的,这就是我想要学习的原因。我被卡住了。正确的方向会有所帮助。
答案 0 :(得分:2)
如果你想在这里学习是一个提示。不要将完整的逻辑实现为单个方法。最好将一个大方法拆分为几个方法,如
private void sendHttpHeaders(PrintWriter out, String payload, String dateString) {
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");
}
这使代码更具可读性。提示:内联注释是创建新方法的良好指示。
问候, 马丁