/** * Returns the contents of the file as a String, using the default * character encoding. This method uses {@link #get()} to retrieve the * contents of the file. * * @return The contents of the file, as a string. * * @todo Consider making this method throw UnsupportedEncodingException. */ public String getString() { byte[] rawdata = get(); String charset = getCharSet(); if (charset == null) { charset = DEFAULT_CHARSET; } try { return new String(rawdata, charset); } catch (UnsupportedEncodingException e) { return new String(rawdata); } }
/** * Returns a string representation of this object. * * @return a string representation of this object. */ @Override public String toString() { return "name=" + this.getName() + ", StoreLocation=" + String.valueOf(this.getStoreLocation()) + ", size=" + this.getSize() + "bytes, " + "isFormField=" + isFormField() + ", FieldName=" + this.getFieldName(); }
@Override public Collection<String> getHeaders(String name) { if (fileItem instanceof DiskFileItem) { HashSet<String> headers = new HashSet<String>(); Iterator<String> iter = ((DiskFileItem) fileItem).getHeaders().getHeaders(name); while (iter.hasNext()) { headers.add(iter.next()); } return headers; } return Collections.emptyList(); }
if (isInMemory()) { FileOutputStream fout = null; try { fout = new FileOutputStream(file); fout.write(get()); } finally { if (fout != null) { File outputFile = getStoreLocation(); if (outputFile != null) {
/** * 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. */ public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) { DiskFileItem result = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, repository); FileCleaningTracker tracker = getFileCleaningTracker(); if (tracker != null) { tracker.track(result.getTempFile(), this); } return result; }
if (isInMemory()) { if (cachedContent == null) { cachedContent = dfos.getData(); byte[] fileData = new byte[(int) getSize()]; FileInputStream fis = null;
/** * 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. */ public String getString(final String charset) throws UnsupportedEncodingException { return new String(get(), charset); }
/** * 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. */ public void delete() { cachedContent = null; File outputFile = getStoreLocation(); if (outputFile != null && outputFile.exists()) { outputFile.delete(); } }
/** * Reads the state of this object during deserialization. * * @param in The stream from which the state should be read. * * @throws IOException if an error occurs. * @throws ClassNotFoundException if class cannot be found. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // read values in.defaultReadObject(); OutputStream output = getOutputStream(); if (cachedContent != null) { output.write(cachedContent); } else { FileInputStream input = new FileInputStream(dfosFile); IOUtils.copy(input, output); dfosFile.delete(); dfosFile = null; } output.close(); cachedContent = null; }
/** * 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"); }
/** * Writes the state of this object during serialization. * * @param out The stream to which the state should be written. * * @throws IOException if an error occurs. */ private void writeObject(ObjectOutputStream out) throws IOException { // Read the data if (dfos.isInMemory()) { cachedContent = get(); } else { cachedContent = null; dfosFile = dfos.getFile(); } // write out values out.defaultWriteObject(); }
@Override public String getHeader(String name) { if (fileItem instanceof DiskFileItem) { return ((DiskFileItem) fileItem).getHeaders().getHeader(name); } return null; }
@Override public Collection<String> getHeaderNames() { if (fileItem instanceof DiskFileItem) { HashSet<String> headerNames = new HashSet<String>(); Iterator<String> iter = ((DiskFileItem) fileItem).getHeaders().getHeaderNames(); while (iter.hasNext()) { headerNames.add(iter.next()); } return headerNames; } return Collections.emptyList(); }