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

📄 smartupload.java

📁 “JSP数据库项目案例导航”一书从第一章到第十一章各章实例的源程序文件以及数据库文件。 注意: 1. 本书中的案例提供的数据库环境不同
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////
// DeJaved by mDeJava v1.0. Copyright 1999 MoleSoftware. //
//       To download last version of this software:      //
//            http://molesoftware.hypermatr.net          //
//               e-mail:molesoftware@mail.ru             //
///////////////////////////////////////////////////////////

package com.jspsmart.upload;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

// Referenced classes of package com.jspsmart.upload:
//            Files, Request, SmartUploadException, File

public class SmartUpload
{

    protected byte m_binArray[] = null;
    protected HttpServletRequest m_request = null;
    protected HttpServletResponse m_response = null;
    protected ServletContext m_application = null;
    private int m_totalBytes = 0;
    private int m_currentIndex = 0;
    private int m_startData = 0;
    private int m_endData = 0;
    private String m_boundary = null;
    private long m_totalMaxFileSize = 0;
    private long m_maxFileSize = 0;
    private Vector m_deniedFilesList = null;
    private Vector m_allowedFilesList = null;
    private boolean m_denyPhysicalPath = false;
    private boolean m_forcePhysicalPath = false;
    private String m_contentDisposition = null;
    public static final int SAVE_AUTO = 0;
    public static final int SAVE_VIRTUAL = 1;
    public static final int SAVE_PHYSICAL = 2;
    private Files m_files = null;
    private Request m_formRequest = null;

    public SmartUpload()
    {
        m_totalBytes = 0;
        m_currentIndex = 0;
        m_startData = 0;
        m_endData = 0;
        m_boundary = new String();
        m_totalMaxFileSize = 0L;
        m_maxFileSize = 0L;
        m_deniedFilesList = new Vector();
        m_allowedFilesList = new Vector();
        m_denyPhysicalPath = false;
        m_forcePhysicalPath = false;
        m_contentDisposition = new String();
        m_files = new Files();
        m_formRequest = new Request();
    }

    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 upload()
        throws ServletException, IOException, SmartUploadException
    {
        int i = 0;
        boolean flag = false;
        long l = 0L;
        boolean flag1 = false;
        String s = new String();
        String s2 = new String();
        String s4 = new String();
        String s5 = new String();
        String s6 = new String();
        String s7 = new String();
        String s8 = new String();
        String s9 = new String();
        String s10 = new String();
        boolean flag2 = false;
        m_totalBytes = m_request.getContentLength();

		/////////////////////////////////////////////////////////paoding修改上传类
		try
		{ 
			if(m_totalBytes>55184652 || m_totalBytes<0)    //55184652相当于52.6M 
			{ 
				throw new SmartUploadException("the file is too large to upload."); 
			} 
			else 
			{ 
				m_binArray = new byte[m_totalBytes]; 
			
		////////////////////////////////////////////////////////paoding修改上传类


        int j;
        for(; i < m_totalBytes; i += j)
            try
            {
                m_request.getInputStream();
                j = m_request.getInputStream().read(m_binArray, i, m_totalBytes - i);
            }
            catch(Exception exception)
            {
                throw new SmartUploadException("Unable to upload.");
            }

        for(; !flag1 && m_currentIndex < m_totalBytes; m_currentIndex++)
            if(m_binArray[m_currentIndex] == 13)
                flag1 = true;
            else
                m_boundary = m_boundary + (char)m_binArray[m_currentIndex];

        if(m_currentIndex == 1)
            return;
        for(m_currentIndex++; m_currentIndex < m_totalBytes; m_currentIndex = m_currentIndex + 2)
        {
            String s1 = getDataHeader();
            m_currentIndex = m_currentIndex + 2;
            boolean flag3 = s1.indexOf("filename") > 0;
            String s3 = getDataFieldValue(s1, "name");
            if(flag3)
            {
                s6 = getDataFieldValue(s1, "filename");
                s4 = getFileName(s6);
                s5 = getFileExt(s4);
                s7 = getContentType(s1);
                s8 = getContentDisp(s1);
                s9 = getTypeMIME(s7);
                s10 = getSubTypeMIME(s7);
            }
            getDataSection();
            if(flag3 && s4.length() > 0)
            {
                if(m_deniedFilesList.contains(s5))
                    throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
                if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(s5))
                    throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
                if(m_maxFileSize > 0L && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
                    throw new SecurityException("Size exceeded for this file : " + s4 + " (1105).");
                l += (m_endData - m_startData) + 1;
                if(m_totalMaxFileSize > 0L && l > m_totalMaxFileSize)
                    throw new SecurityException("Total File Size exceeded (1110).");
            }
            if(flag3)
            {
                com.jspsmart.upload.File file = new com.jspsmart.upload.File();
                file.setParent(this);
                file.setFieldName(s3);
                file.setFileName(s4);
                file.setFileExt(s5);
                file.setFilePathName(s6);
                file.setIsMissing(s6.length() == 0);
                file.setContentType(s7);
                file.setContentDisp(s8);
                file.setTypeMIME(s9);
                file.setSubTypeMIME(s10);
                if(s7.indexOf("application/x-macbinary") > 0)
                    m_startData = m_startData + 128;
                file.setSize((m_endData - m_startData) + 1);
                file.setStartData(m_startData);
                file.setEndData(m_endData);
                m_files.addFile(file);
            }
            else
            {
                String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
                m_formRequest.putParameter(s3, s11);
            }
            if((char)m_binArray[m_currentIndex + 1] == '-')
                break;
        }
		///////////////////////////////////////////paoding修改上传类
			}
		} 
		catch(Exception e){m_binArray=null;} 
		catch(Error err){m_binArray=null;} 
		finally{  } 
		/////////////////////////////////////////////////////////paoding修改上传类

    }

	///////////////////////////////////////////////paoding修改上传类
	public void stop()
	{
		try
		{ 
			m_binArray=null;  
		} 
		catch(Exception e) 
		{ 
				//System.out.println(e.toString());  
		} 

	}

    public int save(String s)
        throws ServletException, IOException, SmartUploadException
    {
        return save(s, 0);
    }

    public int save(String s, int i)
        throws ServletException, IOException, SmartUploadException
    {
        int j = 0;
        if(s == null)
            s = m_application.getRealPath("/");
        if(s.indexOf("/") != -1)
        {
            if(s.charAt(s.length() - 1) != '/')
                s = s + "/";
        }
        else
        if(s.charAt(s.length() - 1) != '\\')
            s = s + "\\";
        for(int k = 0; k < m_files.getCount(); k++)
            if(!m_files.getFile(k).isMissing())
            {
                m_files.getFile(k).saveAs(s + m_files.getFile(k).getFileName(), i);
                j++;
            }

        return j;
    }

    public int getSize()
    {
        return m_totalBytes;
    }

    public byte getBinaryData(int i)
    {
        byte byte0;
        try
        {
            byte0 = m_binArray[i];
        }
        catch(Exception exception)
        {
            throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
        }
        return byte0;
    }

    public Files getFiles()
    {
        return m_files;
    }

    public Request getRequest()
    {
        return m_formRequest;
    }

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

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

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

    public void downloadFile(String s, String s1, String s2, int i)
        throws ServletException, IOException, SmartUploadException
    {
        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);
        java.io.File file = new java.io.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("application/x-msdownload");
        else
        if(s1.length() == 0)
            m_response.setContentType("application/x-msdownload");
        else
            m_response.setContentType(s1);
        m_response.setContentLength((int)l);
        m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
        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();
    }

    public void downloadField(ResultSet resultset, String s, String s1, String s2)
        throws ServletException, IOException, SQLException
    {
        if(resultset == null)
            throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
        if(s == null)
            throw new IllegalArgumentException("The columnName cannot be null (1050).");
        if(s.length() == 0)
            throw new IllegalArgumentException("The columnName cannot be empty (1055).");
        byte abyte0[] = resultset.getBytes(s);
        if(s1 == null)
            m_response.setContentType("application/x-msdownload");
        else
        if(s1.length() == 0)
            m_response.setContentType("application/x-msdownload");
        else
            m_response.setContentType(s1);
        m_response.setContentLength(abyte0.length);
        if(s2 == null)
            m_response.setHeader("Content-Disposition", "attachment;");
        else
        if(s2.length() == 0)
            m_response.setHeader("Content-Disposition", "attachment;");
        else
            m_response.setHeader("Content-Disposition", "attachment; filename=" + s2);
        m_response.getOutputStream().write(abyte0, 0, abyte0.length);
    }

    public void fieldToFile(ResultSet resultset, String s, String s1)
        throws ServletException, IOException, SmartUploadException, SQLException
    {
        try
        {
            if(m_application.getRealPath(s1) != null)
                s1 = m_application.getRealPath(s1);
            InputStream inputstream = resultset.getBinaryStream(s);

⌨️ 快捷键说明

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