简体   繁体   中英

I'm trying to make a Java File Server

Basically what I'm trying to do is have a some server code running on any machine and being able to access and download all of the files and folders on that machine. I've attached my code at the bottom but here's what I have, what I know, and where I'm stuck:

EDIT: I've been told my question is too abstract and not clear. I've figured out how to get my files in list. Now I need to list them inside the 'payLoad' string.

  • I know I've hard coded the file browser. It just helped me get started visually. It says 'some files' and 'some folders' because that is where I want them to go.
  • I've got the files loaded into an Array because they're dynamic. Now I'm to get the array list to load into the HTML table.
  • I know there are http and URL things I have to do, like GET and POST to actually download the files but I still need help to get started

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

Side note. For some reason, people forget the definition of the word help and just turn very rude. Please. I know there's a lot I don't know, that's why I want to learn. I'm stuck. The right direction would be helpful.

if you want to learn here is a tip. Don't implement the complete logic into a single method. It is better to split one large method into several methods like

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");
}

This makes code more readable. Hint: inline comments are a good indication for creating a new method.

Greetings, Martin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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