0

I'm trying to build a simple HTTP Server using Java, using

java.net.ServerSocket = new ServerSocket(this.port, 0, this.ip);
java.net.Socket connection = null;
connection = server.accept();
java.io.OutputStream out = new BufferedOutputStream(connection.getOutputStream());

when connected using web browser, i'm simply write the output (HTTP headers + html code) from a string

String headers = "http headers";
String response = "this is the response";
out.write(headers.getBytes());
out.write(response.getBytes());
out.flush();
connection.close();

and the browser display it correctly.

And now my problem is, i want to construct a full webpage (html, javascript, css, images) and put those files into the Java package (JAR) file, and of course, those files are designed not-to-be modified after the JAR is ready to use. And here's the questions:

  1. how to achieve this? storing the files inside the JAR and then output them when a connection is made.

  2. how output images file (non-text) just like output-ing String by out.write() ?

Thanks, any sample or code is appreciated.

Lee
  • 3,259
  • 4
  • 21
  • 27

3 Answers3

0

To work with JAR files use JarOutputStream or ZipOutputStream. To output binary data just do not wrap your output stream with Writer. OuputStream knows to write bytes using method write(byte) and write(byte[]).

The only question here is "Why are you developing HTTP server yourself?"

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • the JAR file is the running application itself. Not reading from "other" JAR file. So, something like taking resources from the application itself. – Lee Nov 20 '11 at 15:19
  • I'm not trying to build Stand-alone httpserver, but actually building an application, but i just simply trying to build its interface using HTML (webpage), and then the engine is JAVA, since i'm not really good in Swing or other Java GUI programming. So the application will be compiled into a single JAR file, containing the app logic, and teh webpages, with built-in httpserver inside it. – Lee Nov 20 '11 at 15:21
0

Is implementing an HTTP server your primary problem or just a way to achieve some other goal? If the latter, consider embedding Tomcat or Jetty, much simpler and with standard servlet API.

Back to your question: JAR is just a ZIP file, you can put anything there, including files, images, movies, etc. If you place a file inside a JAR file you can load it easily with:

InputStream is = getClass().getResourceAsStream("/dir/file.png");

See these questions for details how getResourceAsStream() works:

About your second question: when you have an InputStream instance you can just read it byte-by-byte and copy to target out OutputStream. Of course there are better, safer and faster ways, but that's beyond the scope of this question. Just have a look at IOUtils.copy():

IOUtils.copy(is, out);

And the last hint concerning your code: if you are sending Strings , consider OutputStreamWriter and PrintWriter which have easier API.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • how to determine the path as "/dir/file.png", i'm currently trying your code, but the `is` seems to be null, but i'm sure the filename is correct, any clue please? – Lee Nov 20 '11 at 15:39
  • I suspected you will have this problem so I added few links to my answer to provide you some extra information. In short: `/dir` is a directory inside you JAR. If you extract it with `jar xf file.jar` there will be a `/dir` directory at the top level. Try it with your current class: if you have `com.example.Foo`, then loading `/com/example/Foo.class` should work. – Tomasz Nurkiewicz Nov 20 '11 at 15:53
  • thanks Tomasz, i need a lot to learn. I still can't get it work, the `is` is still null. – Lee Nov 20 '11 at 16:07
  • @Lee: Where are you placing your file and how are you opening it? You might consider opening another question. – Tomasz Nurkiewicz Nov 20 '11 at 16:16
  • i'll do. but not now, it's midnight here (WIT). Thanks anyway. and the file is inside the project (Eclipse), inside the `src`, i created a folder `pages`, and the file `index.html`. Still, inside the `src`, there's a folder/package `myApp` and `my.java` is trying to open `InputStream is = getClass().getResourceAsStream("/pages/index.html");` – Lee Nov 20 '11 at 16:33
  • the folder `pages` is in the same level as `myApp` package – Lee Nov 20 '11 at 16:34
0

As long as it is not a housework I would not try to reinvent the wheel and develop another web server. There a small embedded Java web-servers available which can be used for that purpose.

I have for example use the Tiny Java Web Server and Servlet Container several times.

If you have integrated it into your application you can implement a new javax.servlet.http.HttpServlet that reads the files from the resources of your JAR file. The content can be loaded as Tomasz Nurkiewicz already pointed out getClass().getRourceAsStream(...).

Robert
  • 39,162
  • 17
  • 99
  • 152