response.appendheader("content-disposition", "attachment;filename=" + filename);
int bufsize = (int)stream.length;
byte[] buf = new byte[bufsize];
int bytesread = stream.read(buf, 0, bufsize);
patchdata(buf, edtusername.text, position);
response.outputstream.write(buf, 0, bytesread);
response.end();
}
finally {
stream.close();
}
}
源代码和运行例子
目前实现的这个版本有一个限制——为了简化这个demo,我们没有实现续载的功能。一旦如果你的文件大了,你也许想要改变这样的行为并增加支持续传。为了实现实现随即资源的访问功能,你需要分析文件头请求字段的范围。通过这个请求客户端指定他们需要下载的资源的字节范围。范围字段可以有1个或2个数字组成,如1024-23544。这表示客户端将要接收1024到23544字节间包含的字节数。参见hypertext transfer protocol rfc文档获得更多关于网络范围请求的信息。
这段代码将不断的优化和改进,我们随时欢迎你的评论和建议。