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

📄 smartupload.java

📁 大象购物系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.comm.upload;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public class SmartUpload {
    /*
        // 新建一个SmartUpload对象
        SmartUpload su = new SmartUpload();
        // 上传初始化
        su.initialize(request.getSession().getServletContext(), (HttpServletRequest)request, (HttpServletResponse)response);
        // 设定上传限制
        // 1.限制每个上传文件的最大长度。
        // su.setMaxFileSize(10000);
        // 2.限制总上传数据的长度。
        // su.setTotalMaxFileSize(20000);
        // 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
        su.setAllowedFilesList("jpg");
        // 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp, htm, html扩展名的文件和没有扩展名的文件 。
        // su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
        // 上传文件
        su.upload();
        // 将上传文件全部保存到指定目录
        int count = su.save("/images/photo");

        System.out.println(count + "个文件上传成功!<br>");

        ImgUtil util = new ImgUtil("H:\\82 JBProjects\\BadmintonNety\\BadmintonNety\\images\\goods\\20061102121127.jpg");
        util.makeThumbnail(getServlet().getServletContext().getRealPath("/") + "/images/photo/test.jpg", 140, 140);

        // 利用Request对象获取参数之值
     System.out.println("TEST=" + su.getRequest().getParameter("TEST") + "<BR><BR>");

        // 逐一提取上传文件信息,同时可保存文件。
        for (int i = 0; i < su.getFiles().getCount(); i++) {
            SmartFile file = su.getFiles().getFile(i);

            // 若文件不存在则继续
            if (file.isMissing())continue;

            // 显示当前文件信息
            System.out.println("<TABLE BORDER=1>");
            System.out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
                        + file.getFieldName() + "</TD></TR>");
            System.out.println("<TR><TD>文件长度(Size)</TD><TD>" +
                        file.getSize() + "</TD></TR>");
            System.out.println("<TR><TD>文件名(FileName)</TD><TD>"
                        + file.getFileName() + "</TD></TR>");
            System.out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>"
                        + file.getFileExt() + "</TD></TR>");
            System.out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
                        + file.getFilePathName() + "</TD></TR>");
            System.out.println("</TABLE><BR>");

            // 将文件另存
            // file.saveAs("/upload/" + myFile.getFileName());
            // 另存到以WEB应用程序的根目录为文件根目录的目录下
            // file.saveAs("/upload/" + myFile.getFileName(),su.SAVE_VIRTUAL);
            // 另存到操作系统的根目录为文件根目录的目录下
            // file.saveAs("c:\\temp\\" + myFile.getFileName(),su.SAVE_PHYSICAL);

        }
     */
    protected byte m_binArray[];
    protected HttpServletRequest m_request;
    protected HttpServletResponse m_response;
    protected ServletContext m_application;
    private int m_totalBytes;
    private int m_currentIndex;
    private int m_startData;
    private int m_endData;
    private String m_boundary;
    private long m_totalMaxFileSize;
    private long m_maxFileSize;
    private Vector m_deniedFilesList;
    private Vector m_allowedFilesList;
    private boolean m_denyPhysicalPath;

    //private boolean m_forcePhysicalPath;
    private String m_contentDisposition;
    public static final int SAVE_AUTO = 0;
    public static final int SAVE_VIRTUAL = 1;
    public static final int SAVE_PHYSICAL = 2;
    private SmartFiles m_files;
    private SmartRequest m_formRequest;

    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 SmartFiles();
        m_formRequest = new SmartRequest();
    }

    /**
     * @deprecated Method init is deprecated
     */
    public final void init(ServletConfig servletconfig) throws ServletException {
        m_application = servletconfig.getServletContext();
    }

    /**
     * @deprecated Method service is deprecated
     */
    public void service(HttpServletRequest httpservletrequest,
                        HttpServletResponse httpservletresponse) throws
            ServletException, IOException {
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public final void initialize(ServletContext servletcontext,
                                 HttpServletRequest httpservletrequest,
                                 HttpServletResponse httpservletresponse) throws
            ServletException {
        m_application = servletcontext;
        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();
    }

    /**
     * @deprecated Method initialize is deprecated
     */
    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;
        boolean flag1 = false;
        //boolean flag2 = false;
        long l = 0L;
        //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();
        m_totalBytes = m_request.getContentLength();
        m_binArray = new byte[m_totalBytes];
        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("对不起,只允许上传JPG格式的图片!");
                }
                if (!m_allowedFilesList.isEmpty() &&
                    !m_allowedFilesList.contains(s5)) {
                    throw new SecurityException(
                            "只对不起,允许上传JPG格式的图片!");
                }
                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) {
                SmartFile file = new SmartFile();
                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, "UTF-8");
                m_formRequest.putParameter(s3, s11);
            }
            if ((char) m_binArray[m_currentIndex + 1] == '-') {
                break;
            }
        }
    }

    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("/");
            //System.out.println("s == null,m_application.getRealPath:" + s);
        }
        if (s.indexOf("/") != -1) {
            if (s.charAt(s.length() - 1) != '/') {
                s = s + "/";
                //System.out.println("m_application.getRealPath::" + s);
            }
        } else {
            if (s.charAt(s.length() - 1) != '\\') {
                s = s + "\\";
                //System.out.println("m_application.getRealPath" + s);
            }
        }
        //System.out.println("m_application.getRealPath:::" + s);
        FileNames = new String[m_files.getCount()];
        for (int k = 0; k < m_files.getCount(); k++) {
            if (!m_files.getFile(k).isMissing()) {
                //System.out.println("s + m_files.getFile(k).getFileName():" + s + m_files.getFile(k).getFileName());
                String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new
                        java.sql.Date(System.currentTimeMillis()));
                fileName += "." + m_files.getFile(k).getFileExt();
                m_files.getFile(k).saveAs(s + fileName,
                                          i);
                FileNames[j] = s + fileName;
                j++;
            }
        }

        return j;
    }

    //Add
    private String[] FileNames;
    public String[] getFileNames() {
        //Method may expose internal representation by returning array
        //Returning an array value stored in one of the object's fields exposes the internal representation of the object.? For classes shared by other untrusted classes, this could potentially be a security issue.? Returning a new copy of the array is better approach in many situations.
        String[] vFileNames = new String[FileNames.length];
        System.arraycopy(FileNames, 0, vFileNames, 0, FileNames.length);
        return vFileNames;
    }

    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 SmartFiles getFiles() {
        return m_files;
    }

    public SmartRequest 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,

⌨️ 快捷键说明

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