qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

一个简单的Java服务器

/**
*
*/
package iotest.serversocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Brandon B. Lin
*
*/
public class SingleFileHttpServer extends Thread {
private byte[] content;
private byte[] header;
private int port = 80;
public SingleFileHttpServer(String data, String encoding, String MIMEType,
int port) throws UnsupportedEncodingException {
this(data.getBytes(encoding), encoding, MIMEType, port);
}
public SingleFileHttpServer(byte[] data, String encoding, String MIMEType,
int port) throws UnsupportedEncodingException {
this.content = data;
this.port = port;
createHeader(encoding, MIMEType);
}
private void createHeader(String encoding, String MIMEType)
throws UnsupportedEncodingException {
String header = "HTTP/1.0 200 OK\r\n" + "Server: OneFIle 1.0\r\n"
+ "Content-length: " + content.length + "\r\n"
+ "Content-type: " + MIMEType + "\r\n\r\n";
this.header = header.getBytes(encoding);
}
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(port);
log(server.getLocalPort());
while (true) {
respond(server);
}
} catch (IOException exception) {
System.err.println("Could not start server. Port Occupied!");
}
}
private void log(int port) {
System.out.println("Accepting connection on port " + port);
System.out.println("Data to be sent: ");
try {
System.out.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
private void respond(ServerSocket server) {
Socket connection = null;
try {
connection = server.accept();
String request = readRequestFromSocket(connection);
boolean writeHeader = (request.toString().indexOf("HTTP/") != -1);
writeToSocket(connection, writeHeader);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null)
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String readRequestFromSocket(Socket connection) throws IOException {
InputStream in = new BufferedInputStream(connection.getInputStream());
StringBuffer request = new StringBuffer(80);
while (true) {
int readByte = in.read();
if (readByte == '\r' || readByte == '\n' || readByte == -1)
break;
request.append((char) readByte);
}
return request.toString();
}
private void writeToSocket(Socket connection, boolean writeHeader)
throws IOException {
OutputStream out = new BufferedOutputStream(
connection.getOutputStream());
if (writeHeader) {
out.write(header);
}
out.write(content);
out.flush();
}
public static void main(String[] args) {
String fileName = "index.html";
String contentType = "text/html";
String encoding = "ASCII";
int port = 80;
byte[] data = readFileToByteArray(fileName);
try {
new SingleFileHttpServer(data, encoding, contentType, port).start();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private static byte[] readFileToByteArray(String fileName) {
byte[] data = null;
try {
InputStream in = new FileInputStream(fileName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int readByte;
while ((readByte = in.read()) != -1)
out.write(readByte);
data = out.toByteArray();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}

posted on 2014-06-20 13:21 顺其自然EVO 阅读(261) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜