📄 fileuploadbase.java
字号:
} }; } else { if (sizeMax >= 0 && requestSize > sizeMax) { throw new SizeLimitExceededException( "the request was rejected because its size (" + requestSize + ") exceeds the configured maximum (" + sizeMax + ")", requestSize, sizeMax); } } } String charEncoding = headerEncoding; if (charEncoding == null) { charEncoding = ctx.getCharacterEncoding(); } boundary = getBoundary(contentType); if (boundary == null) { throw new FileUploadException( "the request was rejected because " + "no multipart boundary was found"); } notifier = new MultipartStream.ProgressNotifier(listener, ctx.getContentLength()); multi = new MultipartStream(input, boundary, notifier); multi.setHeaderEncoding(charEncoding); skipPreamble = true; findNextItem(); } /** * Called for finding the nex item, if any. * @return True, if an next item was found, otherwise false. * @throws IOException An I/O error occurred. */ private boolean findNextItem() throws IOException { if (eof) { return false; } if (currentItem != null) { currentItem.close(); currentItem = null; } for (;;) { boolean nextPart; if (skipPreamble) { nextPart = multi.skipPreamble(); } else { nextPart = multi.readBoundary(); } if (!nextPart) { if (currentFieldName == null) { // Outer multipart terminated -> No more data eof = true; return false; } // Inner multipart terminated -> Return to parsing the outer multi.setBoundary(boundary); currentFieldName = null; continue; } Map headers = parseHeaders(multi.readHeaders()); if (currentFieldName == null) { // We're parsing the outer multipart String fieldName = getFieldName(headers); if (fieldName != null) { String subContentType = getHeader(headers, CONTENT_TYPE); if (subContentType != null && subContentType.toLowerCase() .startsWith(MULTIPART_MIXED)) { currentFieldName = fieldName; // Multiple files associated with this field name byte[] subBoundary = getBoundary(subContentType); multi.setBoundary(subBoundary); skipPreamble = true; continue; } String fileName = getFileName(headers); currentItem = new FileItemStreamImpl(fileName, fieldName, getHeader(headers, CONTENT_TYPE), fileName == null); notifier.noteItem(); itemValid = true; return true; } } else { String fileName = getFileName(headers); if (fileName != null) { currentItem = new FileItemStreamImpl(fileName, currentFieldName, getHeader(headers, CONTENT_TYPE), false); notifier.noteItem(); itemValid = true; return true; } } multi.discardBodyData(); } } /** * Returns, whether another instance of {@link FileItemStream} * is available. * @throws FileUploadException Parsing or processing the * file item failed. * @throws IOException Reading the file item failed. * @return True, if one or more additional file items * are available, otherwise false. */ public boolean hasNext() throws FileUploadException, IOException { if (eof) { return false; } if (itemValid) { return true; } return findNextItem(); } /** * Returns the next available {@link FileItemStream}. * @throws java.util.NoSuchElementException No more items are * available. Use {@link #hasNext()} to prevent this exception. * @throws FileUploadException Parsing or processing the * file item failed. * @throws IOException Reading the file item failed. * @return FileItemStream instance, which provides * access to the next file item. */ public FileItemStream next() throws FileUploadException, IOException { if (eof || (!itemValid && !hasNext())) { throw new NoSuchElementException(); } itemValid = false; return currentItem; } } /** * This exception is thrown for hiding an inner * {@link FileUploadException} in an {@link IOException}. */ public static class FileUploadIOException extends IOException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = -7047616958165584154L; /** The exceptions cause; we overwrite the parent * classes field, which is available since Java * 1.4 only. */ private final FileUploadException cause; /** * Creates a <code>FileUploadIOException</code> with the * given cause. * @param pCause The exceptions cause, if any, or null. */ public FileUploadIOException(FileUploadException pCause) { // We're not doing super(pCause) cause of 1.3 compatibility. cause = pCause; } /** * Returns the exceptions cause. * @return The exceptions cause, if any, or null. */ public Throwable getCause() { return cause; } } /** * Thrown to indicate that the request is not a multipart request. */ public static class InvalidContentTypeException extends FileUploadException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = -9073026332015646668L; /** * Constructs a <code>InvalidContentTypeException</code> with no * detail message. */ public InvalidContentTypeException() { // Nothing to do. } /** * Constructs an <code>InvalidContentTypeException</code> with * the specified detail message. * * @param message The detail message. */ public InvalidContentTypeException(String message) { super(message); } } /** * Thrown to indicate an IOException. */ public static class IOFileUploadException extends FileUploadException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = 1749796615868477269L; /** The exceptions cause; we overwrite the parent * classes field, which is available since Java * 1.4 only. */ private final IOException cause; /** * Creates a new instance with the given cause. * @param pMsg The detail message. * @param pException The exceptions cause. */ public IOFileUploadException(String pMsg, IOException pException) { super(pMsg); cause = pException; } /** * Returns the exceptions cause. * @return The exceptions cause, if any, or null. */ public Throwable getCause() { return cause; } } /** This exception is thrown, if a requests permitted size * is exceeded. */ protected abstract static class SizeException extends FileUploadException { /** * The actual size of the request. */ private final long actual; /** * The maximum permitted size of the request. */ private final long permitted; /** * Creates a new instance. * @param message The detail message. * @param actual The actual number of bytes in the request. * @param permitted The requests size limit, in bytes. */ protected SizeException(String message, long actual, long permitted) { super(message); this.actual = actual; this.permitted = permitted; } /** * Retrieves the actual size of the request. * * @return The actual size of the request. */ public long getActualSize() { return actual; } /** * Retrieves the permitted size of the request. * * @return The permitted size of the request. */ public long getPermittedSize() { return permitted; } } /** * Thrown to indicate that the request size is not specified. In other * words, it is thrown, if the content-length header is missing or * contains the value -1. * @deprecated As of commons-fileupload 1.2, the presence of a * content-length header is no longer required. */ public static class UnknownSizeException extends FileUploadException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = 7062279004812015273L; /** * Constructs a <code>UnknownSizeException</code> with no * detail message. */ public UnknownSizeException() { super(); } /** * Constructs an <code>UnknownSizeException</code> with * the specified detail message. * * @param message The detail message. */ public UnknownSizeException(String message) { super(message); } } /** * Thrown to indicate that the request size exceeds the configured maximum. */ public static class SizeLimitExceededException extends SizeException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = -2474893167098052828L; /** * @deprecated Replaced by * {@link #SizeLimitExceededException(String, long, long)} */ public SizeLimitExceededException() { this(null, 0, 0); } /** * @deprecated Replaced by * {@link #SizeLimitExceededException(String, long, long)} * @param message The exceptions detail message. */ public SizeLimitExceededException(String message) { this(message, 0, 0); } /** * Constructs a <code>SizeExceededException</code> with * the specified detail message, and actual and permitted sizes. * * @param message The detail message. * @param actual The actual request size. * @param permitted The maximum permitted request size. */ public SizeLimitExceededException(String message, long actual, long permitted) { super(message, actual, permitted); } } /** * Thrown to indicate that A files size exceeds the configured maximum. */ public static class FileSizeLimitExceededException extends SizeException { /** The exceptions UID, for serializing an instance. */ private static final long serialVersionUID = 8150776562029630058L; /** * Constructs a <code>SizeExceededException</code> with * the specified detail message, and actual and permitted sizes. * * @param message The detail message. * @param actual The actual request size. * @param permitted The maximum permitted request size. */ public FileSizeLimitExceededException(String message, long actual, long permitted) { super(message, actual, permitted); } } /** * Returns the progress listener. * @return The progress listener, if any, or null. */ public ProgressListener getProgressListener() { return listener; } /** * Sets the progress listener. * @param pListener The progress listener, if any. Defaults to null. */ public void setProgressListener(ProgressListener pListener) { listener = pListener; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -