歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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();
  }
 }
 
}
}

Copyright © Linux教程網 All Rights Reserved