标签:
Atitit.嵌入式web 服务器 java android最佳实践
3. CyberHTTP for Java cybergarage6
First使用jetty9,三说jdk编译级别非常高的...
i-jetty 就是jetty6的apk版本..马用..
使用jetty6 ,三,jpg都ok....大的文件就jetty.io.EofException..找到资料没法儿解除...放弃..jetty6 的bug应该..
子好自己实现web server兰...
AtiPlat_vodcp/bin/classes
/AtiPlat_vodcp/src/com/attilax/web/HttpServer.java
差不多也ok兰.就是性能稍微不行...
CyberHTTP for Java cybergarage
CyberHTTP 这个很不错...走这个兰啊..
作者:: ★(attilax)>>> 绰号:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊 ) 汉字名:艾龙, EMAIL:1466519819@qq.com
转载请注明来源: http://www.cnblogs.com/attilax/
package com.attilax.web;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jcifs.smb.SmbFile;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.handler.AbstractHandler;
import com.attilax.util.StreamUtil;
/**
* @author thomaslwq
* @version 创建时间:Sep 5, 2012 9:59:16 PM 类说明
*/
public class HttpServer {
/**
* @param args
*/
// WEB_ROOT是服务器的根目录
public static final String WEB_ROOT = System.getProperty("user.dir")
+ File.separator + "webroot";
// 关闭的命令
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
private int port;
private Handler handler;
public HttpServer(int port) {
this.port = port;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Handler handler = new AbstractHandler() { // 创建一个句柄
public void handle(String target, HttpServletRequest request,
HttpServletResponse response, int dispatch)
throws IOException {
// target zosh url
// response.setContentType("text/html");
// response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
// response.setHeader("Connection", "Keep-Alive");
// response.setHeader("Keep-Alive", "timeout=2000");
//response.setHeader("Content-Type", "image/jpeg");
response.setHeader("Content-Type", "video/mp4");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Server", "AtiWebServer");
ServletOutputStream outputStream = response.getOutputStream();
System.out.println("----handle");
// if("1".equals("1"))
// t(outputStream);
String smb = request.getParameter("smb");
if (smb == null)
return;
SmbFile file = new SmbFile(smb);
System.out.println(" ---jetty6FilesServer handle start ");
System.out.println("--smb file is exist:" + file.exists());
// file length
long contentLen = file.length();
try {
response.setContentLength(Integer.parseInt(String
.valueOf(contentLen)));
} catch (Exception e) {
e.printStackTrace();
response.setContentLength(Integer.MAX_VALUE);
}
InputStream contentIn = file.getInputStream();
((Response) response).wirteNflushHead();
// ( ( AtiServletOutputStream )outputStream).wirteNflushHead();
new StreamUtil().convertStream(contentIn, outputStream);
System.out.println(" ---jetty6FilesServer handle finish ");
// response.getWriter().println("<h1>Hello world</h1>");
// ((Request) request).setHandled(true);
}
private void t(ServletOutputStream outputStream) throws IOException {
{
// 找不到文件
String errorMessage = "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "Content-Length: 4\r\n" + "\r\n" + "okkk";
byte[] bytes = errorMessage.getBytes();
outputStream.write(bytes);
outputStream.flush();
// return;
}
}
};
HttpServer server = new HttpServer(7788);
server.setHandler(handler);
System.out.println("start..");
server.start();
}
private void setHandler(Handler handler) {
this.handler = handler;
}
public void start() {
ServerSocket serverSocket = null;
// int port = 7788;
try {
serverSocket = new ServerSocket(port, 100);
while (true) {
Socket socket = serverSocket.accept();
sinlgeHandle(socket);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sinlgeHandle(final Socket socket) {
Runnable r = new Runnable() {
public void run() {
try {
// Socket socket = null;
InputStream input = null;
OutputStream output = null;
input = socket.getInputStream();
output = socket.getOutputStream();
// 封装request请求
Request request = new Request(input);
request.parse();
// 封装response对象
Response response = new Response(output);
response.setRequest(request);
// response.sendStaticResource();
response.exeHandler(handler);
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
};
new Thread(r).start();
}
}
package demo.playfile.service;
import org.cybergarage.http.HTTPServer;
import demo.playfile.util.FileServer;
public class AtiHttpServer {
public static void main(String[] args) {
HTTPServer srv=new HTTPServer();
srv.open("127.0.0.1", 7788);
srv.addRequestListener(new FileServer());
System.out.println("---http start");
srv.start();
System.out.println("---http finish over");
}
}
Atitit.嵌入式web 服务器 java android最佳实践
标签:
原文地址:http://www.cnblogs.com/attilax/p/5334269.html