/** * Set the maximum allowed size (in bytes) for each individual file before * an upload gets rejected. -1 indicates no limit (the default). * @param maxUploadSizePerFile the maximum upload size per file * @since 4.2 * @see org.apache.commons.fileupload.FileUploadBase#setFileSizeMax */ public void setMaxUploadSizePerFile(long maxUploadSizePerFile) { this.fileUpload.setFileSizeMax(maxUploadSizePerFile); }
/** * Create an instance wrapping the given FileItem. * @param fileItem the FileItem to wrap */ public CommonsMultipartFile(FileItem fileItem) { this.fileItem = fileItem; this.size = this.fileItem.getSize(); }
/** * Set the maximum allowed size (in bytes) before an upload gets rejected. * -1 indicates no limit (the default). * @param maxUploadSize the maximum upload size allowed * @see org.apache.commons.fileupload.FileUploadBase#setSizeMax */ public void setMaxUploadSize(long maxUploadSize) { this.fileUpload.setSizeMax(maxUploadSize); }
@Override public String getName() { return this.fileItem.getFieldName(); }
/** * Determine whether the multipart content is still available. * If a temporary file has been moved, the content is no longer available. */ protected boolean isAvailable() { // If in memory, it's available. if (this.fileItem.isInMemory()) { return true; } // Check actual existence of temporary file. if (this.fileItem instanceof DiskFileItem) { return ((DiskFileItem) this.fileItem).getStoreLocation().exists(); } // Check whether current file size is different than original one. return (this.fileItem.getSize() == this.size); }
public String get(String key) { FileItem fi = byName.get(key); if(fi==null) return null; return fi.getString(); }
@Override public InputStream getInputStream() throws IOException { if (!isAvailable()) { throw new IllegalStateException("File has been moved - cannot be read again"); } InputStream inputStream = this.fileItem.getInputStream(); return (inputStream != null ? inputStream : StreamUtils.emptyInput()); }
@Override public String getContentType() { return this.fileItem.getContentType(); }
/** * If any file is created on the disk, delete them all. * Even if this method is not called, the resource will be still cleaned up later by GC. */ public void cleanUp() { for (FileItem item : byName.values()) item.delete(); }
@Override public byte[] getBytes() { if (!isAvailable()) { throw new IllegalStateException("File has been moved - cannot be read again"); } byte[] bytes = this.fileItem.get(); return (bytes != null ? bytes : new byte[0]); }
/** * Return a description for the storage location of the multipart content. * Tries to be as specific as possible: mentions the file location in case * of a temporary file. */ public String getStorageDescription() { if (this.fileItem.isInMemory()) { return "in memory"; } else if (this.fileItem instanceof DiskFileItem) { return "at [" + ((DiskFileItem) this.fileItem).getStoreLocation().getAbsolutePath() + "]"; } else { return "on disk"; } }
/** * Retrieves the field name from the <code>Content-disposition</code> * header. * * @param headers A <code>Map</code> containing the HTTP request headers. * * @return The field name for the current <code>encapsulation</code>. */ protected String getFieldName(FileItemHeaders headers) { return getFieldName(headers.getHeader(CONTENT_DISPOSITION)); }
/** * Retrieves the file name from the <code>Content-disposition</code> * header. * * @param headers The HTTP headers object. * * @return The file name for the current <code>encapsulation</code>. */ protected String getFileName(FileItemHeaders headers) { return getFileName(headers.getHeader(CONTENT_DISPOSITION)); }
/** * Set the default character encoding to use for parsing requests, * to be applied to headers of individual parts and to form fields. * Default is ISO-8859-1, according to the Servlet spec. * <p>If the request specifies a character encoding itself, the request * encoding will override this setting. This also allows for generically * overriding the character encoding in a filter that invokes the * {@code ServletRequest.setCharacterEncoding} method. * @param defaultEncoding the character encoding to use * @see javax.servlet.ServletRequest#getCharacterEncoding * @see javax.servlet.ServletRequest#setCharacterEncoding * @see WebUtils#DEFAULT_CHARACTER_ENCODING * @see org.apache.commons.fileupload.FileUploadBase#setHeaderEncoding */ public void setDefaultEncoding(String defaultEncoding) { this.fileUpload.setHeaderEncoding(defaultEncoding); }
/** * Determine the default encoding to use for parsing requests. * @see #setDefaultEncoding */ protected String getDefaultEncoding() { String encoding = getFileUpload().getHeaderEncoding(); if (encoding == null) { encoding = WebUtils.DEFAULT_CHARACTER_ENCODING; } return encoding; }
@Override public String getName() { return this.fileItem.getFieldName(); }
@Override public String getContentType() { return this.fileItem.getContentType(); }
/** * Create an instance wrapping the given FileItem. * @param fileItem the FileItem to wrap */ public CommonsMultipartFile(FileItem fileItem) { this.fileItem = fileItem; this.size = this.fileItem.getSize(); }
/** * Set the maximum allowed size (in bytes) before an upload gets rejected. * -1 indicates no limit (the default). * @param maxUploadSize the maximum upload size allowed * @see org.apache.commons.fileupload.FileUploadBase#setSizeMax */ public void setMaxUploadSize(long maxUploadSize) { this.fileUpload.setSizeMax(maxUploadSize); }
/** * Set the maximum allowed size (in bytes) for each individual file before * an upload gets rejected. -1 indicates no limit (the default). * @param maxUploadSizePerFile the maximum upload size per file * @since 4.2 * @see org.apache.commons.fileupload.FileUploadBase#setFileSizeMax */ public void setMaxUploadSizePerFile(long maxUploadSizePerFile) { this.fileUpload.setFileSizeMax(maxUploadSizePerFile); }