⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 staticrequesthandler.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
字号:
package freecs.external;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

import freecs.Server;
import freecs.content.ContentContainer;
import freecs.interfaces.IRequest;

public class StaticRequestHandler extends AbstractRequestHandler {
    private HashMap fileCache = new HashMap();
    
	public StaticRequestHandler(String handlerName) {
		super(handlerName);
	}

	/**
	 * class rendering information on server internals
	 */

	public void handle(IRequest req, ContentContainer c) {
		String file = req.getAction();
        file = file.substring(file.substring(1).indexOf("/") + 2);
        Object cached = fileCache.get(file);
        if (cached != null) {
            FileProperties fp = (FileProperties) cached;
            if (!fp.f.exists()) {
                    c.setTemplate("not_found");
                    return;
            } else if (fp.f.lastModified() == fp.lastModified) {
                try {
                    c.setContent(fp.content);
                } catch (Exception e) {
                    throw new AccessForbiddenException(true);
                }
                c.setContentType(fp.contentType);
                return;
            }
        }
        if (file.indexOf("/") > -1 && !file.endsWith(".class"))
            throw new AccessForbiddenException (true);
        File f = new File (Server.BASE_PATH + "/static/" + file);
        if (!f.exists()) {
            c.setTemplate("not_found");
            return;
        }
        String contentType;
        if (file.toLowerCase().endsWith (".gif"))
            contentType="image/gif";
        else if (file.toLowerCase().endsWith (".jpg")
                || file.toLowerCase().endsWith (".jpeg"))
            contentType="image/jpeg";
        else if (file.toLowerCase().endsWith (".class"))
            contentType="application/x-java-applet";
        else
            throw new AccessForbiddenException (true);
        try {
            InputStream is = new FileInputStream (f);
            byte[] cntnt = new byte[(int) f.length()];
            for (int i = 0; i<cntnt.length; i++)
                cntnt[i] = (byte) is.read();
            fileCache.put(file, new FileProperties(f, cntnt, contentType));
            c.setContent(cntnt);
        } catch (Exception e) {
            Server.debug ("StaticRequestHandler", "exception during reading file " + file, e, Server.MSG_ERROR, Server.LVL_MINOR);
            c.setTemplate("techerror");
            return;
        }
        c.setContentType(contentType);
    }
    
    class FileProperties {
        long lastModified = -1;
        File f;
        byte[] content;
        String contentType;
        
        FileProperties (File f, byte[] content, String contentType) {
            this.f = f;
            this.content = content;
            this.contentType = contentType;
            lastModified = f.lastModified();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -