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 7179815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:13:02+00:00 2026-05-28T17:13:02+00:00

When i use one jpeg image with this web-server its very slow to show

  • 0

When i use one jpeg image with this web-server its very slow to show the picture in the browser. But the same picture when i open using Apache web server its super fast.

What am i missing in my code which is so slow to render the jpeg file? Following is the code i am using:

server.java:

package www;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class server extends Thread 
{
  public server(int listen_port, webserver_starter to_send_message_to) 
  {
    message_to = to_send_message_to;
    port = listen_port;
    this.start();
  }

  private void s(String s2) 
  { 
    message_to.send_message_to_window(s2);
  }

  private webserver_starter message_to; 
  private int port; 

  public void run() 
  {
    ServerSocket serversocket = null; 
    s("The httpserver v. 0000000000\n\n");

    try {   
      s("Trying to bind to localhost on port " + Integer.toString(port) + "...");      
      serversocket = new ServerSocket(port);
    }catch (Exception e) {
      s("\nFatal Error:" + e.getMessage());

      System.exit(0);
      return;
    }
    s("OK!\n");

    while (true) 
    {
      s("\nReady, Waiting for requests...\n");
      try {
        Socket connectionsocket = serversocket.accept();
        InetAddress client = connectionsocket.getInetAddress();
        s(client.getHostName() + " connected to server.\n");
        BufferedReader input =
            new BufferedReader(new InputStreamReader(connectionsocket.
            getInputStream()));
        DataOutputStream output =
            new DataOutputStream(connectionsocket.getOutputStream());
        http_handler(input, output);
      } catch (Exception e) {         
        s("\nError:" + e.getMessage());
      }

    }
  }

  private void http_handler(BufferedReader input, DataOutputStream output) 
  {
    int method = 0;                   //1 get, 2 head, 0 not supported
    String http = new String();       //a bunch of strings to hold
    String path = new String();       //the various things, what http v, what path,
    String file = new String();       //what file
    String user_agent = new String(); //what user_agent
    try {

      String tmp = input.readLine();  //read from the stream
      String tmp2 = new String(tmp);
      tmp.toUpperCase();              //convert it to uppercase
      if (tmp.startsWith("GET")) {    //compare it is it GET
        method = 1;
      } //if we set it to method 1
      if (tmp.startsWith("HEAD")) {   //same here is it HEAD
        method = 2;
      } //set method to 2

      if (method == 0) { 
        try {
          output.writeBytes(construct_http_header(501, 0));
          output.close();
          return;
        }
        catch (Exception e3) { 
          s("error:" + e3.getMessage());
        }
      }


      int start = 0;
      int end = 0;
      for (int a = 0; a < tmp2.length(); a++) {
        if (tmp2.charAt(a) == ' ' && start != 0) {
          end = a;
          break;
        }
        if (tmp2.charAt(a) == ' ' && start == 0) {
          start = a;
        }
      }
      path = tmp2.substring(start + 2, end);
    }
    catch (Exception e) {
      s("errorr" + e.getMessage());
    } 

    s("\nClient requested:" + new File(path).getAbsolutePath() + "\n");
    FileInputStream requestedfile = null;

    try {
      requestedfile = new FileInputStream(path);
    }
    catch (Exception e) {
      try {
        output.writeBytes(construct_http_header(404, 0));
        output.close();
      }
      catch (Exception e2) {}
      ;
      s("error" + e.getMessage());
    } 


    try {
      int type_is = 0;

      if (path.endsWith(".zip")) 
      {
        type_is = 3;
      }

      if (path.endsWith(".jpg") || path.endsWith(".jpeg")) 
      {
        type_is = 1;
      }

      if (path.endsWith(".gif")) 
      {
        type_is = 2;       
      }

      output.writeBytes(construct_http_header(200, 5));

      if (method == 1) 
      { 
        while (true) 
        {
          int b = requestedfile.read();
          if (b == -1) {
            break; //end of file
          }
          output.write(b);
        }

      }
      output.close();
      requestedfile.close();
    }

    catch (Exception e) {}

  }

  private String construct_http_header(int return_code, int file_type) 
  {
    String s = "HTTP/1.0 ";

    switch (return_code) 
    {
      case 200:
        s = s + "200 OK";
        break;
      case 400:
        s = s + "400 Bad Request";
        break;
      case 403:
        s = s + "403 Forbidden";
        break;
      case 404:
        s = s + "404 Not Found";
        break;
      case 500:
        s = s + "500 Internal Server Error";
        break;
      case 501:
        s = s + "501 Not Implemented";
        break;
    }

    s = s + "\r\n"; 
    s = s + "Connection: close\r\n"; 
    s = s + "Server: SimpleHTTPtutorial v0\r\n";

    switch (file_type) {
      case 0:
        break;
      case 1:
        s = s + "Content-Type: image/jpeg\r\n";
        break;
      case 2:
        s = s + "Content-Type: image/gif\r\n";
      case 3:
        s = s + "Content-Type: application/x-zip-compressed\r\n";
      default:
        //s = s + "Content-Type: text/html\r\n";
        s = s + "Content-Type: image/jpeg\r\n";
        break;
    }
    s = s + "\r\n";
    return s;
  }
}
  • 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-05-28T17:13:03+00:00Added an answer on May 28, 2026 at 5:13 pm

    Well here’s the first thing I’d fix:

    while (true) 
    {
      int b = requestedfile.read();
      if (b == -1) {
        break; //end of file
      }
      output.write(b);
    }
    

    You’re reading and writing a single byte at a time. That will be painfully slow. Read and write a whole buffer at a time instead:

    byte[] buffer = new byte[32 * 1024]; // 32K is a reasonable buffer size
    int bytesRead;
    while ((bytesRead = requestedfile.read(buffer)) > 0) {
        output.write(buffer, 0, bytesRead);
    }
    

    There may well be other performance problems in your code – there are certainly a lot of things I’d change about it, including following Java naming conventions everywhere and certainly fixing this:

    // You should basically *never* have this code
    catch (Exception e){}
    

    … but as you asked about the performance, that’s the first bit I’ve checked for.

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

Sidebar

Related Questions

I wanted to use one of the Git-completion.bash features but I can't customize the
I'm trying to use a JPEG image in a QImage object from a Python
Hi I am integrating the watermark code http://koivi.com/php-gd-image-watermark/ This script works well. But I
i use this code to create thumbnails System.Drawing.Image.GetThumbnailImageAbort abort = new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback); System.Drawing.Image image2
I use imagecreatefromjpeg , imagecreatefromgif , and imagecreatefrompng functions to create thumbnails of image/jpeg
Why use one over the other?
Why one would use one of the following packages instead of the other? Java
And why would I use one over the other in my code?
I need to use one of my VB.NET projects in a C++ project. The
I need to use one logical PGM based multicast address in application while enable

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.