Reputation: 483
I am writing a webserver that returns static html pages. In response to the request GET /path/to/file/index.html HTTP/1.0, I am supposed to return index.html. I am writing this in Java, so I am basically returning the page via socket.outputstream. However, I don't know how to return an html file via outputstream. Do I have to open the input file, read it line by line, and then print it to socket.outputstream? Or is there a shortcut way of sending an html file as a HTTP response? Thanks!
Upvotes: 1
Views: 6152
Reputation: 27233
If you need to use streams, then yes, you need to copy the data yourself. You can find example code here and here.
You can however use SocketChannel.open()
from java.nio to open the socket, FileChannel.open()
to open the file and then FileChannel.transferTo()
method to transfer all file's data to the socket.
Upvotes: 2