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

📄 tempdirhelper.java

📁 杰表服务器 部分源码 杰创网络报表(简称“杰表”)是一款国产的网络报表开发工具
💻 JAVA
字号:
package servlet;

import com.jatools.core.ZFileFactory;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
public class TempDirHelper implements ZFileFactory {
    static String TEMP = "temp";
    static File temp_dir;
    HttpSession session;

    /**
     * Creates a new jrs object.
     * @throws ServletException
     */
    public TempDirHelper(HttpServletRequest request) throws ServletException {
        this.session = request.getSession();

        if (temp_dir == null) {
            initDir(request);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @throws ServletException DOCUMENT ME!
     */
    public void initDir(HttpServletRequest request) throws ServletException {
        String ctx = request.getSession().getServletContext().getRealPath("/");

        if (!ctx.endsWith(File.separator)) {
            ctx = ctx + File.separator;
        }

        temp_dir = new File(ctx + "temp");

        if (!temp_dir.exists()) {
            temp_dir.mkdirs();
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param prefix DOCUMENT ME!
     * @param ext DOCUMENT ME!
     * @param autodelete DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public File createTempFile(String prefix, String ext, boolean autodelete) {
        File file = null;

        try {
            file = File.createTempFile(prefix, ext, temp_dir);
        } catch (IOException ioexception) {
        }

        return file;
    }

    /**
     * DOCUMENT ME!
     *
     * @param prefix DOCUMENT ME!
     * @param ext DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public File createTempFile(String prefix, String ext) {
        return createTempFile(prefix, ext, true);
    }

    /* 对应该临时文件的引用路径
	 *
     * @see com.jatools.core.ZFileFactory#register(java.io.File)
     */
    public String register(File file) {
        session.setAttribute(file.getName(), new TempFile(file));

        return TEMP + "/" + file.getName();
    }

    /* member class not found */
    class TempFile implements HttpSessionBindingListener {
        File file;

        TempFile(File file) {
            this.file = file;
        }

        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        }

        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
            file.delete();
        }

        File getFile() {
            return file;
        }
    }
}

⌨️ 快捷键说明

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