use of org.apache.commons.fileupload.FileUploadException in project ninja by ninjaframework.
the class UploadController method uploadFinish.
/**
*
* This upload method expects a file and simply displays the file in the
* multipart upload again to the user (in the correct mime encoding).
*
* @param context
* @return
* @throws Exception
*/
public Result uploadFinish(Context context) throws Exception {
// we are using a renderable inner class to stream the input again to
// the user
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
try {
// make sure the context really is a multipart context...
if (context.isMultipart()) {
// This is the iterator we can use to iterate over the
// contents
// of the request.
FileItemIterator fileItemIterator = context.getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
String contentType = item.getContentType();
if (contentType != null) {
result.contentType(contentType);
} else {
contentType = mimeTypes.getMimeType(name);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
ByteStreams.copy(stream, responseStreams.getOutputStream());
}
}
}
} catch (IOException | FileUploadException exception) {
throw new InternalServerErrorException(exception);
}
}
};
return new Result(200).render(renderable);
}
use of org.apache.commons.fileupload.FileUploadException in project spring-framework by spring-projects.
the class CommonsMultipartFile method transferTo.
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
if (!isAvailable()) {
throw new IllegalStateException("File has already been moved - cannot be transferred again");
}
if (dest.exists() && !dest.delete()) {
throw new IOException("Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted");
}
try {
this.fileItem.write(dest);
if (logger.isDebugEnabled()) {
String action = "transferred";
if (!this.fileItem.isInMemory()) {
action = isAvailable() ? "copied" : "moved";
}
logger.debug("Multipart file '" + getName() + "' with original filename [" + getOriginalFilename() + "], stored " + getStorageDescription() + ": " + action + " to [" + dest.getAbsolutePath() + "]");
}
} catch (FileUploadException ex) {
throw new IllegalStateException(ex.getMessage());
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
logger.error("Could not transfer to file", ex);
throw new IOException("Could not transfer to file: " + ex.getMessage());
}
}
use of org.apache.commons.fileupload.FileUploadException 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.FileUploadException in project v7files by thiloplanz.
the class BucketsServlet method doFormPost.
private void doFormPost(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException {
ObjectId uploadId = new ObjectId();
BSONObject parameters = new BasicBSONObject();
List<FileItem> files = new ArrayList<FileItem>();
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
for (Object _file : upload.parseRequest(request)) {
FileItem file = (FileItem) _file;
if (file.isFormField()) {
String v = file.getString();
parameters.put(file.getFieldName(), v);
} else {
files.add(file);
}
}
} catch (FileUploadException e) {
throw new IOException(e);
}
} else {
for (Entry<String, String[]> param : request.getParameterMap().entrySet()) {
String[] v = param.getValue();
if (v.length == 1)
parameters.put(param.getKey(), v[0]);
else
parameters.put(param.getKey(), v);
}
}
BSONObject result = new BasicBSONObject("_id", uploadId);
BSONObject uploads = new BasicBSONObject();
for (FileItem file : files) {
DiskFileItem f = (DiskFileItem) file;
// inline until 10KB
if (f.isInMemory()) {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.get(), uploadId, f.getName(), f.getContentType()));
} else {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.getStoreLocation(), uploadId, f.getName(), f.getContentType()));
}
file.delete();
}
result.put("files", uploads);
result.put("parameters", parameters);
bucketCollection.update(new BasicDBObject("_id", bucket.get("_id")), new BasicDBObject("$push", new BasicDBObject("FormPost.data", result)));
String redirect = BSONUtils.getString(bucket, "FormPost.redirect");
// redirect mode
if (StringUtils.isNotBlank(redirect)) {
response.sendRedirect(redirect + "?v7_formpost_id=" + uploadId);
return;
}
// echo mode
// JSON does not work, see https://jira.mongodb.org/browse/JAVA-332
// response.setContentType("application/json");
// response.getWriter().write(JSON.serialize(result));
byte[] bson = BSON.encode(result);
response.getOutputStream().write(bson);
}
use of org.apache.commons.fileupload.FileUploadException in project jaggery by wso2.
the class RequestHostObject method parseMultipart.
private static void parseMultipart(RequestHostObject rho) throws ScriptException {
if (rho.files != null) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(rho.request);
} catch (FileUploadException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
// Process the uploaded items
String name;
rho.files = rho.context.newObject(rho);
for (Object obj : items) {
FileItem item = (FileItem) obj;
name = item.getFieldName();
if (item.isFormField()) {
ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
if (x == null) {
ArrayList<FileItem> array = new ArrayList<FileItem>(1);
array.add(item);
rho.parameterMap.put(name, array);
} else {
x.add(item);
}
} else {
rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[] { item }));
}
}
}
Aggregations