multipartiterator.java
来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 456 行 · 第 1/2 页
JAVA
456 行
else
{
//default to system-wide tempdir
this.tempDir = System.getProperty("java.io.tmpdir");
}
this.maxLengthExceeded = false;
this.inputStream = new MultipartBoundaryInputStream();
parseRequest();
}
/**
* Handles retrieving the boundary and setting the input stream
*/
protected void parseRequest() throws IOException
{
//get the content-type header, which contains the boundary used for separating multipart elements
getContentTypeOfRequest();
//get the content-length header, used to prevent denial of service attacks and for detecting
//whether a file size is over the limit before the client sends the file
this.contentLength = this.request.getContentLength();
//parse the boundary from the content-type header's value
getBoundaryFromContentType();
//don't let the stream read past the content length
this.inputStream.setMaxLength(this.contentLength+1);
//just stop now if the content length is bigger than the maximum allowed size
if ((this.maxSize > -1) && (this.contentLength > this.maxSize))
{
this.maxLengthExceeded = true;
}
else
{
InputStream requestInputStream = this.request.getInputStream();
//mark the input stream to allow multiple reads
if (requestInputStream.markSupported())
{
requestInputStream.mark(contentLength+1);
}
this.inputStream.setBoundary(this.boundary);
this.inputStream.setInputStream(requestInputStream);
}
}
/**
* Retrieves the next element in the iterator if one exists.
*
* @throws IOException if the post size exceeds the maximum file size
* passed in the 3 argument constructor or if the "ISO-8859-1" encoding isn't found
* @return a {@link org.apache.struts.upload.MultipartElement MultipartElement}
* representing the next element in the request data
*
*/
public MultipartElement getNextElement() throws IOException
{
//the MultipartElement to return
MultipartElement element = null;
if (!isMaxLengthExceeded())
{
if (!this.inputStream.isFinalBoundaryEncountered())
{
if (this.inputStream.isElementFile())
{
//attempt to create the multipart element from the collected data
element = createFileMultipartElement();
}
//process a text element
else
{
String encoding = getElementEncoding();
element = createTextMultipartElement(encoding);
}
this.inputStream.resetForNextBoundary();
}
}
return element;
}
/**
* Get the character encoding used for this current multipart element.
*/
protected String getElementEncoding()
{
String encoding = this.inputStream.getElementCharset();
if (encoding == null)
{
encoding = this.request.getCharacterEncoding();
if (encoding == null)
{
encoding = DEFAULT_ENCODING;
}
}
return encoding;
}
/**
* Create a text element from the data in the body of the element.
* @param encoding The character encoding of the string.
*/
protected MultipartElement createTextMultipartElement(String encoding) throws IOException
{
MultipartElement element;
int read = 0;
byte[] buffer = new byte[TEXT_BUFFER_SIZE];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((read = this.inputStream.read(buffer, 0, TEXT_BUFFER_SIZE)) > 0)
{
baos.write(buffer, 0, read);
}
//create the element
String value = baos.toString(encoding);
element = new MultipartElement(this.inputStream.getElementName(), value);
return element;
}
/**
* Create a multipart element instance representing the file in the stream.
*/
protected MultipartElement createFileMultipartElement() throws IOException
{
MultipartElement element;
//create a local file on disk representing the element
File elementFile = createLocalFile();
element = new MultipartElement(this.inputStream.getElementName(), this.inputStream.getElementFileName(),
this.inputStream.getElementContentType(), elementFile);
return element;
}
/**
* 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;
}
/**
* Set the maximum post data size allowed for a multipart request
* @param maxSize The maximum post data size in bytes, set to <code>-1</code>
* for no limit
*/
public void setMaxSize(long maxSize) {
this.maxSize = maxSize;
}
/**
* Get the maximum post data size allowed for a multipart request
* @return The maximum post data size in bytes
*/
public long getMaxSize()
{
return this.maxSize;
}
/**
* Whether or not the maximum length has been exceeded by the client.
*/
public boolean isMaxLengthExceeded()
{
return (this.maxLengthExceeded || this.inputStream.isMaxLengthMet());
}
/**
* Parses a content-type String for the boundary.
*/
private final void getBoundaryFromContentType() throws IOException
{
if (this.contentType.lastIndexOf(PARAMETER_BOUNDARY) != -1)
{
String _boundary = this.contentType.substring(this.contentType.lastIndexOf(PARAMETER_BOUNDARY) + 9);
if (_boundary.endsWith("\n"))
{
//strip it off
this.boundary = _boundary.substring(0, _boundary.length()-1);
}
this.boundary = _boundary;
}
else
{
this.boundary = null;
}
//throw an exception if we're unable to obtain a boundary at this point
if ((this.boundary == null) || (this.boundary.length() < 1))
{
throw new IOException(MESSAGE_CANNOT_RETRIEVE_BOUNDARY);
}
}
/**
* Gets the value of the Content-Type header of the request.
*/
private final void getContentTypeOfRequest()
{
this.contentType = request.getContentType();
if (this.contentType == null)
{
this.contentType = this.request.getHeader(HEADER_CONTENT_TYPE);
}
}
/**
* Creates a file on disk from the current mulitpart element.
*/
protected File createLocalFile() throws IOException
{
File tempFile = File.createTempFile(FILE_PREFIX, null, new File(this.tempDir));
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(tempFile), this.diskBufferSize);
int read = 0;
byte buffer[] = new byte[this.diskBufferSize];
while ((read = this.inputStream.read(buffer, 0, this.diskBufferSize)) > 0)
{
fos.write(buffer, 0, read);
}
fos.flush();
fos.close();
return tempFile;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?