📄 multipartiterator.java
字号:
package jaction.upload;
import java.io.File;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
/**
* The MultipartIterator class is responsible for reading the
* input data of a multipart request and splitting it up into
* input elements, wrapped inside of a
* {@link jaction.upload.MultipartElement MultipartElement}
* for easy definition. To use this class, create a new instance
* of MultipartIterator passing it a HttpServletRequest in the
* constructor. Then use the {@link #getNextElement() getNextElement}
* method until it returns null, then you're finished. Example: <br>
* <pre>
* MultipartIterator iterator = new MultipartIterator(request);
* MultipartElement element;
*
* while ((element = iterator.getNextElement()) != null) {
* //do something with element
* }
* </pre>
*
* @see jaction.upload.MultipartElement
* @author Mike Schachter
*/
public class MultipartIterator {
/**
* The maximum size in bytes of the buffer used to read lines [4K]
*/
public static int MAX_LINE_SIZE = 4096;
/**
* The request instance for this class
*/
protected HttpServletRequest request;
/**
* The input stream instance for this class
*/
protected BufferedMultipartInputStream inputStream;
/**
* The boundary for this multipart request
*/
protected String boundary;
/**
* The byte array representing the boundary for this multipart request
*/
protected byte[] boundaryBytes;
/**
* Whether or not the input stream is finished
*/
protected boolean contentRead = false;
/**
* The maximum file size in bytes allowed. Ignored if -1
*/
protected long maxSize = -1;
/**
* The total bytes read from this request
*/
protected long totalLength = 0;
/**
* The content length of this request
*/
protected int contentLength;
/**
* The size in bytes written to the filesystem at a time [20K]
*/
protected int diskBufferSize = 2 * 10240;
/**
* The amount of data read from a request at a time.
* This also represents the maximum size in bytes of
* a line read from the request [4KB]
*/
protected int bufferSize = 4096;
/**
* The temporary directory to store files
*/
protected String tempDir;
/**
* Constructs a MultipartIterator with a default buffer size and no file size
* limit
*
* @param request The multipart request to iterate
*/
public MultipartIterator(HttpServletRequest request) throws ServletException{
this(request, -1);
}
/**
* Constructs a MultipartIterator with the specified buffer size and
* no file size limit
*
* @param request The multipart request to iterate
* @param bufferSize The size in bytes that should be read from the input
* stream at a times
*/
public MultipartIterator(HttpServletRequest request, int bufferSize) throws ServletException {
this (request, bufferSize, -1);
}
/**
* Constructs a MultipartIterator with the specified buffer size and
* the specified file size limit in bytes
*
* @param request The multipart request to iterate
* @param bufferSize The size in bytes that should be read from the input
* stream at a times
* @param maxSize The maximum size in bytes allowed for a multipart element's data
*/
public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize)
throws ServletException {
this(request, bufferSize, maxSize, null);
}
public MultipartIterator(HttpServletRequest request,
int bufferSize,
long maxSize,
String tempDir) throws ServletException {
this.request = request;
this.maxSize = maxSize;
if (bufferSize > -1) {
this.bufferSize = bufferSize;
}
if (tempDir != null) {
this.tempDir = tempDir;
}
else {
//default to system-wide tempdir
tempDir = System.getProperty("java.io.tmpdir");
}
parseRequest();
}
/**
* Retrieves the next element in the iterator if one exists.
*
* @throws a ServletException if the post size exceeds the maximum file size
* passed in the 3 argument constructor
* @throws an UnsupportedEncodingException if the "ISO-8859-1" encoding isn't found
* @return a {@link jaction.upload.MultipartElement MultipartElement}
* representing the next element in the request data
*
*/
public MultipartElement getNextElement() throws ServletException, UnsupportedEncodingException {
//retrieve the "Content-Disposition" header
//and parse
String disposition = readLine();
if ((disposition != null) && (disposition.startsWith("Content-Disposition"))) {
String name = parseDispositionName(disposition);
String filename = parseDispositionFilename(disposition);
String contentType = null;
boolean isFile = (filename != null);
if (isFile) {
filename = new File(filename).getName();
//check for windows filenames,
//from linux jdk's the entire filepath
//isn't parsed correctly from File.getName()
int colonIndex = filename.indexOf(":");
if (colonIndex == -1) {
//check for Window's SMB server file paths
colonIndex = filename.indexOf("\\\\");
}
int slashIndex = filename.lastIndexOf("\\");
if ((colonIndex > -1) && (slashIndex > -1)) {
//then consider this filename to be a full
//windows filepath, and parse it accordingly
//to retrieve just the file name
filename = filename.substring(slashIndex+1, filename.length());
}
//get the content type
contentType = readLine();
contentType = parseContentType(contentType);
}
//ignore next line (whitespace) (unless it's a file
//without content-type)
if (! ((isFile) && contentType == null)) {
readLine();
}
MultipartElement element = null;
//process a file element
if (isFile) {
try {
//create a local file on disk representing the element
File elementFile = createLocalFile();
element = new MultipartElement(name, filename, contentType, elementFile);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
throw new ServletException("IOException while reading file element: " + ioe.getMessage(), ioe);
}
}
else {
//read data into String form, then convert to bytes
//for text
StringBuffer textData = new StringBuffer();
String line;
//parse for text data
line = readLine();
while ((line != null) && (!line.startsWith(boundary))) {
textData.append(line);
line = readLine();
}
if (textData.length() > 0) {
//cut off "\r" from the end if necessary
if (textData.charAt(textData.length()-1) == '\r') {
textData.setLength(textData.length()-1);
}
}
//create the element
element = new MultipartElement(name, textData.toString());
}
return element;
}
//reset stream
if (inputStream.markSupported()) {
try {
inputStream.reset();
}
catch (IOException ioe) {
throw new ServletException("IOException while resetting input stream: " +
ioe.getMessage());
}
}
return null;
}
/**
* Set the maximum amount of bytes read from a line at one time
*
* @see javax.servlet.ServletInputStream#readLine(byte[], int, int)
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}
/**
* Get the maximum amount of bytes read from a line at one time
*
* @see javax.servlet.ServletInputStream#readLine(byte[], int, int)
*/
public int getBufferSize() {
return bufferSize;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -