Java Web基本实例


Java Web基本实例:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

WebServer.java监听某个端口
public class WebServer {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int port=80;
   if(args.length==1){
 port=Integer.parseInt(args[0]);

} new WebServer().serverStart(port);
 }

 //对80端口监听
 public void serverStart(int port){
  try {
   ServerSocket serverScoket =new ServerSocket(port);
   while(true){
    Socket scoket=serverScoket.accept();
    new Processor(scoket).start();
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

Processor.java 处理请求文件是否存在。存在则返回给客户端,不存在则返回404

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class Processor extends Thread{
 private Socket socket;
 private final static String WEB_ROOT="E:\\zhxh\\web\\SpringSecurity";
 private InputStream inputStream;
 private OutputStream out;
public Processor(Socket socket){
 this.socket=socket;
 try {
  inputStream=socket.getInputStream();
  out=socket.getOutputStream();
 } catch (IOException e) {
 
  e.printStackTrace();
 }
}
public void run(){
String fileName=parse(inputStream);//用户发过来的协议内容
sendFile(fileName);
}

public String parse(InputStream in){
 String fileName="";
 BufferedReader br=new BufferedReader(new InputStreamReader(in));
 try {
  String httpMessage=br.readLine();//获取第一行数据:用空格隔开的
     String[]content=httpMessage.split(" ");
     //1部分协议状态,2请求文件的名称,3协议版本号
     if(content.length!=3){
      sendErrorMessage(400,"客户请求错误");
      return null;
     }
     for(String str:content)
     System.out.println(str);
     fileName=content[1];
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return fileName;
 
}
public void sendErrorMessage(int errorCode,String errorMessage){
 PrintStream pOut=new PrintStream(out);
 pOut.println("HTTP/1.0 "+errorCode+" "+errorMessage+"");//协议版本,状态码,信息
 pOut.println("content-type:text/html");
pOut.println();
pOut.println("<html><title>错误信息</title><body><h1>errorCode:"+errorCode+","+errorMessage+"</h1></body></html>");
pOut.close();
try {
 out.close();
 inputStream.close();
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}

}
public void sendFile(String fileName){
 PrintStream pOut=new PrintStream(out);
 System.out.println(Processor.WEB_ROOT+fileName);
 File file=new File(Processor.WEB_ROOT+fileName);
 if(!file.exists()){
  sendErrorMessage(404,"资源不存在");
 }
 else {
  try {
   InputStream in=new FileInputStream(file);
   byte[]content=new byte[(int)file.length()];
   in.read(content);
  pOut.println("HTTP/1.0 200 查询文件");//协议版本,状态码,信息
  pOut.println("content-length:"+content.length);
  pOut.println();
  pOut.write(content);
  pOut.flush();
  pOut.close();
  out.close();
  inputStream.close();
 
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}
}

相关内容