package zfileupload;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.util.Closeable;
import org.apache.commons.fileupload.util.Streams;
/**
* <p> Low level API for processing file uploads.
*
* <p> This class can be used to process data streams conforming to MIME
* 'multipart' format as defined in
* <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Arbitrarily
* large amounts of data in the stream can be processed under constant
* memory usage.
*
* <p> The format of the stream is defined in the following way:<br>
*
* <code>
* multipart-body := preamble 1*encapsulation close-delimiter epilogue<br>
* encapsulation := delimiter body CRLF<br>
* delimiter := "--" boundary CRLF<br>
* close-delimiter := "--" boudary "--"<br>
* preamble := <ignore><br>
* epilogue := <ignore><br>
* body := header-part CRLF body-part<br>
* header-part := 1*header CRLF<br>
* header := header-name ":" header-value<br>
* header-name := <printable ascii characters except ":"><br>
* header-value := <any ascii characters except CR & LF><br>
* body-data := <arbitrary data><br>
* </code>
*
* <p>Note that body-data can contain another mulipart entity. There
* is limited support for single pass processing of such nested
* streams. The nested stream is <strong>required</strong> to have a
* boundary token of the same length as the parent stream (see {@link
* #setBoundary(byte[])}).
*
* <p>Here is an example of usage of this class.<br>
*
* <pre>
* try {
* MultipartStream multipartStream = new MultipartStream(input, boundary);
* boolean nextPart = multipartStream.skipPreamble();
* OutputStream output;
* while(nextPart) {
* String header = multipartStream.readHeaders();
* // process headers
* // create some output stream
* multipartStream.readBodyData(output);
* nextPart = multipartStream.readBoundary();
* }
* } catch(MultipartStream.MalformedStreamException e) {
* // the stream failed to follow required syntax
* } catch(IOException e) {
* // a read or write error occurred
* }
* </pre>
*
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
* @author Sean C. Sullivan
*
* @version $Id: MultipartStream.java 735374 2009-01-18 02:18:45Z jochen $
*/
public class MultipartStream {
/**
* Internal class, which is used to invoke the
* {@link ProgressListener}.
*/
public static class ProgressNotifier {
/** The listener to invoke.
*/
private final ProgressListener listener;
/** Number of expected bytes, if known, or -1.
*/
private final long contentLength;
/** Number of bytes, which have been read so far.
*/
private long bytesRead;
/** Number of items, which have been read so far.
*/
private int items;
/** Creates a new instance with the given listener
* and content length.
* @param pListener The listener to invoke.
* @param pContentLength The expected content length.
*/
ProgressNotifier(ProgressListener pListener, long pContentLength) {
listener = pListener;
contentLength = pContentLength;
}
/** Called to indicate that bytes have been read.
* @param pBytes Number of bytes, which have been read.
*/
void noteBytesRead(int pBytes) {
/* Indicates, that the given number of bytes have been read from
* the input stream.
*/
bytesRead += pBytes;
notifyListener();
}
/** Called to indicate, that a new file item has been detected.
*/
void noteItem() {
++items;
notifyListener();
}
/** Called for notifying the listener.
*/
private void notifyListener() {
if (listener != null) {
listener.update(bytesRead, contentLength, items);
}
}
}
// ----------------------------------------------------- Manifest constants
/**
* The Carriage Return ASCII character value.
*/
public static final byte CR = 0x0D;
/**
* The Line Feed ASCII character value.
*/
public static final byte LF = 0x0A;
/**
* The dash (-) ASCII character value.
*/
public static final byte DASH = 0x2D;
/**
* The maximum length of <code>header-part</code> that will be
* processed (10 kilobytes = 10240 bytes.).
*/
public static final int HEADER_PART_SIZE_MAX = 10240;
/**
* The default length of the buffer used for processing a request.
*/
protected static final int DEFAULT_BUFSIZE = 4096;
/**
* A byte sequence that marks the end of <code>header-part</code>
* (<code>CRLFCRLF</code>).
*/
protected static final byte[] HEADER_SEPARATOR = {
CR, LF, CR, LF };
/**
* A byte sequence that that follows a delimiter that will be
* followed by an encapsulation (<code>CRLF</code>).
*/
protected static final byte[] FIELD_SEPARATOR = {
CR, LF};
/**
* A byte sequence that that follows a delimiter of the last
* encapsulation in the stream (<code>--</code>).
*/
protected static final byte[] STREAM_TERMINATOR = {
DASH, DASH};
/**
* A byte sequence that precedes a boundary (<code>CRLF--</code>).
*/
protected static final byte[] BOUNDARY_PREFIX = {
CR, LF, DASH, DASH};
// ----------------------------------------------------------- Data members
/**
* The input stream from which data is read.
*/
private final InputStream input;
/**
* The length of the boundary token plus the leading <code>CRLF--</code>.
*/
private int boundaryLength;
/**
* The amount of data, in bytes, that must be kept in the buffer in order
* to detect delimiters reliably.
*/
private int keepRegion;
/**
* The byte sequence that partitions the stream.
*/
private byte[] boundary;
/**
* The length of the buffer used for processing the request.
*/
private final int bufSize;
/**
* The buffer used for processing the request.
*/
private final byte[] buffer;
/**
* The index of first valid character in the buffer.
* <br>
* 0 <= head < bufSize
*/
private int head;
/**
* The index of last valid characer in the buffer + 1.
* <br>
* 0 <= tail <= bufSize
*/
pri
没有合适的资源?快使用搜索试试~ 我知道了~
基于apache commons.fileupload的文件上传组件
共25个文件
class:13个
java:5个
jar:3个
需积分: 9 0 下载量 133 浏览量
2022-07-17
15:17:36
上传
评论
收藏 1.04MB ZIP 举报
温馨提示
抽取了apache commons.fileupload的最基本代码,组合成一个简单的文件上传组件
资源详情
资源评论
资源推荐
收起资源包目录
zfileupload.zip (25个子文件)
zfileupload
bin
zfileupload
MultipartStream$ItemInputStream.class 4KB
MultipartStream$MalformedStreamException.class 583B
ZFileUploadBase$FileItemIteratorImpl.class 6KB
ZFileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.class 3KB
MultipartStream.class 7KB
ZFileUploadBase$FileItemIteratorImpl$1.class 2KB
UploadHandler.class 2KB
MultipartStream$ProgressNotifier.class 1KB
ZFileUploadBase.class 10KB
ZFileUploadBase$FileItemIteratorImpl$FileItemStreamImpl.class 4KB
MultipartStream$IllegalBoundaryException.class 583B
UploadAction.class 2KB
PostFileTest.class 4KB
.settings
org.eclipse.core.resources.prefs 57B
org.eclipse.jdt.core.prefs 598B
src
zfileupload
ZFileUploadBase.java 26KB
UploadHandler.java 2KB
MultipartStream.java 33KB
UploadAction.java 5KB
PostFileTest.java 2KB
.project 387B
.classpath 503B
lib
commons-httpclient.jar 298KB
httpclient-4.5.13.jar 762KB
commons-fileupload-1.2.2.jar 58KB
共 25 条
- 1
daqinzl
- 粉丝: 56
- 资源: 100
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0