你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:杂志经典 / 跟高手学编程
用Java实现非阻塞的HTTP服务器(十七)
 

Content接口有两个实现类:StringContentFileContentStringContent表示字符串形式的正文,FileContent表示文件形式的正文。例如在RequestHandler类的build()方法中,如果HTTP请求发式不是GETHEAD,就创建一个包含StringContentResponse对象,否则就创建一个包含FileContentResponse对象。

  private void build() throws IOException {

    Request.Action action = request.action();

    //仅仅支持GET和HEAD请求方式

    if ((action != Request.Action.GET) &&

            (action != Request.Action.HEAD)){

       response = new Response(Response.Code.METHOD_NOT_ALLOWED,

                          new StringContent("Method Not Allowed"));

    }else{

       response = new Response(Response.Code.OK,

                      new FileContent(request.uri()), action);

    }

  }

下面主要介绍FileContent类的实现。FileContent类有一个成员变量fileChannel,它表示读文件的通道。FileContent类的send()方法把fileChannel中的数据发送到ChannelIOSocketChannel中,如果文件中的所有数据发送完毕,send()方法就返回false。例程8FileContent类的源程序:

//例程8  FileContent.java

//此处省略import语句

public class FileContent implements Content {

  //假定文件的根目录为"root",该目录应该位于classpath

  private static File ROOT = new File("root");

  private File file;

 

  public FileContent(URI uri) {

    file = new File(ROOT,

                  uri.getPath()

                  .replace('/',File.separatorChar));

  }

 

  private String type = null;

 

  /* 确定文件类型 */

  public String type() {

    if (type != null) return type;

    String nm = file.getName();

    if (nm.endsWith(".html")|| nm.endsWith(".htm"))

        type = "text/html; charset=iso-8859-1";  //HTML网页

    else if ((nm.indexOf('.') < 0) || nm.endsWith(".txt"))

        type = "text/plain; charset=iso-8859-1";  //文本文件

    else

        type = "application/octet-stream";  //应用程序

    return type;

  }

 

  private FileChannel fileChannel = null;

  private long length = -1;  //文件长度

  private long position = -1; //文件的当前位置     

 

  public long length() {

      return length;

  }

  推荐精品文章

·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 
·2023年7月目录
·2023年6月目录 
·2023年5月目录
·2023年4月目录 
·2023年3月目录 
·2023年2月目录 
·2023年1月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089