package nbfileupload;
/*
* 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
*/
pr
没有合适的资源?快使用搜索试试~ 我知道了~
基于apache commons.fileupload的文件上传组件,改进了上传速度
共26个文件
class:13个
java:6个
jar:3个
需积分: 10 2 下载量 194 浏览量
2022-07-17
15:49:59
上传
评论
收藏 1.04MB ZIP 举报
温馨提示
apache commons.fileupload上传组件在上传文件时,在调用parseRequest方法时,先将输入流上传到服务器临时目录,在之后的文件处理过程中,再将临时目录下保存的文件复制到指定的目录,多了一道转储步骤。本实现抽取了apache commons.fileupload的最基本代码,组合成一个简单的文件上传组件,对parseRequest方法进行了改进,对于文件,直接存储到指定的上传目录,加快了上传速度。使用本程序时,注意上传端的参数设置,文件路径在前,文件附件在后,一次上传一个文件 StringPart filePathPart = new StringPart("filePath", "111/"+targetFile.getName(), "utf-8"); FilePart part = new FilePart(targetFile.getName(), targetFile); Part[] parts = {filePathPart, part};
资源详情
资源评论
资源推荐
收起资源包目录
NBfileupload.zip (26个子文件)
NBfileupload
bin
nbfileupload
MultipartStream$ItemInputStream.class 4KB
FileItemIteratorImpl$FileItemStreamImpl$1.class 2KB
FileItemIteratorImpl.class 10KB
MultipartStream$MalformedStreamException.class 586B
FileItemIteratorImpl$1.class 2KB
MultipartStream.class 7KB
UploadHandler.class 2KB
MultipartStream$ProgressNotifier.class 1KB
FileItemIteratorImpl$FileItemStreamImpl.class 4KB
NBServletFileUpload.class 6KB
MultipartStream$IllegalBoundaryException.class 586B
UploadAction.class 1KB
PostFileTest.class 4KB
.settings
org.eclipse.core.resources.prefs 57B
org.eclipse.jdt.core.prefs 598B
src
UploadAction.java 3KB
nbfileupload
FileItemIteratorImpl.java 18KB
UploadHandler.java 2KB
NBServletFileUpload.java 5KB
MultipartStream.java 33KB
PostFileTest.java 2KB
.project 388B
.classpath 503B
lib
commons-httpclient.jar 298KB
httpclient-4.5.13.jar 762KB
commons-fileupload-1.2.2.jar 58KB
共 26 条
- 1
daqinzl
- 粉丝: 56
- 资源: 100
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- onenet_chongwukanhu_v06.apk
- 2022NOC软件创意编程赛项真题C++初中-决赛(有解析)
- 2022NOC软件创意编程赛项真题python初中-决赛(有解析)
- openLayer-本地数据加载 (day5)
- A题-正弦信号发生器.xdf
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0