jodd.upload.FileUpload Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jodd-upload Show documentation
Show all versions of jodd-upload Show documentation
Jodd Upload is library for managing uploads.
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.
package jodd.upload;
import java.io.IOException;
import java.io.InputStream;
/**
* Encapsulates base for uploaded file. Its instance may be
* either valid, when it represent an uploaded file, or invalid
* when uploaded file doesn't exist or there was a problem with it.
*/
public abstract class FileUpload {
protected final MultipartRequestInputStream input;
protected final int maxFileSize;
protected final FileUploadHeader header;
protected FileUpload(MultipartRequestInputStream input, int maxFileSize) {
this.input = input;
this.header = input.lastHeader;
this.maxFileSize = maxFileSize;
}
// ---------------------------------------------------------------- header
/**
* Returns {@link FileUploadHeader} of uploaded file.
*/
public FileUploadHeader getHeader() {
return header;
}
// ---------------------------------------------------------------- data
/**
* Returns all bytes of uploaded file.
*/
public abstract byte[] getFileContent() throws IOException;
/**
* Returns input stream of uploaded file.
*/
public abstract InputStream getFileInputStream() throws IOException;
// ---------------------------------------------------------------- size and validity
protected boolean valid;
protected int size = -1;
protected boolean fileTooBig;
/**
* Returns the file upload size or -1
if file was not uploaded.
*/
public int getSize() {
return size;
}
/**
* Returns true
if file was uploaded.
*/
public boolean isUploaded() {
return size != -1;
}
/**
* Returns true
if upload process went correctly.
* This still does not mean that file is uploaded, e.g. when file
* was not specified on the form.
*/
public boolean isValid() {
return valid;
}
/**
* Returns max file size or -1
if there is no max file size.
*/
public int getMaxFileSize() {
return maxFileSize;
}
/**
* Returns true
if file is too big. File will be marked as invalid.
*/
public boolean isFileTooBig() {
return fileTooBig;
}
// ---------------------------------------------------------------- status
/**
* Returns true
if uploaded file content is stored in memory.
*/
public abstract boolean isInMemory();
// ---------------------------------------------------------------- process
/**
* Process request input stream. Note that file size is unknown at this point.
* Therefore, the implementation should set the {@link #getSize() size}
* attribute after successful processing. This method also must set the
* {@link #isValid() valid} attribute.
*
* @see MultipartRequestInputStream
*/
protected abstract void processStream() throws IOException;
// ---------------------------------------------------------------- toString
/**
* Returns basic information about the uploaded file.
*/
@Override
public String toString() {
return "FileUpload: uploaded=[" + isUploaded() + "] valid=[" + valid + "] field=[" +
header.getFormFieldName() + "] name=[" + header.getFileName() + "] size=[" + size + ']';
}
}