/** * Factory method for a Commons DiskFileItemFactory instance. * <p>Default implementation returns a standard DiskFileItemFactory. * Can be overridden to use a custom subclass, e.g. for testing purposes. * @return the new DiskFileItemFactory instance */ protected DiskFileItemFactory newFileItemFactory() { return new DiskFileItemFactory(); }
/** * Set the maximum allowed size (in bytes) before uploads are written to disk. * Uploaded files will still be received past this amount, but they will not be * stored in memory. Default is 10240, according to Commons FileUpload. * @param maxInMemorySize the maximum in memory size allowed * @see org.apache.commons.fileupload.disk.DiskFileItemFactory#setSizeThreshold */ public void setMaxInMemorySize(int maxInMemorySize) { this.fileItemFactory.setSizeThreshold(maxInMemorySize); }
public MultipartParser(final int spillToDiskSize) { this._uploadItemFactory = new DiskFileItemFactory(); this._uploadItemFactory.setSizeThreshold(spillToDiskSize); }
/** * Set the temporary directory where uploaded files get stored. * Default is the servlet container's temporary directory for the web application. * @see org.springframework.web.util.WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE */ public void setUploadTempDir(Resource uploadTempDir) throws IOException { if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { throw new IllegalArgumentException("Given uploadTempDir [" + uploadTempDir + "] could not be created"); } this.fileItemFactory.setRepository(uploadTempDir.getFile()); this.uploadTempDirSpecified = true; }
/** * 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"; } }
/** * Deletes the underlying storage for a file item, including deleting any * associated temporary disk file. Although this storage will be deleted * automatically when the <code>FileItem</code> instance is garbage * collected, this method can be used to ensure that this is done at an * earlier time, thus preserving system resources. */ @Override public void delete() { cachedContent = null; File outputFile = getStoreLocation(); if (outputFile != null && !isInMemory() && outputFile.exists()) { outputFile.delete(); } }
/** * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem} * instance from the supplied parameters and the local factory * configuration. * * @param fieldName The name of the form field. * @param contentType The content type of the form field. * @param isFormField <code>true</code> if this is a plain form field; * <code>false</code> otherwise. * @param fileName The name of the uploaded file, if any, as supplied * by the browser or other client. * * @return The newly created file item. */ @Override public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) { DiskFileItem result = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, repository); result.setDefaultCharset(defaultCharset); FileCleaningTracker tracker = getFileCleaningTracker(); if (tracker != null) { tracker.track(result.getTempFile(), result); } return result; }
/** * Returns the content charset passed by the agent or <code>null</code> if * not defined. * * @return The content charset passed by the agent or <code>null</code> if * not defined. */ public String getCharSet() { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input Map<String, String> params = parser.parse(getContentType(), ';'); return params.get("charset"); }
/** * Returns the contents of the file as a String, using the specified * encoding. This method uses {@link #get()} to retrieve the * contents of the file. * * @param charset The charset to use. * * @return The contents of the file, as a string. * * @throws UnsupportedEncodingException if the requested character * encoding is not available. */ @Override public String getString(final String charset) throws UnsupportedEncodingException { return new String(get(), charset); }
/** * Returns an {@link java.io.OutputStream OutputStream} that can * be used for storing the contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used * for storing the contents of the file. * * @throws IOException if an error occurs. */ @Override public OutputStream getOutputStream() throws IOException { if (dfos == null) { File outputFile = getTempFile(); dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); } return dfos; }
/** * Factory method for a Commons DiskFileItemFactory instance. * <p>Default implementation returns a standard DiskFileItemFactory. * Can be overridden to use a custom subclass, e.g. for testing purposes. * @return the new DiskFileItemFactory instance */ protected DiskFileItemFactory newFileItemFactory() { return new DiskFileItemFactory(); }
/** * Set the temporary directory where uploaded files get stored. * Default is the servlet container's temporary directory for the web application. * @see org.springframework.web.util.WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE */ public void setUploadTempDir(Resource uploadTempDir) throws IOException { if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { throw new IllegalArgumentException("Given uploadTempDir [" + uploadTempDir + "] could not be created"); } this.fileItemFactory.setRepository(uploadTempDir.getFile()); this.uploadTempDirSpecified = true; }
/** * Set the maximum allowed size (in bytes) before uploads are written to disk. * Uploaded files will still be received past this amount, but they will not be * stored in memory. Default is 10240, according to Commons FileUpload. * @param maxInMemorySize the maximum in memory size allowed * @see org.apache.commons.fileupload.disk.DiskFileItemFactory#setSizeThreshold */ public void setMaxInMemorySize(int maxInMemorySize) { this.fileItemFactory.setSizeThreshold(maxInMemorySize); }
/** * 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); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { // Process regular form field (input type="text|radio|checkbox|etc", select, etc). String fieldName = item.getFieldName(); String fieldValue = item.getString(); // ... (do your job here) } else { // Process form file field (input type="file"). String fieldName = item.getFieldName(); String fileName = FilenameUtils.getName(item.getName()); InputStream fileContent = item.getInputStream(); // ... (do your job here) } } } catch (FileUploadException e) { throw new ServletException("Cannot parse multipart request.", e); } // ... }
@Override public void setServletContext(ServletContext servletContext) { if (!isUploadTempDirSpecified()) { getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext)); } }
/** * 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"; } }
@Before public void setUp() { upload = new PortletFileUpload(new DiskFileItemFactory()); }
@Override public void setServletContext(ServletContext servletContext) { if (!isUploadTempDirSpecified()) { getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext)); } }
/** * 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); }