HTTP MIME Response : MIME « Network Protocol « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » MIMEScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.