0

I would like to implement a simple web server in java. The problem is that images are not correctly rendered on the web browser; all I can see, if I go to localhost:8888/image.png, is a white square with exact width, height and weight. Thank you in advance! :)

Here is the code:

public Http(Socket server) throws IOException {
    in = new BufferedReader(new InputStreamReader(server.getInputStream()));
    parseHeader(in);
    String response = new String();
    out = new PrintWriter(server.getOutputStream(), true);
    Files f = new Files(getHomePath() + httpRequestedPage);

    if(!f.exists) {
        // 404 ERROR
    } else {
            response += "HTTP/1.1 200 OK\r\n";
            response += "Date: " + nowDate + "\r\n";
            response += "Content-Type: image/png\r\n";
            response += "Content-Length: " + res.length() + "\r\n";
            response += "Connection: keep-alive\r\n";
            response += "\r\n";
            response +=  IOUtils.toString(new FileInputStream(getHomePath() + httpRequestedPage));
    }

    out.println(response);
    in.close();
    out.close();
}

EDIT:

Unfortunately it returns the same message.

    out = new PrintWriter(server.getOutputStream(), true);

    OutputStream out2 = server.getOutputStream(); 
    File file = new File(HttpServer.getHomePath() + httpRequestedPage);
    InputStream stream = new FileInputStream(file); 


    String response = new String();
    response += "HTTP/1.1 200 OK\r\n";
    response += "Date: " + nowDate + "\r\n";
    response += "Content-Type: image/png\r\n";
    response += "Content-Length: " + file.length() + "\r\n";
    response += "Connection: keep-alive\r\n";
    response += "\r\n";
    out.println(response);
    IOUtils.copy(stream, out2); 
    out.close();
    out2.close(); 

2 Answers 2

2

You are using Write class for rendering the image. Use the OutputStream to write the image. Images are bytes and always byte based streams should be used to render them.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried something like this, but unfortunately the browser returns "The image ..... cannot be displayed because it contains errors". OutputStream out2 = server.getOutputStream(); BufferedImage originalImage = ImageIO.read(new File(HttpServer.getHomePath() + httpRequestedPage)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write( originalImage, "png", baos ); baos.flush(); byte[] imageInByte = baos.toByteArray(); out2.write(imageInByte);
OutputStream out2 = server.getOutputStream(); server.setContentType("image/png"); InputStream stream = new FileInputStream(new File(HttpServer.getHomePath() + httpRequestedPage)); IOUtils.copy(stream, out2);
Unfortunately it returns me the same error. I edited the question, so that I can post there the new version of the code. Thanks
The code seems good to me. Try removing the Writer part. Use the exact code the above comment in your method ans see if that resolves that issue.
The thing is that in my code server is declared as 'private Socket server;', and I'm not writing a servlet app...so I can't use the setContentType method.
|
0

If you are converting bytes into String then you must use Base64 encoding. And on the client side you can specify the image src similar to this "data:image/png;base64," + imageData.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.