HTTP MIME Response : MIME « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » MIME 




HTTP MIME Response
  
//Revised from jcommon web;


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


public class HTTPResponse {
  public static final HashMap<String,String> MIME_TYPES = new HashMap<String,String>();
  static {
    MIME_TYPES.put("gif""image/gif");
    MIME_TYPES.put("jpeg""image/jpeg");
    MIME_TYPES.put("jpg""image/jpeg");
    MIME_TYPES.put("jpe""image/jpeg");
    MIME_TYPES.put("bmp""image/bmp");
    MIME_TYPES.put("png""image/png");
    MIME_TYPES.put("tif""image/tiff");
    MIME_TYPES.put("tiff""image/tiff");
    MIME_TYPES.put("jnlp""application/x-java-jnlp-file");
    MIME_TYPES.put("js""application/x-javascript");
    MIME_TYPES.put("doc""application/msword");
    MIME_TYPES.put("bin""application/octet-stream");
    MIME_TYPES.put("exe""application/octet-stream");
    MIME_TYPES.put("pdf""application/pdf");
    MIME_TYPES.put("ai""application/postscript");
    MIME_TYPES.put("eps""application/postscript");
    MIME_TYPES.put("ps""application/postscript");
    MIME_TYPES.put("rtf""application/rtf");
    MIME_TYPES.put("class""application/x-java-vm");
    MIME_TYPES.put("ser""application/x-java-serialized-object");
    MIME_TYPES.put("jar""application/x-java-archive");
    MIME_TYPES.put("sh""application/x-sh");
    MIME_TYPES.put("tar""application/x-tar");
    MIME_TYPES.put("zip""application/zip");
    MIME_TYPES.put("ua""audio/basic");
    MIME_TYPES.put("wav""audio/x-wav");
    MIME_TYPES.put("mid""audio/x-midi");
    MIME_TYPES.put("htm""text/html");
    MIME_TYPES.put("html""text/html");
    MIME_TYPES.put("css""text/css");
    MIME_TYPES.put("txt""text/plain");
    MIME_TYPES.put("mpeg""video/mpeg");
    MIME_TYPES.put("mpg""video/mpeg");
    MIME_TYPES.put("mpe""video/mpeg");
    MIME_TYPES.put("qt""video/quicktime");
    MIME_TYPES.put("mov""video/quicktime");
    MIME_TYPES.put("avi""video/avi");
    MIME_TYPES.put("movie""video/x-sgi-movie");
  }
  
  public static String SERVER = "JavaWebServer/1.0";
  
  public static final int OK = 200;
  
  private Socket s;
  private int mode;
  private HashMap<String,String> headers;
  private HashSet<String> keys;
  
  public HTTPResponse(Socket s, int modethrows IOException {
    this.s = s;
    this.mode = mode;
    
    headers = new HashMap<String,String>();
    keys = new HashSet<String>();
  }
  
  public void addHeader(String header, String value) {
    keys.add(header.toLowerCase());
    headers.put(header, value);
  }
  
  private void writeHeaders() throws IOException {
    if (mode == OK) {
      writeLine("HTTP/1.1 200 OK");
    }
    if (!keys.contains("server")) {
      headers.put("Server", SERVER);
    }
    if (!keys.contains("date")) {
      headers.put("Date", String.format("%EEE%, %d% %MMM% %yyyy% %HH%:%mm%:%ss% %Z%",new GregorianCalendar()));
    }
    
    Iterator<String> iterator = headers.keySet().iterator();
    String key;
    String value;
    while (iterator.hasNext()) {
      key = iterator.next();
      value = headers.get(key);
      writeLine(key + ": " + value);
    }
    
    writeLine("");
  }
  
  private void writeLine(String stringthrows IOException {
    s.getOutputStream().write((string + "\r\n").getBytes());
  }
  
  public void writeFile(File fthrows IOException {
    if ((!keys.contains("content-type")) && (f.getName().indexOf('.'> -1)) {
      String ext = f.getName().substring(f.getName().lastIndexOf('.'1).toLowerCase();
      if (MIME_TYPES.containsKey(ext)) {
        headers.put("Content-Type", MIME_TYPES.get(ext));
      }
    }
    if (!keys.contains("content-length")) {
      headers.put("Content-Length", String.valueOf(f.length()));
    }
    writeHeaders();
    
    FileInputStream fis = new FileInputStream(f);
    byte[] b = new byte[512];
    int len;
    while ((len = fis.read(b)) > -1) {
      s.getOutputStream().write(b, 0, len);
    }
    s.getOutputStream().flush();
    s.close();
  }
}

   
    
  














Related examples in the same category
1.MIME type mappings
2.A convenience class which handles conversions between MIME charset names and Java encoding names.
3.A utility class, which provides some MIME related application logic.
4.MIME types to use for various requests
5.Known mime types
6.Declaring MIME types to use for various requests and provide utility manipulation methods
7.Map file extensions to MIME types. Based on the Apache mime.types file
8.Frequently used MIME types
9.Print MIME header
10.enum Mime Type
11.Mime type map
12.Mime Type
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.