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

📄 smartupload.java

📁 jsp+servlet实现的标准MVC系统 jsp-servlet-service-DAO-DBMS
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    if (option == 1)

    {

        if (isVirtual(path))

        {

            path = m_application.getRealPath(path);

            if (path.endsWith(fileSeparator))

                path = path + fileName;

            else

                path =

                    String.valueOf(

                        (new StringBuffer(String.valueOf(path))).append(fileSeparator).append(

                            fileName));

            return path;

        }

        if (isPhysical)

            throw new IllegalArgumentException("The path is not a virtual path.");

        else

            throw new IllegalArgumentException("This path does not exist (1135).");

    }

    if (option == 2)

    {

        if (isPhysical)

            if (m_denyPhysicalPath)

                throw new IllegalArgumentException("Physical path is denied (1125).");

            else

                return filePathName;

        if (isVirtual(path))

            throw new IllegalArgumentException("The path is not a physical path.");

        else

            throw new IllegalArgumentException("This path does not exist (1135).");

    }

    else

    {

        return null;

    }

}





public Request getRequest()

{

    return m_formRequest;

}





public int getSize()

{

    return m_totalBytes;

}





private String getSubTypeMIME(String ContentType)

{

    String value = new String();

    int start = 0;

    int end = 0;

    start = ContentType.indexOf("/") + 1;

    if (start != -1)

    {

        end = ContentType.length();

        return ContentType.substring(start, end);

    }

    else

    {

        return ContentType;

    }

}





private String getTypeMIME(String ContentType)

{

    String value = new String();

    int pos = 0;

    pos = ContentType.indexOf("/");

    if (pos != -1)

        return ContentType.substring(1, pos);

    else

        return ContentType;

}





public final void init(ServletConfig config) throws ServletException

{

    m_application = config.getServletContext();

}





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(

    ServletConfig config,

    HttpServletRequest request,

    HttpServletResponse response)

    throws ServletException

{

    m_application = config.getServletContext();

    m_request = request;

    m_response = response;

}





public final void initialize(

    ServletContext application,

    HttpSession session,

    HttpServletRequest request,

    HttpServletResponse response,

    JspWriter out)

    throws ServletException

{

    m_application = application;

    m_request = request;

    m_response = response;

}





private boolean isVirtual(String pathName)

{

    if (m_application.getRealPath(pathName) != null)

    {

        java.io.File virtualFile =

            new java.io.File(m_application.getRealPath(pathName));

        return virtualFile.exists();

    }

    else

    {

        return false;

    }

}





public int save(String destPathName)

    throws SmartUploadException, IOException, ServletException

{

    return save(destPathName, 0);

}





public int save(String destPathName, int option)

    throws SmartUploadException, IOException, ServletException

{

    int count = 0;

    if (destPathName == null)

        destPathName = m_application.getRealPath("/");

    if (destPathName.indexOf("/") != -1)

    {

        if (destPathName.charAt(destPathName.length() - 1) != '/')

            destPathName = String.valueOf(destPathName).concat("/");

    }

    else if (destPathName.charAt(destPathName.length() - 1) != '\\')

        destPathName = String.valueOf(destPathName).concat("\\");

    for (int i = 0; i < m_files.getCount(); i++)

        if (!m_files.getFile(i).isMissing())

        {

            m_files.getFile(i).saveAs(

                destPathName + m_files.getFile(i).getFileName(),

                option);

            count++;

        }



    return count;

}





public void service(HttpServletRequest request, HttpServletResponse response)

    throws IOException, ServletException

{

    m_request = request;

    m_response = response;

}





public void setAllowedFilesList(String allowedFilesList)

{

    String ext = "";

    if (allowedFilesList != null)

    {

        ext = "";

        for (int i = 0; i < allowedFilesList.length(); i++)

            if (allowedFilesList.charAt(i) == ',')

            {

                if (!m_allowedFilesList.contains(ext))

                    m_allowedFilesList.addElement(ext);

                ext = "";

            }

            else

            {

                ext = ext + allowedFilesList.charAt(i);

            }



        if (ext != "")

            m_allowedFilesList.addElement(ext);

    }

    else

    {

        m_allowedFilesList = null;

    }

}





public void setContentDisposition(String contentDisposition)

{

    m_contentDisposition = contentDisposition;

}





public void setDeniedFilesList(String deniedFilesList)

    throws SQLException, IOException, ServletException

{

    String ext = "";

    if (deniedFilesList != null)

    {

        ext = "";

        for (int i = 0; i < deniedFilesList.length(); i++)

            if (deniedFilesList.charAt(i) == ',')

            {

                if (!m_deniedFilesList.contains(ext))

                    m_deniedFilesList.addElement(ext);

                ext = "";

            }

            else

            {

                ext = ext + deniedFilesList.charAt(i);

            }



        if (ext != "")

            m_deniedFilesList.addElement(ext);

    }

    else

    {

        m_deniedFilesList = null;

    }

}





public void setDenyPhysicalPath(boolean deny)

{

    m_denyPhysicalPath = deny;

}





public void setForcePhysicalPath(boolean force)

{

    m_forcePhysicalPath = force;

}





public void setMaxFileSize(long maxFileSize)

{

    m_maxFileSize = maxFileSize;

}





public void setTotalMaxFileSize(long totalMaxFileSize)

{

    m_totalMaxFileSize = totalMaxFileSize;

}





public void upload() throws SmartUploadException, IOException, ServletException

{

    int totalRead = 0;

    int readBytes = 0;

    long totalFileSize = 0L;

    boolean found = false;

    String dataHeader = new String();

    String fieldName = new String();

    String fileName = new String();

    String fileExt = new String();

    String filePathName = new String();

    String contentType = new String();

    String contentDisp = new String();

    String typeMIME = new String();

    String subTypeMIME = new String();

    boolean isFile = false;

    m_totalBytes = m_request.getContentLength();

    m_binArray = new byte[m_totalBytes];

    for (; totalRead < m_totalBytes; totalRead += readBytes)

        try

        {

            m_request.getInputStream();

            readBytes =

                m_request.getInputStream().read(

                    m_binArray,

                    totalRead,

                    m_totalBytes - totalRead);

        }

        catch (Exception e)

        {

            throw new SmartUploadException("Unable to upload.");

        }



    for (; !found && m_currentIndex < m_totalBytes; m_currentIndex++)

        if (m_binArray[m_currentIndex] == 13)

            found = true;

        else

            m_boundary = m_boundary + (char) m_binArray[m_currentIndex];



    if (m_currentIndex == 1)

        return;

    m_currentIndex++;

    do {

        if (m_currentIndex >= m_totalBytes)

            break;

        dataHeader = getDataHeader();

        m_currentIndex = m_currentIndex + 2;

        isFile = dataHeader.indexOf("filename") > 0;

        fieldName = getDataFieldValue(dataHeader, "name");

        if (isFile)

        {

            filePathName = getDataFieldValue(dataHeader, "filename");

            fileName = getFileName(filePathName);

            fileExt = getFileExt(fileName);

            contentType = getContentType(dataHeader);

            contentDisp = getContentDisp(dataHeader);

            typeMIME = getTypeMIME(contentType);

            subTypeMIME = getSubTypeMIME(contentType);

        }

        getDataSection();

        if (isFile && fileName.length() > 0)

        {

            if (m_deniedFilesList.contains(fileExt))

                throw new SecurityException("上传文件中含有服务器拒绝上传的文件类型 (1015).");

            if (!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))

                throw new SecurityException("上传文件中含有服务器不允许上传的文件类型 (1010).");

            if (m_maxFileSize > (long) 0

                && (long) ((m_endData - m_startData) + 1) > m_maxFileSize)

                throw new SecurityException(

                    String.valueOf(

                        (new StringBuffer("文件 ")).append(fileName).append(

                            " 大小超过服务器允许上传文件的最大值 (1105).")));

            totalFileSize += (m_endData - m_startData) + 1;

            if (m_totalMaxFileSize > (long) 0 && totalFileSize > m_totalMaxFileSize)

                throw new SecurityException("文件总大小超过服务器允许上传文件的最大值 (1110).");

        }

        if (isFile)

        {

            com.jspsmart.upload.File newFile = new com.jspsmart.upload.File();

            newFile.setParent(this);

            newFile.setFieldName(fieldName);

            newFile.setFileName(fileName);

            newFile.setFileExt(fileExt);

            newFile.setFilePathName(filePathName);

            newFile.setIsMissing(filePathName.length() == 0);

            newFile.setContentType(contentType);

            newFile.setContentDisp(contentDisp);

            newFile.setTypeMIME(typeMIME);

            newFile.setSubTypeMIME(subTypeMIME);

            if (contentType.indexOf("application/x-macbinary") > 0)

                m_startData = m_startData + 128;

            newFile.setSize((m_endData - m_startData) + 1);

            newFile.setStartData(m_startData);

            newFile.setEndData(m_endData);

            m_files.addFile(newFile);

        }

        else

        {

            String value =

                new String(m_binArray, m_startData, (m_endData - m_startData) + 1);

            m_formRequest.putParameter(fieldName, value);

        }

        if ((char) m_binArray[m_currentIndex + 1] == '-')

            break;

        m_currentIndex = m_currentIndex + 2;

    }

    while (true);

}





public void uploadInFile(String destFilePathName)

    throws SmartUploadException, IOException

{

    int intsize = 0;

    int pos = 0;

    int readBytes = 0;

    if (destFilePathName == null)

        throw new IllegalArgumentException("There is no specified destination file (1025).");

    if (destFilePathName.length() == 0)

        throw new IllegalArgumentException("There is no specified destination file (1025).");

    if (!isVirtual(destFilePathName) && m_denyPhysicalPath)

        throw new SecurityException("Physical path is denied (1035).");

    intsize = m_request.getContentLength();

    m_binArray = new byte[intsize];

    for (; pos < intsize; pos += readBytes)

        try

        {

            readBytes = m_request.getInputStream().read(m_binArray, pos, intsize - pos);

        }

        catch (Exception e)

        {

            throw new SmartUploadException("Unable to upload.");

        }



    if (isVirtual(destFilePathName))

        destFilePathName = m_application.getRealPath(destFilePathName);

    try

    {

        java.io.File file = new java.io.File(destFilePathName);

        FileOutputStream fileOut = new FileOutputStream(file);

        fileOut.write(m_binArray);

        fileOut.close();

    }

    catch (Exception e)

    {

        throw new SmartUploadException("The Form cannot be saved in the specified file (1030).");

    }

}

}

⌨️ 快捷键说明

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