验证中...
语言: Java
分类: 常用工具包
最后更新于 2018-06-14 23:45
封装响应信息
原始数据 复制代码
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Date;
/**
* 封装响应信息
* @author Administrator
*
*/
public class Response {
//两个常量
public static final String CRLF="\r\n";
public static final String BLANK="";
//流
private BufferedWriter bw;
//正文
public StringBuilder content;
//存储头信息
private StringBuilder headInfo;
//存储正文长度
private int len;
public Response() {
headInfo =new StringBuilder();
content =new StringBuilder();
len=0;
}
public Response(Socket client) {
this();
try {
bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
headInfo=null;
}
}
public Response(OutputStream os) {
bw=new BufferedWriter(new OutputStreamWriter(os));
}
/**
* 构建正文
*/
public Response print(String info) {
content.append(info);
len+=info.getBytes().length;
return this;
}
/**
* 构建正文+回车
* @param info
* @return
*/
public Response println(String info) {
content.append(info).append(CRLF);
len+=(info+CRLF).getBytes().length;
return this;
}
/**
* 构建响应头
*/
private void createHeadInfo(int code) {
//1) HTTP协议版本、状态代码、描述
headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);
switch(code) {
case 200:
headInfo.append("OK");
break;
case 404:
headInfo.append("NOT FOUND");
break;
case 500:
headInfo.append("SEVER ERROR");
break;
}
headInfo.append(CRLF);
//2)响应头(Response Head)
headInfo.append("Server:uname Server/0.0.1").append(CRLF); //用户名
headInfo.append("Data:").append(new Date()).append(CRLF); //响应时间
headInfo.append("Content-type:text/html;charset=GBK").append(CRLF);
//正文长度:字节长度
headInfo.append("Content-Length:").append(len).append(CRLF);
//3)正文之前
headInfo.append(CRLF); //分隔符
}
//推送到客户端
void pushToClient(int code) throws IOException {
if(null==headInfo) {
code =500;
}
createHeadInfo(code);
//头信息+分隔符
bw.append(headInfo.toString());
//正文
bw.append(content.toString());
bw.flush();
}
public void close() throws IOException {
bw.close();
}
}

评论列表( 0 )

你可以在登录后,发表评论

11_float_left_people 11_float_left_close