use of org.apache.commons.fileupload.FileUpload in project spring-framework by spring-projects.
the class CommonsMultipartResolver method parseRequest.
/**
* Parse the given servlet request, resolving its multipart elements.
* @param request the request to parse
* @return the parsing result
* @throws MultipartException if multipart resolution failed.
*/
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
String encoding = determineEncoding(request);
FileUpload fileUpload = prepareFileUpload(encoding);
try {
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
} catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
} catch (FileUploadBase.FileSizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
} catch (FileUploadException ex) {
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
}
use of org.apache.commons.fileupload.FileUpload in project spring-framework by spring-projects.
the class CommonsFileUploadSupport method prepareFileUpload.
/**
* Determine an appropriate FileUpload instance for the given encoding.
* <p>Default implementation returns the shared FileUpload instance
* if the encoding matches, else creates a new FileUpload instance
* with the same configuration other than the desired encoding.
* @param encoding the character encoding to use
* @return an appropriate FileUpload instance.
*/
protected FileUpload prepareFileUpload(String encoding) {
FileUpload fileUpload = getFileUpload();
FileUpload actualFileUpload = fileUpload;
// its own encoding that does not match the default encoding.
if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
actualFileUpload = newFileUpload(getFileItemFactory());
actualFileUpload.setSizeMax(fileUpload.getSizeMax());
actualFileUpload.setFileSizeMax(fileUpload.getFileSizeMax());
actualFileUpload.setHeaderEncoding(encoding);
}
return actualFileUpload;
}
use of org.apache.commons.fileupload.FileUpload in project xwiki-platform by xwiki.
the class FileUploadPlugin method loadFileList.
/**
* Loads the list of uploaded files in the context if there are any uploaded files.
*
* @param uploadMaxSize Maximum size of the uploaded files.
* @param uploadSizeThreashold Threashold over which the file data should be stored on disk, and not in memory.
* @param tempdir Temporary directory to store the uploaded files that are not kept in memory.
* @param context Context of the request.
* @throws XWikiException if the request could not be parsed, or the maximum file size was reached.
* @see FileUploadPluginApi#loadFileList(long, int, String)
*/
public void loadFileList(long uploadMaxSize, int uploadSizeThreashold, String tempdir, XWikiContext context) throws XWikiException {
LOGGER.debug("Loading uploaded files");
// Continuing would empty the list.. We need to stop.
if (context.get(FILE_LIST_KEY) != null) {
LOGGER.debug("Called loadFileList twice");
return;
}
// Get the FileUpload Data
// Make sure the factory only ever creates file items which will be deleted when the jvm is stopped.
DiskFileItemFactory factory = new DiskFileItemFactory() {
@Override
public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) {
try {
final DiskFileItem item = (DiskFileItem) super.createItem(fieldName, contentType, isFormField, fileName);
// Needed to make sure the File object is created.
item.getOutputStream();
return item;
} catch (IOException e) {
String path = System.getProperty("java.io.tmpdir");
if (super.getRepository() != null) {
path = super.getRepository().getPath();
}
throw new RuntimeException("Unable to create a temporary file for saving the attachment. " + "Do you have write access on " + path + "?");
}
}
};
factory.setSizeThreshold(uploadSizeThreashold);
if (tempdir != null) {
File tempdirFile = new File(tempdir);
if (tempdirFile.mkdirs() && tempdirFile.canWrite()) {
factory.setRepository(tempdirFile);
}
}
// TODO: Does this work in portlet mode, or we must use PortletFileUpload?
FileUpload fileupload = new ServletFileUpload(factory);
RequestContext reqContext = new ServletRequestContext(context.getRequest().getHttpServletRequest());
fileupload.setSizeMax(uploadMaxSize);
try {
@SuppressWarnings("unchecked") List<FileItem> list = fileupload.parseRequest(reqContext);
if (list.size() > 0) {
LOGGER.info("Loaded " + list.size() + " uploaded files");
}
// We store the file list in the context
context.put(FILE_LIST_KEY, list);
} catch (FileUploadBase.SizeLimitExceededException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE, "Exception uploaded file");
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION, "Exception while parsing uploaded file", e);
}
}
use of org.apache.commons.fileupload.FileUpload in project camunda-bpm-platform by camunda.
the class MultipartPayloadProvider method readFrom.
public MultipartFormData readFrom(Class<MultipartFormData> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
final MultipartFormData multipartFormData = createMultipartFormDataInstance();
final FileUpload fileUpload = createFileUploadInstance();
String contentType = httpHeaders.getFirst("content-type");
RestMultipartRequestContext requestContext = createRequestContext(entityStream, contentType);
// parse the request (populates the multipartFormData)
parseRequest(multipartFormData, fileUpload, requestContext);
return multipartFormData;
}
Aggregations