Atitit.嵌入式web 服务器 java android最佳实践

时间:2021-11-12 21:08:03

Atitit.嵌入式web服务器 java android最佳实践

 

1.Android4.4.21

2.自己的webserver1

3.CyberHTTP forJava  cybergarage6

 

 

1. Android4.4.2 

First使用jetty9,三说jdk编译级别非常高的...

 

 

i-jetty 就是jetty6apk版本..马用..

 

使用jetty6 ,,jpgok....大的文件就jetty.io.EofException..找到资料没法儿解除...放弃..jetty6 的bug应该..

子好自己实现web  server兰...

AtiPlat_vodcp/bin/classes

/AtiPlat_vodcp/src/com/attilax/web/HttpServer.java

差不多也ok兰.就是性能稍微不行...

CyberHTTP forJava  cybergarage

CyberHTTP这个很不错...走这个兰啊..

 

作者::  ★(attilax)>>>   绰号:老哇的爪子  全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊  汉字名:艾龙,  EMAIL:1466519819@qq.com

转载请注明来源: http://blog.csdn.net/attilax

 

2. 自己的webserver

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 StringWEB_ROOT = System.getProperty("user.dir")

+ File.separator +"webroot";

 

// 关闭的命令

private static final StringSHUTDOWN_COMMAND ="/SHUTDOWN";

 

private int port;

 

private Handlerhandler;

 

public HttpServer(int port) {

this.port =port;

}

 

public static void main(String[]args) {

//TODO Auto-generated method stub

 

Handlerhandler =new AbstractHandler() {// 创建一个句柄

public void handle(Stringtarget, HttpServletRequestrequest,

HttpServletResponseresponse,int dispatch)

throws IOException {

// targetzosh 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");

 

ServletOutputStreamoutputStream =response.getOutputStream();

System.out.println("----handle");

// if("1".equals("1"))

// t(outputStream);

 

Stringsmb =request.getParameter("smb");

if (smb ==null)

return;

 

SmbFilefile =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 (Exceptione) {

e.printStackTrace();

response.setContentLength(Integer.MAX_VALUE);

}

 

InputStreamcontentIn =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(ServletOutputStreamoutputStream)throws IOException {

{

// 找不到文件

StringerrorMessage ="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;

}

}

};

 

HttpServerserver =new HttpServer(7788);

server.setHandler(handler);

System.out.println("start..");

server.start();

 

}

 

private void setHandler(Handlerhandler) {

this.handler =handler;

 

}

 

public void start() {

ServerSocketserverSocket =null;

//int port = 7788;

try {

serverSocket =new ServerSocket(port, 100);

while (true) {

Socketsocket =serverSocket.accept();

sinlgeHandle(socket);

 

}

}catch (Exceptione) {

//TODO Auto-generated catch block

e.printStackTrace();

}

 

}

 

private void sinlgeHandle(final Socketsocket) {

Runnabler =new Runnable() {

public void run() {

try {

// Socket socket = null;

InputStreaminput =null;

OutputStreamoutput =null;

 

input =socket.getInputStream();

output =socket.getOutputStream();

// 封装request请求

Requestrequest =new Request(input);

request.parse();

// 封装response对象

Responseresponse =new Response(output);

response.setRequest(request);

// response.sendStaticResource();

response.exeHandler(handler);

socket.close();

}catch (Exceptione) {

//TODO Auto-generated catch block

e.printStackTrace();

return;

}

}

};

new Thread(r).start();

 

}

 

}

 

3. CyberHTTP forJava  cybergarage

 

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");

}

 

}