现在的位置: 首页 > 综合 > 正文

osgi搭建http server

2018年04月29日 ⁄ 综合 ⁄ 共 2586字 ⁄ 字号 评论关闭

详细参考:http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html

1.框架使用jetty框架,这个类似appace tomcat 

2.假如使用org.apache.felix.http.bundle这个需要额外配置http server功能开启的,否则使用单个bundle


3.服务端的例子

package server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class HttpServer extends HttpServlet {
	private static final String CONTENTFORM = "application/x-www-form-urlencoded";
	
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	  
	  
	  
	  
    resp.getWriter().write("Hello World");
    resp.getWriter().flush();
    resp.getWriter().close();
  }
  
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  
		System.out.println("..........HttpServer doPost begin...........");	
		
     //   response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String contentType = request.getContentType();
//check param begin
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        System.out.println(map);
        
        
        
        
        
        String fileAbsolutePath = map.get("filefullpath");
        String fileFullPath = "f:/" +  fileAbsolutePath == null?"nofilepath.txt":fileAbsolutePath;
        
        File testFp = new File(fileFullPath);
        if(!testFp.exists()){
        	
        	testFp.getParentFile().mkdirs();
        }
        
 //check end
        
        //表单项post上来
        if(contentType.equals( CONTENTFORM ))
        {
        	
        }
        else //post 文件
        {
        	
            InputStream is = request.getInputStream();
            long current = 0;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
            	
                FileOutputStream fileOutputStream = null;
                fileOutputStream = new FileOutputStream(fileFullPath, true);//filename
                bis = new BufferedInputStream(is);
                bos = new BufferedOutputStream(fileOutputStream);

                byte[] tmp = new byte[4096];
                int len;
                while ((len = bis.read(tmp)) != -1) {
                    bos.write(tmp, 0, len);
                    current += len;

                }
                bos.flush();
                bis.close();
                bos.close();
            } finally {

            }
        	
        	
        }
        

        
/*        PrintWriter out = response.getWriter();
        out.flush();
        out.close();*/
		
        System.out.println("..........HttpServer doPost end...........");
	}
  
}

3.active中注册服务

	    Hashtable props = new Hashtable();

	    props.put("alias", "/HttpServer");
	    props.put("init.message", "Hello World!");

	    this.registration = context.registerService(Servlet.class.getName(), new HttpServer(), props);

4.默认端口为8080


抱歉!评论已关闭.