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

📄 fileuploadbase.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                OutputStream os = item.getOutputStream();                                try {                                    multi.readBodyData(os);                                } finally {                                    os.close();                                }                                items.add(item);                            } else {                                // Ignore anything but files inside                                // multipart/mixed.                                multi.discardBodyData();                            }                            nextSubPart = multi.readBoundary();                        }                        multi.setBoundary(boundary);                    } else {                        FileItem item = createItem(headers,                                getFileName(headers) == null);                        OutputStream os = item.getOutputStream();                        try {                            multi.readBodyData(os);                        } finally {                            os.close();                        }                        items.add(item);                    }                } else {                    // Skip this part.                    multi.discardBodyData();                }                nextPart = multi.readBoundary();            }        } catch (IOException e) {            throw new FileUploadException(                "Processing of " + MULTIPART_FORM_DATA                    + " request failed. " + e.getMessage());        }        return items;    }    // ------------------------------------------------------ Protected methods    /**     * Retrieves the boundary from the <code>Content-type</code> header.     *     * @param contentType The value of the content type header from which to     *                    extract the boundary value.     *     * @return The boundary, as a byte array.     */    protected byte[] getBoundary(String contentType) {        ParameterParser parser = new ParameterParser();        parser.setLowerCaseNames(true);        // Parameter parser can handle null input        Map params = parser.parse(contentType, ';');        String boundaryStr = (String) params.get("boundary");        if (boundaryStr == null) {            return null;        }        byte[] boundary;        try {            boundary = boundaryStr.getBytes("ISO-8859-1");        } catch (UnsupportedEncodingException e) {            boundary = boundaryStr.getBytes();        }        return boundary;    }    /**     * Retrieves the file name from the <code>Content-disposition</code>     * header.     *     * @param headers A <code>Map</code> containing the HTTP request headers.     *     * @return The file name for the current <code>encapsulation</code>.     */    protected String getFileName(Map /* String, String */ headers) {        String fileName = null;        String cd = getHeader(headers, CONTENT_DISPOSITION);        if (cd != null) {            String cdl = cd.toLowerCase();            if (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) {                ParameterParser parser = new ParameterParser();                parser.setLowerCaseNames(true);                // Parameter parser can handle null input                Map params = parser.parse(cd, ';');                if (params.containsKey("filename")) {                    fileName = (String) params.get("filename");                    if (fileName != null) {                        fileName = fileName.trim();                    } else {                        // Even if there is no value, the parameter is present,                        // so we return an empty file name rather than no file                        // name.                        fileName = "";                    }                }            }        }        return fileName;    }    /**     * Retrieves the field name from the <code>Content-disposition</code>     * header.     *     * @param headers A <code>Map</code> containing the HTTP request headers.     *     * @return The field name for the current <code>encapsulation</code>.     */    protected String getFieldName(Map /* String, String */ headers) {        String fieldName = null;        String cd = getHeader(headers, CONTENT_DISPOSITION);        if (cd != null && cd.toLowerCase().startsWith(FORM_DATA)) {            ParameterParser parser = new ParameterParser();            parser.setLowerCaseNames(true);            // Parameter parser can handle null input            Map params = parser.parse(cd, ';');            fieldName = (String) params.get("name");            if (fieldName != null) {                fieldName = fieldName.trim();            }        }        return fieldName;    }    /**     * Creates a new {@link FileItem} instance.     *     * @param headers       A <code>Map</code> containing the HTTP request     *                      headers.     * @param isFormField   Whether or not this item is a form field, as     *                      opposed to a file.     *     * @return A newly created <code>FileItem</code> instance.     *     * @throws FileUploadException if an error occurs.     */    protected FileItem createItem(Map /* String, String */ headers,                                  boolean isFormField)        throws FileUploadException {        return getFileItemFactory().createItem(getFieldName(headers),                getHeader(headers, CONTENT_TYPE),                isFormField,                getFileName(headers));    }    /**     * <p> Parses the <code>header-part</code> and returns as key/value     * pairs.     *     * <p> If there are multiple headers of the same names, the name     * will map to a comma-separated list containing the values.     *     * @param headerPart The <code>header-part</code> of the current     *                   <code>encapsulation</code>.     *     * @return A <code>Map</code> containing the parsed HTTP request headers.     */    protected Map /* String, String */ parseHeaders(String headerPart) {        Map headers = new HashMap();        char[] buffer = new char[MAX_HEADER_SIZE];        boolean done = false;        int j = 0;        int i;        String header, headerName, headerValue;        try {            while (!done) {                i = 0;                // Copy a single line of characters into the buffer,                // omitting trailing CRLF.                while (i < 2                        || buffer[i - 2] != '\r' || buffer[i - 1] != '\n') {                    buffer[i++] = headerPart.charAt(j++);                }                header = new String(buffer, 0, i - 2);                if (header.equals("")) {                    done = true;                } else {                    if (header.indexOf(':') == -1) {                        // This header line is malformed, skip it.                        continue;                    }                    headerName = header.substring(0, header.indexOf(':'))                        .trim().toLowerCase();                    headerValue =                        header.substring(header.indexOf(':') + 1).trim();                    if (getHeader(headers, headerName) != null) {                        // More that one heder of that name exists,                        // append to the list.                        headers.put(headerName,                                    getHeader(headers, headerName) + ','                                        + headerValue);                    } else {                        headers.put(headerName, headerValue);                    }                }            }        } catch (IndexOutOfBoundsException e) {            // Headers were malformed. continue with all that was            // parsed.        }        return headers;    }    /**     * Returns the header with the specified name from the supplied map. The     * header lookup is case-insensitive.     *     * @param headers A <code>Map</code> containing the HTTP request headers.     * @param name    The name of the header to return.     *     * @return The value of specified header, or a comma-separated list if     *         there were multiple headers of that name.     */    protected final String getHeader(Map /* String, String */ headers,                                     String name) {        return (String) headers.get(name.toLowerCase());    }    /**     * Thrown to indicate that the request is not a multipart request.     */    public static class InvalidContentTypeException        extends FileUploadException {        /**         * Constructs a <code>InvalidContentTypeException</code> with no         * detail message.         */        public InvalidContentTypeException() {            super();        }        /**         * Constructs an <code>InvalidContentTypeException</code> with         * the specified detail message.         *         * @param message The detail message.         */        public InvalidContentTypeException(String message) {            super(message);        }    }    /**     * Thrown to indicate that the request size is not specified.     */    public static class UnknownSizeException        extends FileUploadException {        /**         * Constructs a <code>UnknownSizeException</code> with no         * detail message.         */        public UnknownSizeException() {            super();        }        /**         * Constructs an <code>UnknownSizeException</code> with         * the specified detail message.         *         * @param message The detail message.         */        public UnknownSizeException(String message) {            super(message);        }    }    /**     * Thrown to indicate that the request size exceeds the configured maximum.     */    public static class SizeLimitExceededException        extends FileUploadException {        /**         * The actual size of the request.         */        private long actual;        /**         * The maximum permitted size of the request.         */        private long permitted;        /**         * Constructs a <code>SizeExceededException</code> with no         * detail message.         */        public SizeLimitExceededException() {            super();        }        /**         * Constructs a <code>SizeExceededException</code> with         * the specified detail message.         *         * @param message The detail message.         */        public SizeLimitExceededException(String message) {            super(message);        }        /**         * Constructs a <code>SizeExceededException</code> with         * the specified detail message, and actual and permitted sizes.         *         * @param message   The detail message.         * @param actual    The actual request size.         * @param permitted The maximum permitted request size.         */        public SizeLimitExceededException(String message, long actual,                long permitted) {            super(message);            this.actual = actual;            this.permitted = permitted;        }        /**         * Retrieves the actual size of the request.         *         * @return The actual size of the request.         */        public long getActualSize() {            return actual;        }        /**         * Retrieves the permitted size of the request.         *         * @return The permitted size of the request.         */        public long getPermittedSize() {            return permitted;        }    }}

⌨️ 快捷键说明

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