title: map file directory from a java server to local machine
body: <p>I have created a java server application which will take GET requests and return files to the browser. Also the files are downloaded to the directory using "content-disposition:attachment" in header.But this only downloads as files, I want to download as a folder and map it to a local directory. for example 
  localhost:8080/xyz.jpg gives u an image .But localhost:8080/src/xyz.jpg should be downlaoded as src folder with an image in it eg: downloads/src/xyz.jpg. Currently if i do that ,it downloads as just a file. Since you guys asked.Here is the example code i used :) .thanks</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  
  public class myHTTPServer extends Thread {
  
      static final String HTML_START =
              "&lt;html&gt;" +
                      "&lt;title&gt;HTTP Server in java&lt;/title&gt;" +
                      "&lt;body&gt;";
  
      static final String HTML_END =
              "&lt;/body&gt;" +
                      "&lt;/html&gt;";
  
      Socket connectedClient = null;
      BufferedReader inFromClient = null;
      DataOutputStream outToClient = null;
  
  
      public myHTTPServer(Socket client) {
          connectedClient = client;
      }
  
      public void run() {
  
          try {
  
              System.out.println( "The Client "+
                      connectedClient.getInetAddress() + ":" + connectedClient.getPort() + " is connected");
  
              inFromClient = new BufferedReader(new InputStreamReader (connectedClient.getInputStream()));
              outToClient = new DataOutputStream(connectedClient.getOutputStream());
  
              String requestString = inFromClient.readLine();
              String headerLine = requestString;
  
              StringTokenizer tokenizer = new StringTokenizer(headerLine);
              String httpMethod = tokenizer.nextToken();
              String httpQueryString = tokenizer.nextToken();
  
              StringBuffer responseBuffer = new StringBuffer();
              responseBuffer.append("&lt;b&gt; This is the HTTP Server Home Page.... &lt;/b&gt;&lt;BR&gt;");
              responseBuffer.append("The HTTP Client request is ....&lt;BR&gt;");
  
              System.out.println("The HTTP request string is ....");
              while (inFromClient.ready())
              {
                  // Read the HTTP complete HTTP Query
                  responseBuffer.append(requestString + "&lt;BR&gt;");
                  System.out.println(requestString);
                  requestString = inFromClient.readLine();
              }
  
              if (httpMethod.equals("GET")) {
                  if (httpQueryString.equals("/")) {
                      // The default home page
                      sendResponse(200, responseBuffer.toString(), false);
                  } else {
  //This is interpreted as a file name
                      String fileName = httpQueryString.replaceFirst("/", "");
                      fileName = URLDecoder.decode(fileName);
                      if (new File(fileName).isFile()){
                          sendResponse(200, fileName, true);
                      }
                      else {
                          sendResponse(404, "&lt;b&gt;The Requested resource not found ...." +
                                  "Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/&lt;/b&gt;", false);
                      }
                  }
              }
              else sendResponse(404, "&lt;b&gt;The Requested resource not found ...." +
                      "Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/&lt;/b&gt;", false);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void sendResponse (int statusCode, String responseString, boolean isFile) throws Exception {
  
          String statusLine = null;
          String serverdetails = "Server: Java HTTPServer";
          String contentLengthLine = null;
          String fileName = null;
          String contentTypeLine = "Content-Type: text/html" + "\r\n";
          String Content_disposition="Content-Disposition: attachment; filename='src/fname.ext'";
  
          FileInputStream fin = null;
  
          if (statusCode == 200)
              statusLine = "HTTP/1.1 200 OK" + "\r\n";
          else
              statusLine = "HTTP/1.1 404 Not Found" + "\r\n";
  
          if (isFile) {
              fileName = responseString;
              fin = new FileInputStream(fileName);
              contentLengthLine = "Content-Length: " + Integer.toString(fin.available()) + "\r\n";
              if (!fileName.endsWith(".htm") &amp;&amp; !fileName.endsWith(".html"))
                  contentTypeLine = "Content-Type: \r\n";
          }
          else {
              responseString = myHTTPServer.HTML_START + responseString + myHTTPServer.HTML_END;
              contentLengthLine = "Content-Length: " + responseString.length() + "\r\n";
          }
  
          outToClient.writeBytes(statusLine);
          outToClient.writeBytes(serverdetails);
          outToClient.writeBytes(contentTypeLine);
          outToClient.writeBytes(contentLengthLine);
          outToClient.writeBytes(Content_disposition);
          outToClient.writeBytes("Connection: close\r\n");
          outToClient.writeBytes("\r\n");
  
          if (isFile) sendFile(fin, outToClient);
          else outToClient.writeBytes(responseString);
  
          outToClient.close();
      }
  
      public void sendFile (FileInputStream fin, DataOutputStream out) throws Exception {
          byte[] buffer = new byte[1024] ;
          int bytesRead;
  
          while ((bytesRead = fin.read(buffer)) != -1 ) {
              out.write(buffer, 0, bytesRead);
          }
          fin.close();
      }
  
      public static void main (String args[]) throws Exception {
  
          ServerSocket Server = new ServerSocket (5000, 10, InetAddress.getByName("127.0.0.1"));
          System.out.println ("TCPServer Waiting for client on port 5000");
  
          while(true) {
              Socket connected = Server.accept();
              (new myHTTPServer(connected)).start();
          }
      }
  }
  </code></pre>