📄 multipartrequest.java
字号:
*/
private byte[] readFile(InputStream in) throws IOException {
// In this case, we do not need to worry about a outputdirectory.
ByteArrayOutputStream out = new ByteArrayOutputStream();
long count = readAndWrite(in, out);
// Count would NOT be larger than zero if out was null.
if (count > 0) {
// Return contents of file to parse method for inclusion in htFiles object.
return out.toByteArray();
} else {
return null;
}
}
/**
Returns the length of the line minus line ending.
@param endOfArray This is because in many cases the byteLine will have garbage data at the end, so we
act as though the actual end of the array is this parameter. If you want to process
the complete byteLine, specify byteLine.length as the endOfArray parameter.
*/
private static final int getLengthMinusEnding(byte[] byteLine,
int endOfArray) {
if (byteLine == null) {
return 0;
}
if ((endOfArray >= 2) && (byteLine[endOfArray - 2] == '\r') &&
(byteLine[endOfArray - 1] == '\n')) {
return endOfArray - 2;
} else if (((endOfArray >= 1) && (byteLine[endOfArray - 1] == '\n')) ||
(byteLine[endOfArray - 1] == '\r')) {
return endOfArray - 1;
} else {
return endOfArray;
}
}
/**
* DOCUMENT ME!
*
* @param buf
*
* @return
*/
private static final int getLengthMinusEnding(StringBuffer buf) {
if ((buf.length() >= 2) && (buf.charAt(buf.length() - 2) == '\r') &&
(buf.charAt(buf.length() - 1) == '\n')) {
return buf.length() - 2;
} else if (((buf.length() >= 1) &&
(buf.charAt(buf.length() - 1) == '\n')) ||
(buf.charAt(buf.length() - 1) == '\r')) {
return buf.length() - 1;
} else {
return buf.length();
}
}
/**
Reads at most READ_BLOCK blocks of data, or a single line whichever is smaller.
Returns -1, if nothing to read, or we have reached the specified content-length.
Assumes that bytToBeRead.length indicates the block size to read.
@return -1 if stream has ended, before a newline encountered (should never happen) OR
we have read past the Content-Length specified. (Should also not happen). Otherwise
return the number of characters read. You can test whether the number returned is less
than bytesToBeRead.length, which indicates that we have read the last line of a file or parameter or
a border line, or some other formatting stuff.
*/
private int readLine(InputStream in, byte[] bytesToBeRead)
throws IOException {
// Ensure that there is still stuff to read...
if (intTotalRead >= intContentLength) {
return -1;
}
// Get the length of what we are wanting to read.
int length = bytesToBeRead.length;
// End of content, but some servers (apparently) may not realise this and end the InputStream, so
// we cover ourselves this way.
if (length > (intContentLength - intTotalRead)) {
length = (int) (intContentLength - intTotalRead); // So we only read the data that is left.
}
int result = readLine(in, bytesToBeRead, 0, length);
// Only if we get actually read something, otherwise something weird has happened, such as the end of stream.
if (result > 0) {
intTotalRead += result;
}
return result;
}
/**
This needs to support the possibility of a / or a \ separator.
Returns strFilename after removing all characters before the last
occurence of / or \.
*/
private static final String getBasename(String strFilename) {
if (strFilename == null) {
return strFilename;
}
int intIndex = strFilename.lastIndexOf("/");
if ((intIndex == -1) || (strFilename.lastIndexOf("\\") > intIndex)) {
intIndex = strFilename.lastIndexOf("\\");
}
if (intIndex != -1) {
return strFilename.substring(intIndex + 1);
} else {
return strFilename;
}
}
/**
trimQuotes trims any quotes from the start and end of a string and returns the trimmed string...
*/
private static final String trimQuotes(String strItem) {
// Saves having to go any further....
if ((strItem == null) || (strItem.indexOf("\"") == -1)) {
return strItem;
}
// Get rid of any whitespace..
strItem = strItem.trim();
if (strItem.charAt(0) == '\"') {
strItem = strItem.substring(1);
}
if (strItem.charAt(strItem.length() - 1) == '\"') {
strItem = strItem.substring(0, strItem.length() - 1);
}
return strItem;
}
/**
Format of string name=value; name=value;
If not found, will return null.
*/
private static final String getValue(String strName, String strToDecode) {
strName = strName + "=";
int startIndexOf = 0;
while (startIndexOf < strToDecode.length()) {
int indexOf = strToDecode.indexOf(strName, startIndexOf);
// Ensure either first name, or a space or ; precedes it.
if (indexOf != -1) {
if ((indexOf == 0) ||
Character.isWhitespace(strToDecode.charAt(indexOf - 1)) ||
(strToDecode.charAt(indexOf - 1) == ';')) {
int endIndexOf = strToDecode.indexOf(";",
indexOf + strName.length());
if (endIndexOf == -1) { // May return an empty string...
return strToDecode.substring(indexOf +
strName.length());
} else {
return strToDecode.substring(indexOf +
strName.length(), endIndexOf);
}
} else {
startIndexOf = indexOf + strName.length();
}
} else {
return null;
}
}
return null;
}
/**
* <I>Tomcat's ServletInputStream.readLine(byte[],int,int) Slightly Modified to utilise in.read()</I>
* <BR>
* Reads the input stream, one line at a time. Starting at an
* offset, reads bytes into an array, until it reads a certain number
* of bytes or reaches a newline character, which it reads into the
* array as well.
*
* <p>This method <u><b>does not</b></u> returns -1 if it reaches the end of the input
* stream before reading the maximum number of bytes, it returns -1, if no bytes read.
*
* @param b an array of bytes into which data is read
*
* @param off an integer specifying the character at which
* this method begins reading
*
* @param len an integer specifying the maximum number of
* bytes to read
*
* @return an integer specifying the actual number of bytes
* read, or -1 if the end of the stream is reached
*
* @exception IOException if an input or output exception has occurred
*
Note: We have a problem with Tomcat reporting an erroneous number of bytes, so we need to check this.
This is the method where we get an infinite loop, but only with binary files.
*/
private int readLine(InputStream in, byte[] b, int off, int len)
throws IOException {
if (len <= 0) {
return 0;
}
int count = 0;
int c;
while ((c = in.read()) != -1) {
b[off++] = (byte) c;
count++;
if ((c == '\n') || (count == len)) {
break;
}
}
return (count > 0) ? count : (-1);
}
/**
Use when debugging this object.
*/
protected void debug(String x) {
if (debug != null) {
debug.println(x);
debug.flush();
}
}
/**
For debugging.
Be aware that if you have a form with multiple FILE input types of the same name, only the first one
will actually be returned from the getFileParameter(...) method, which all the file access methods call.
So if your upload file was actually uploaded against the second file input field, then it will not be accessible,
via the methods.
*/
public String getHtmlTable() {
StringBuffer sbReturn = new StringBuffer();
sbReturn.append("<h2>Parameters</h2>");
sbReturn.append(
"\n<table border=3><tr><td><b>Name</b></td><td><b>Value</b></td></tr>");
for (Enumeration e = getParameterNames(); e.hasMoreElements();) {
String strName = (String) e.nextElement();
sbReturn.append("\n<tr>" + "<td>" + strName + "</td>");
sbReturn.append("<td><table border=1><tr>");
for (Enumeration f = getURLParameters(strName);
f.hasMoreElements();) {
String value = (String) f.nextElement();
sbReturn.append("<td>" + value + "</td>");
}
sbReturn.append("</tr></table></td></tr>");
}
sbReturn.append("</table>");
sbReturn.append("<h2>File Parameters</h2>");
sbReturn.append(
"\n<table border=2><tr><td><b>Name</b></td><td><b>Filename</b></td><td><b>Path</b></td><td><b>Content Type</b></td><td><b>Size</b></td></tr>");
for (Enumeration e = getFileParameterNames(); e.hasMoreElements();) {
String strName = (String) e.nextElement();
sbReturn.append("\n<tr>" + "<td>" + strName + "</td>" + "<td>" +
((getFileSystemName(strName) != null)
? getFileSystemName(strName) : "") + "</td>");
if (loadIntoMemory) {
sbReturn.append("<td>" +
((getFileSize(strName) > 0) ? "<i>in memory</i>" : "") +
"</td>");
} else {
sbReturn.append("<td>" +
((getFile(strName) != null)
? getFile(strName).getAbsolutePath() : "") + "</td>");
}
sbReturn.append("<td>" +
((getContentType(strName) != null) ? getContentType(strName) : "") +
"</td>" + "<td>" +
((getFileSize(strName) != -1) ? (getFileSize(strName) + "") : "") +
"</td>" + "</tr>");
}
sbReturn.append("</table>");
return sbReturn.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -