Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:51:10+00:00 2026-06-13T04:51:10+00:00

I am trying to display images inside my HTTP Web Server but I’m unable

  • 0

I am trying to display images inside my HTTP Web Server but I’m unable to. I can display the HTML. I think it is something to do with the way I handle my IO (input and output streams). There’s probably a lot of mistakes in there that I haven’t noticed.

import java.io.* ;
import java.net.* ;
import java.util.Properties;


public class HTTPThread extends Thread
{
    private Socket socket = null;
    private Properties config = null;
    private String root = "";

    public HTTPThread(Socket s, Properties config)
    {
        this.socket = s;
        this.config = config;
        this.root = this.config.getProperty("root");        
    }

    public void run()
    {   

//      InputStream in = null;
        OutputStream out = null;

        try
        {
            out = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(out, true);

            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String request = reader.readLine();
            writer.println("HTTP/1.1 200 OK");
            writer.println("Content-Type: text/html");          
            writer.println();

            // Search for filename 
            int slash = request.indexOf("/"); //find first occurrence of slash
            int emptySpace = request.indexOf(" ", slash); //find first space after slash
            String filename = request.substring(slash, emptySpace); //return filename 
//          writer.println("<b>" + filename + "</b>");

            String pathname = "";
            try
            {
                pathname = (filename == "/") ? root + filename : root;
//              System.out.println(filename);           
                URL url = new URL(pathname);
                URLConnection urlc = url.openConnection();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        urlc.getInputStream()));

                String line;                
                while ((line = in.readLine()) != null)
                {
                    writer.println(line);
                }
                in.close();
            }
            catch (MalformedURLException e)
            {
                System.err.println("Don't know about host: " + pathname);
                System.exit(1);
            }
            catch (IOException e)
            {
                  System.err.println("Couldn't get I/O for "
                                     + "the connection to: " + pathname);
                  System.exit(1);
            }



//          reader.close();
            writer.close();
            socket.close();
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);          
        }

        finally
        {
            try
            {
//              in.close() ;
                out.close() ;
                socket.close();
            }
            catch(IOException e)
            {
                System.out.println("Error: " + e);          
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:51:11+00:00Added an answer on June 13, 2026 at 4:51 am

    Are you trying to write some sort of proxy server that takes external URL from the request and returns the contents? Well, there are several issues with your code:

    writer.println("HTTP/1.1 200 OK");
    writer.println("Content-Type: text/html");          
    

    When browser sees the above, it assumes whatever is returned is HTML. Rendering binary image as HTML will clearly fail. Which leads us to this:

    String line;                
    while ((line = in.readLine()) != null)
    {
        writer.println(line);
    }
    in.close();
    

    In the loop above you are reading some external URL line-by-line (in text mode) and forwarding it to original client. This works for HTML (which is text-based) but will fail for any image (binary). You must use InputStream/OutputStream instead.

    And small thing at the end:

    pathname = (filename == "/") ? root + filename : root;
    

    Do not compare strings using == operator, replace it with:

    pathname = (filename.equals("/")) ? root + filename : root;
    

    As a final note, consider using servlet container like Tomcat or Jetty, which will release you from HTTP-handling code and provide more high-level constructs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display images using my web application written in Rails. I've come
I'm trying to display some large images with HTML img tags. At the moment
I am trying to produce images without gamma information so that IE8 can display
I'm trying to display a number of images horizontally inside of a fixed-width div.
I am trying to display all the images available in a blackberry device inside
I've been trying to display some images from the web in a UIWebView .
I am trying to retrieve data from an img element inside a html web
Greetings! I've been trying to display some HTML with Java using JEditorPane . But
From any specific Post, I am trying to display all images, with their corresponding
I'm trying to display video from a webcam. I capture the images from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.