try {
/*监听端口号,只要是8081就能接收到*/
ServerSocket ss = new ServerSocket(8081);
System.out.println("启动HTTP服务成功");
while (true) {
/*实例化客户端,固定套路,通过服务端接受的对象,生成相应的客户端实例*/
Socket socket = ss.accept();
new ClientHandler(socket).start();
}
} catch (IOException e) {
System.out.println("启动HTTP服务失败: "+e.getMessage());
e.printStackTrace();
}
package httpSerever;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Date;
public class ClientHandler extends Thread {
private Socket socket = null;
private HttpReqHeader request = null;
ClientHandler(Socket socket)
{
this.socket = socket;
}
private HttpReqHeader parse(Socket socket)
{
request = new HttpReqHeader();
request.clientAddress = socket.getRemoteSocketAddress().toString();
System.out.println(request.clientAddress);
try
{
/*获取客户端输入流,就是请求过来的基本信息:请求头,换行符,请求体*/
BufferedReader bd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/**
* 接受HTTP请求,并解析数据
*/
String requestHeader;
int contentLength = 0;
while ((requestHeader = bd.readLine()) != null && !requestHeader.isEmpty()) {
System.out.println(requestHeader);
/**
* 获得GET参数
*/
int requestType = 0;
if (requestHeader.startsWith("GET")) {
request.requestType = "GET";
requestType = 1;
}
else if (requestHeader.startsWith("POST")) {
request.requestType = "POST";
requestType = 2;
}
else{
}
if (requestType>0) {
int begin = requestHeader.indexOf("/") + 1;
int end = requestHeader.indexOf("HTTP/");
String condition = requestHeader.substring(begin, end);
int questionPos = condition.indexOf("?");
if(condition.length()>0){
if(questionPos>0){
request.requestPage = condition.substring(0, questionPos);
request.setParams(condition.substring(questionPos+1));
System.out.println(condition.substring(questionPos));
}
else{
request.requestPage = condition;
System.out.println(condition);
}
}
System.out.println(request.requestType+"参数是:" + condition);
}
/**
* 获得POST参数
* 1.获取请求内容长度
*/
else if (requestHeader.startsWith("Content-Length")) {
int begin = requestHeader.indexOf("Content-Length:") + "Content-Length:".length();
String postParamterLength = requestHeader.substring(begin).trim();
contentLength = Integer.parseInt(postParamterLength);
request.contentLengh = contentLength;
System.out.println("POST参数长度是:" + Integer.parseInt(postParamterLength));
}
else if (requestHeader.startsWith("Content-Type")) {
int begin = requestHeader.indexOf("Content-Type:") + "Content-Type:".length();
request.ContentType = requestHeader.substring(begin).trim();
}
else if (requestHeader.startsWith("Host")) {
int begin = requestHeader.indexOf("Host:") + "Host:".length();
request.remoteHost = requestHeader.substring(begin).trim();
}
}
StringBuffer sb = new StringBuffer();
if (contentLength > 0) {
for (int i = 0; i < contentLength; i++) {
sb.append((char) bd.read());
}
System.out.println("POST参数是:" + sb.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return request;
}
public void run()
{
request = parse(socket); // I read and parse the HTTP request here
try
{
RandomAccessFile is = new RandomAccessFile("D:\\test\\2.jpg", "rw");
long fileLen = is.length();
int sendLen = 0;
/*发送回执*/
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("HTTP/1.1 200 OK");
pw.println("Accept-Ranges: bytes");
//pw.println("Content-type:text/html");
pw.println("Content-type: image/jpeg");
pw.println("Content-Length: "+fileLen);
pw.println("Server: device commu");
pw.println("Last-Modified: " + new Date());
pw.println("Date: " + new Date());
pw.println("ETag: " + new Date());
pw.println();
pw.flush();
//响应图片
OutputStream filew = socket.getOutputStream();
while(sendLen<fileLen)
{
int buflen = (int)Math.min(fileLen-sendLen, 10240);
byte[] readBuf = new byte[buflen];
int readLen = is.read(readBuf, 0, buflen);
char[] cData = getChars(readBuf);
String data = new String(readBuf, "GBK");
filew.write(readBuf);
sendLen += readLen;
}
/*pw.println();
pw.println("<h1>successful</h1>");*/
pw.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static char[] getChars(byte[] bytes) {
Charset cs = Charset.forName("UTF-8");
ByteBuffer bb = ByteBuffer.allocate(bytes.length);
bb.put(bytes).flip();
CharBuffer cb = cs.decode(bb);
return cb.array();
}
public static byte[] charToByte(char c) {
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
}