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

📄 upload.java

📁 JSP聊天系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.ehotsoft.yekki.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;


public class Upload
{

    protected byte binArray[];
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected ServletContext application;
    private int totalBytes;
    private int currentIndex;
    private int startData;
    private int endData;
    private String boundary;
    private long totalMaxFileSize;
    private long maxFileSize;
    private Vector deniedFilesList;
    private Vector allowedFilesList;
    private boolean denyPhysicalPath;
    private boolean forcePhysicalPath;
    private String contentDisposition;
    public static final int SAVE_AUTO = 0;
    public static final int SAVE_VIRTUAL = 1;
    public static final int SAVE_PHYSICAL = 2;
    private Files files;
    private Request formRequest;

    public Upload()
    {
        this.totalBytes = 0;
        this.currentIndex = 0;
        this.startData = 0;
        this.endData = 0;
        this.boundary = new String();
        this.totalMaxFileSize = 0L;
        this.maxFileSize = 0L;
        this.deniedFilesList = new Vector();
        this.allowedFilesList = new Vector();
        this.denyPhysicalPath = false;
        this.forcePhysicalPath = false;
        this.contentDisposition = new String();
        this.files = new Files();
        this.formRequest = new Request();
    }

    public final void init(ServletConfig config)
        throws ServletException
    {
        this.application = config.getServletContext();
    }

    public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        this.request = request;
        this.response = response;
    }

    public final void initialize(ServletConfig config, HttpServletRequest request, HttpServletResponse response)
        throws ServletException
    {
        this.application = config.getServletContext();
        this.request = request;
        this.response = response;
    }

    public final void initialize(PageContext pageContext)
        throws ServletException
    {
        this.application = pageContext.getServletContext();
        this.request = (HttpServletRequest)pageContext.getRequest();
        this.response = (HttpServletResponse)pageContext.getResponse();
    }

    public final void initialize(ServletContext application, HttpSession session, HttpServletRequest request, HttpServletResponse response, JspWriter out)
        throws ServletException
    {
        this.application = application;
        this.request = request;
        this.response = response;
    }

    public void upload()
        throws UploadException, 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;
        this.totalBytes = this.request.getContentLength();
        this.binArray = new byte[this.totalBytes];
        for(; totalRead < this.totalBytes; totalRead += readBytes)
            try
            {
                this.request.getInputStream();
                readBytes = this.request.getInputStream().read(this.binArray, totalRead, this.totalBytes - totalRead);
            }
            catch(Exception e)
            {
                throw new UploadException("Unable to upload.");
            }

        for(; !found && this.currentIndex < this.totalBytes; this.currentIndex++)
            if(this.binArray[this.currentIndex] == 13)
                found = true;
            else
                this.boundary = this.boundary + (char)this.binArray[this.currentIndex];

        if(this.currentIndex == 1)
            return;
        this.currentIndex++;
        do
        {
            if(this.currentIndex >= this.totalBytes)
                break;
            dataHeader = getDataHeader();
            this.currentIndex = this.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(this.deniedFilesList.contains(fileExt))
                    throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
                if(!this.allowedFilesList.isEmpty() && !this.allowedFilesList.contains(fileExt))
                    throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
                if(this.maxFileSize > (long)0 && (long)((this.endData - this.startData) + 1) > this.maxFileSize)
                    throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
                totalFileSize += (this.endData - this.startData) + 1;
                if(this.totalMaxFileSize > (long)0 && totalFileSize > this.totalMaxFileSize)
                    throw new SecurityException("Total java.io.File Size exceeded (1110).");
            }
            if(isFile)
            {
                org.ehotsoft.yekki.upload.File newFile = new org.ehotsoft.yekki.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)
                    this.startData = this.startData + 128;
                newFile.setSize((this.endData - this.startData) + 1);
                newFile.setStartData(this.startData);
                newFile.setEndData(this.endData);
                this.files.addFile(newFile);
            } else
            {
                String value = new String(this.binArray, this.startData, (this.endData - this.startData) + 1);
                this.formRequest.putParameter(fieldName, value);
            }
            if((char)this.binArray[this.currentIndex + 1] == '-')
                break;
            this.currentIndex = this.currentIndex + 2;
        } while(true);
    }

    public int save(String destPathName)
        throws UploadException, IOException, ServletException
    {
        return save(destPathName, 0);
    }

    public int save(String destPathName, int option)
        throws UploadException, IOException, ServletException
    {
        int count = 0;
        if(destPathName == null)
            destPathName = this.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 < this.files.getCount(); i++)
            if(!this.files.getFile(i).isMissing())
            {
                this.files.getFile(i).saveAs(destPathName + this.files.getFile(i).getFileName(), option);
                count++;
            }

        return count;
    }

    public int getSize()
    {
        return this.totalBytes;
    }

    public byte getBinaryData(int index)
    {
        byte retval;
        try
        {
            retval = this.binArray[index];
        }
        catch(Exception e)
        {
            throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
        }
        return retval;
    }

    public Files getFiles()
    {
        return this.files;
    }

    public Request getRequest()
    {
        return this.formRequest;
    }

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

    public void downloadFile(String sourceFilePathName, String contentType)
        throws UploadException, IOException, ServletException
    {
        downloadFile(sourceFilePathName, contentType, null);
    }

    public void downloadFile(String sourceFilePathName, String contentType, String destFileName)
        throws UploadException, IOException, ServletException
    {
        downloadFile(sourceFilePathName, contentType, destFileName, 65000);
    }

    public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize)
        throws UploadException, IOException, ServletException
    {
        if(sourceFilePathName == null)
            throw new IllegalArgumentException(String.valueOf((new StringBuffer("java.io.File '")).append(sourceFilePathName).append("' not found (1040).")));
        if(sourceFilePathName.equals(""))
            throw new IllegalArgumentException(String.valueOf((new StringBuffer("java.io.File '")).append(sourceFilePathName).append("' not found (1040).")));
        if(!isVirtual(sourceFilePathName) && this.denyPhysicalPath)
            throw new SecurityException("Physical path is denied (1035).");
        if(isVirtual(sourceFilePathName))
            sourceFilePathName = this.application.getRealPath(sourceFilePathName);
        java.io.File file = new java.io.File(sourceFilePathName);
        FileInputStream fileIn = new FileInputStream(file);
        long fileLen = file.length();
        int readBytes = 0;
        int totalRead = 0;
        byte b[] = new byte[blockSize];
        if(contentType == null)
            this.response.setContentType("application/x-msdownload");
        else
        if(contentType.length() == 0)
            this.response.setContentType("application/x-msdownload");
        else
            this.response.setContentType(contentType);
        this.response.setContentLength((int)fileLen);
        this.contentDisposition = this.contentDisposition != null ? this.contentDisposition : "attachment;";
        if(destFileName == null)
            this.response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(this.contentDisposition))).append(" filename=").append(getFileName(sourceFilePathName))));
        else
        if(destFileName.length() == 0)
            this.response.setHeader("Content-Disposition", this.contentDisposition);
        else
            this.response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(this.contentDisposition))).append(" filename=").append(destFileName)));
        while((long)totalRead < fileLen) 
        {
            readBytes = fileIn.read(b, 0, blockSize);
            totalRead += readBytes;
            this.response.getOutputStream().write(b, 0, readBytes);
        }
        fileIn.close();
    }

    public void downloadField(ResultSet rs, String columnName, String contentType, String destFileName)
        throws SQLException, IOException, ServletException
    {
        if(rs == null)
            throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
        if(columnName == null)
            throw new IllegalArgumentException("The columnName cannot be null (1050).");
        if(columnName.length() == 0)
            throw new IllegalArgumentException("The columnName cannot be empty (1055).");
        byte b[] = rs.getBytes(columnName);
        if(contentType == null)
            this.response.setContentType("application/x-msdownload");
        else
        if(contentType.length() == 0)
            this.response.setContentType("application/x-msdownload");
        else
            this.response.setContentType(contentType);
        this.response.setContentLength(b.length);
        if(destFileName == null)
            this.response.setHeader("Content-Disposition", "attachment;");
        else
        if(destFileName.length() == 0)
            this.response.setHeader("Content-Disposition", "attachment;");
        else
            this.response.setHeader("Content-Disposition", "attachment; filename=".concat(String.valueOf(destFileName)));
        this.response.getOutputStream().write(b, 0, b.length);
    }

    public void fieldToFile(ResultSet rs, String columnName, String destFilePathName)
        throws SQLException, UploadException, IOException, ServletException
    {
        try
        {
            if(this.application.getRealPath(destFilePathName) != null)
                destFilePathName = this.application.getRealPath(destFilePathName);
            InputStream is_data = rs.getBinaryStream(columnName);
            FileOutputStream file = new FileOutputStream(destFilePathName);
            int c;
            while((c = is_data.read()) != -1) 
                file.write(c);
            file.close();
        }
        catch(Exception e)
        {
            throw new UploadException("Unable to save file from the DataBase (1020).");
        }
    }

    private String getDataFieldValue(String dataHeader, String fieldName)
    {
        String token = new String();
        String value = new String();
        int pos = 0;
        int i = 0;

⌨️ 快捷键说明

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