📄 userequestprocessor.java
字号:
import java.net.*;import java.io.*;import java.util.*; public class useRequestProcessor implements Runnable { //设置属性 private static List pool = new LinkedList(); private File documentRootDirectory; private String indexFileName = "index.html"; //构造器 public useRequestProcessor(File documentRootDirectory,String indexFileName) { if (documentRootDirectory.isFile()) { throw new IllegalArgumentException( "documentRootDirectory must be a directory, not a file"); } //根目录 this.documentRootDirectory = documentRootDirectory; try { this.documentRootDirectory= documentRootDirectory.getCanonicalFile(); } catch (IOException e) { } if (indexFileName != null) this.indexFileName = indexFileName; } public static void processRequest(Socket request) { //同步 synchronized (pool) { pool.add(pool.size(), request); pool.notifyAll(); } } public void run() { //安全检查 String root = documentRootDirectory.getPath(); while (true) { Socket connection; synchronized (pool) { while (pool.isEmpty()) { try { pool.wait(); } catch (InterruptedException e) {} } //设置Socket connection = (Socket) pool.remove(0); } try { String filename; String contentType; //缓冲输出流 OutputStream raw = new BufferedOutputStream(Connection.getOutputStream() ); //新建Writer对象 Writer out = new OutputStreamWriter(raw); //输入缓冲 Reader in = new InputStreamReader(new BufferedInputStream(Connection.getInputStream()),"ASCII" ); StringBuffer requestLine = new StringBuffer(); int c; while (true) { //读取输入 c = in.read(); //设置循环中止条件 if (c == '\r' || c == '\n') break; requestLine.append((char) c); } String get = requestLine.toString(); // 输出请求 System.out.println(get); StringTokenizer st = new StringTokenizer(get); String method = st.nextToken(); String version = ""; if (method.equals("GET")) { //获取下一个令牌 filename = st.nextToken(); if (filename.endsWith("/")) filename += indexFileName; contentType = guessContentTypeFromName(filename); if (st.hasMoreTokens()) { version = st.nextToken(); } File theFile = new File(documentRootDirectory, filename.substring(1,filename.length())); // 不让客户端在文件根目录之外 if (theFile.canRead() && theFile.getCanonicalPath().startsWith(root)) { //新建输入流对象 DataInputStream fis = new DataInputStream( new BufferedInputStream( new FileInputStream(theFile))); byte[] theData = new byte[(int) theFile.length()]; fis.readFully(theData); fis.close(); if (version.startsWith("HTTP/")) { // 发送一个MIME 头 out.write("HTTP/1.0 200 OK\r\n"); Date now = new Date(); out.write("Date: " + now + "\r\n"); out.write("Server: JHTTP 1.0\r\n"); out.write("Content-length: " + theData.length + "\r\n"); out.write("Content-type: " + contentType + "\r\n\r\n"); out.flush(); } // 发送文件,可能是图像也可能是其他二进制数据 // 我们采用underlying输出流 raw.write(theData); raw.flush(); } // end if else { // 不能发现文件 if (version.startsWith("HTTP/")) { // 发送一个MIME头 out.write("HTTP/1.0 404 File Not Found\r\n"); Date now = new Date(); out.write("Date: " + now + "\r\n"); out.write("Server: JHTTP 1.0\r\n"); out.write("Content-type: text/html\r\n\r\n"); } //发送HTML out.write("<HTML>\r\n"); out.write("<HEAD><TITLE>File Not Found</TITLE>\r\n"); out.write("</HEAD>\r\n"); out.write("<BODY>"); out.write("<H1>HTTP Error 404: File Not Found</H1>\r\n"); out.write("</BODY></HTML>\r\n"); out.flush(); } } else { // 不等于"get"方法 if (version.startsWith("HTTP/")) { // 发送一个 MIME头 out.write("HTTP/1.0 501 Not Implemented\r\n"); Date now = new Date(); out.write("Date: " + now + "\r\n"); out.write("Server: JHTTP 1.0\r\n"); out.write("Content-type: text/html\r\n\r\n"); } out.write("<HTML>\r\n"); out.write("<HEAD><TITLE>Not Implemented</TITLE>\r\n"); out.write("</HEAD>\r\n"); out.write("<BODY>"); out.write("<H1>HTTP Error 501: Not Implemented</H1>\r\n"); out.write("</BODY></HTML>\r\n"); out.flush(); } } catch (IOException e) {} finally { try { //关闭连接 connection.close(); } catch (IOException e) {} } } // end while } // end run public static String guessContentTypeFromName(String name) { //返回文件类型 if (name.endsWith(".html") || name.endsWith(".htm")) { return "text/html"; } else if (name.endsWith(".txt") || name.endsWith(".java")) { return "text/plain"; } else if (name.endsWith(".gif")) { return "image/gif"; } else if (name.endsWith(".class")) { return "application/octet-stream"; } else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) { return "image/jpeg"; } else return "text/plain"; } } // end useRequestProcessor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -