开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from 老馬/WebJava
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
WebJava
/
Web
/
StaticFileServer.java
WebJava
/
Web
/
StaticFileServer.java
StaticFileServer.java 6.80 KB
一键复制 编辑 原始数据 按行查看 历史
Ma 提交于 2015年06月12日 09:41 +08:00 . 添加Demo
package Web;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.TimeUnit;
/**
* 静态文件服务器
*
* @author Ma
*/
public class StaticFileServer {
public Request request;
public int size;
public ByteBuffer byteBuffer;
public SocketChannel socketChannel;
public Path filePath;
public String ExName;//扩展名字
/**
* 初始化一些东西
*
* @param request
*/
public StaticFileServer(Request request) {
this.request = request;
String path = Options.StaticPath + this.request.Path.substring(8);
this.filePath = Paths.get(path);
//设置扩展名
int index = this.request.Path.lastIndexOf(".");
this.ExName = this.request.Path.substring(index + 1);
}
/**
* 检测文件是否存在
*
* @return
*/
public boolean exists() {
return Files.exists(filePath);
}
/**
* 检查文件是否需要更新还是304状态吗
*
* @return
*/
public boolean update() {
try {
int lastTime = (int) Files.getLastModifiedTime(filePath).to(TimeUnit.SECONDS);//获取最后修改的时间戳
Object paramTime = this.request.Header.get("If-Modified-Since");
if ((paramTime != null) && (Integer.parseInt((String) paramTime) <= lastTime)) {
//处理304
Response.setStatus(304);
Response.setContentType(Response.findContentType(this.ExName));
Response.header("Last-Modified: " + lastTime);
Response.header("Cache-Control:public, max-age=2592000");
byte[] headerBytes = Response.getHeader().getBytes();
ByteBuffer writerBuffer = ByteBuffer.allocate(headerBytes.length);
writerBuffer.clear();
writerBuffer.put(headerBytes);
writerBuffer.flip();
while (writerBuffer.hasRemaining()) {
this.socketChannel.write(writerBuffer);
}
writerBuffer.clear();
this.socketChannel.close();
//输出debug信息
if (Options.DEBUG || Options.ParseLine) {
System.out.println(Response.Status + " " + this.request.Header.get("Host") + this.request.Path);
}
Response.finish();//清理信息
return false;
}
} catch (IOException ex) {
Logger.getLogger(StaticFileServer.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
public void read(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
//处理304
if (this.update()) {
//输出debug信息
if (Options.DEBUG || Options.ParseLine) {
System.out.println("Static File" + " " + this.request.Header.get("Host") + this.request.Path);
}
//处理200
BasicFileAttributeView basicView = Files.getFileAttributeView(this.filePath, BasicFileAttributeView.class);
try {
BasicFileAttributes basicAttrs = basicView.readAttributes();
long size = basicAttrs.size();
if (size > 2097152) {
//说明文件大于2M
}
this.size = (int) size;
int lastTime = (int) Files.getLastModifiedTime(filePath).to(TimeUnit.SECONDS);//获取最后修改的时间戳
this.byteBuffer = ByteBuffer.allocate((int) size);//申请缓存空间
//开始获取通道,读取文件
AsynchronousFileChannel afc = AsynchronousFileChannel.open(this.filePath);
afc.read(byteBuffer, 0, lastTime, new CompletionHandlerImpl(afc));
} catch (IOException ex) {
//处理500错误
Logger.getLogger(StaticFileServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* 成员类 处理文件完成后的操作
*/
private class CompletionHandlerImpl implements CompletionHandler<Integer, Object> {
AsynchronousFileChannel afc;
public CompletionHandlerImpl(AsynchronousFileChannel afc) {
this.afc = afc;
}
/**
* 处理成功的操作
*
* @param result
* @param attachment
*/
@Override
public void completed(Integer result, Object attachment) {
Integer lastModifyInteger = (Integer) attachment;
int lastModify = lastModifyInteger.intValue();
//Header不能使用Response
String header = "HTTP/1.1 200 OK\r\n"
+ "Content-type: " + Response.findContentType(StaticFileServer.this.ExName) + ";\r\n"
+ "Content-ength: " + StaticFileServer.this.size + "\r\n"
+ "Last-Modified: " + lastModify + "\r\n"
+ "Cache-Control:public, max-age=2592000 \r\n"
+ "Server: " + Response.Server + "\r\n\r\n";
byte[] headerBytes = header.getBytes();
byte[] contentBytes = StaticFileServer.this.byteBuffer.array();
ByteBuffer writerBuffer = ByteBuffer.allocate(headerBytes.length + contentBytes.length);
writerBuffer.clear();
writerBuffer.put(headerBytes);
writerBuffer.put(contentBytes);
writerBuffer.flip();
try {
while (writerBuffer.hasRemaining()) {
StaticFileServer.this.socketChannel.write(writerBuffer);
}
writerBuffer.clear();
StaticFileServer.this.socketChannel.close();
this.afc.close();
} catch (IOException ex) {
Logger.getLogger(StaticFileServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 处理失败的操作
*
* @param exc
* @param attachment
*/
@Override
public void failed(Throwable exc, Object attachment) {
System.out.println(exc.getCause());
//@todo 处理500错误
try {
StaticFileServer.this.socketChannel.close();
this.afc.close();
} catch (IOException ex) {
Logger.getLogger(StaticFileServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Web.Java是一个高性能的Java服务器。
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yan5845hao/WebJava.git
git@gitee.com:yan5845hao/WebJava.git
yan5845hao
WebJava
WebJava
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /