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

📄 shangchuanxiazai.txt

📁 这是很好用的利用JAVA语言编写的程序
💻 TXT
字号:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

public class SmartDownload {

    protected HttpServletRequest m_request;

    protected HttpServletResponse m_response;

    protected ServletContext m_application;

    private boolean m_denyPhysicalPath;

    private String m_contentDisposition;

    public SmartDownload() {
        m_contentDisposition = "";
    }

    public final void init(ServletConfig servletconfig) throws ServletException {
        m_application = servletconfig.getServletContext();
    }

    public void service(HttpServletRequest httpservletrequest,
            HttpServletResponse httpservletresponse) throws ServletException,
            IOException {
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public final void initialize(ServletConfig servletconfig,
            HttpServletRequest httpservletrequest,
            HttpServletResponse httpservletresponse) throws ServletException {
        m_application = servletconfig.getServletContext();
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public final void initialize(PageContext pagecontext)
            throws ServletException {
        m_application = pagecontext.getServletContext();
        m_request = (HttpServletRequest) pagecontext.getRequest();
        m_response = (HttpServletResponse) pagecontext.getResponse();
    }

    public final void initialize(ServletContext servletcontext,
            HttpSession httpsession, HttpServletRequest httpservletrequest,
            HttpServletResponse httpservletresponse, JspWriter jspwriter)
            throws ServletException {
        m_application = servletcontext;
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public void downloadFile(String s) throws ServletException, IOException {
        downloadFile(s, null, null);
    }

    public void downloadFile(String s, String s1, String s2)
            throws ServletException, IOException {
        downloadFile(s, s1, s2, 65000);
    }

    public void downloadFile(String s, String s1, String s2, int i)
            throws ServletException, IOException {
        if (s == null)
            throw new IllegalArgumentException("File '" + s
                    + "' not found (1040).");
        if (s.equals(""))
            throw new IllegalArgumentException("File '" + s
                    + "' not found (1040).");
        if (!isVirtual(s) && m_denyPhysicalPath)
            throw new SecurityException("Physical path is denied (1035).");
        if (isVirtual(s))
            s = m_application.getRealPath(s);
        File file = new File(s);
        FileInputStream fileinputstream = new FileInputStream(file);
        long l = file.length();
        boolean flag = false;
        int k = 0;
        byte abyte0[] = new byte[i];
        if (s1 == null)
            m_response.setContentType("nothing");
        else if (s1.length() == 0)
            m_response.setContentType("nothing");
        else
            m_response.setContentType(s1);
        m_response.setContentLength((int) l);
        m_contentDisposition = m_contentDisposition == null ? "attachment;"
                : m_contentDisposition;
        if (s2 == null)
            m_response.setHeader("Content-Disposition", m_contentDisposition
                    + "filename=" + getFileName(s));
        else if (s2.length() == 0)
            m_response.setHeader("Content-Disposition", m_contentDisposition);
        else
            m_response.setHeader("Content-Disposition", m_contentDisposition
                    + " filename=" + s2);
        while ((long) k < l) {
            int j = fileinputstream.read(abyte0, 0, i);
            k += j;
            m_response.getOutputStream().write(abyte0, 0, j);
        }
        fileinputstream.close();
        if (file.exists()) {
            file.delete();
            System.out.println("File " + file.getPath()
                    + "  has been deleted!!!!");
        }
    }

    private String getFileName(String s) {
        String s1 = new String();
        String s2 = new String();
        int i = 0;
        boolean flag = false;
        boolean flag1 = false;
        boolean flag2 = false;
        i = s.lastIndexOf(47);
        if (i != -1)
            return s.substring(i + 1, s.length());
        i = s.lastIndexOf(92);
        if (i != -1)
            return s.substring(i + 1, s.length());
        else
            return s;
    }

    private boolean isVirtual(String s) {
        if (m_application.getRealPath(s) != null) {
            File file = new File(m_application.getRealPath(s));
            return file.exists();
        } else {
            return false;
        }
    }
} 

⌨️ 快捷键说明

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