Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Writing a server using Sockets that lists all files in the current directory

HI,
I have currently a very basic startup code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class JokeServer {
    public static void main(String a[]) throws IOException {
        // Using try with resources as it closes everything autocloseable
        try (
                ServerSocket serverSocket = new ServerSocket(Constants.jokeServerPort, Constants.backLog);
                Socket socket = serverSocket.accept();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        ) {
            printWriter.println("Hello World");
//        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();

        }
    }

}


Open in new window

This server runs on localhost and port 4545
I opened the url http://localhost:4545/ in chrome browser
The server was waiting for the request and after i hit this url the server exited.
Which is understandable. But the Hello World didnt get printed on the chrome page.

What i actually want to achieve is when this url is hit. Then chrome displays all the files and folder in the directory where server was started (Kind of like an FTP server)
Not only that the user should be able to click on files which opens in chrome
or click on directories which list down the files inside it or may be something simpler...

Any help on the above. How do i go about achieving this.
Any links/videos etc...

Specially if you can point  how do i list files in the current directory which open when it is clicked... That should be a good beginning point.

Thanks

Avatar of noci
noci

Why write a new one......
you can use a http server, like nginx, apache etc.   point the document root to the right directory. (File download is also possibe).
If you need more with upload/download & security try Nextcloud.
Or less ford Knox like: http://github.com/DanRohde/webdavcgi
Avatar of Rohit Bajaj

ASKER

I am doing this as a practice assignment.
Need to do something simpler basic one
Http web server 
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial