use of ninja.uploads.FileItem in project ninja by ninjaframework.
the class NinjaServletContext method processFormFields.
private void processFormFields() {
if (formFieldsProcessed)
return;
formFieldsProcessed = true;
// return if not multipart
if (!ServletFileUpload.isMultipartContent(httpServletRequest))
return;
// get fileProvider from route method/class, or defaults to an injected one
// if none injected, then we do not process form fields this way and let the user
// call classic getFileItemIterator() by himself
FileProvider fileProvider = null;
if (route != null) {
if (fileProvider == null) {
fileProvider = route.getControllerMethod().getAnnotation(FileProvider.class);
}
if (fileProvider == null) {
fileProvider = route.getControllerClass().getAnnotation(FileProvider.class);
}
}
// get file item provider from file provider or default one
FileItemProvider fileItemProvider = null;
if (fileProvider == null) {
fileItemProvider = injector.getInstance(FileItemProvider.class);
} else {
fileItemProvider = injector.getInstance(fileProvider.value());
}
if (fileItemProvider instanceof NoFileItemProvider)
return;
// Initialize maps and other constants
ArrayListMultimap<String, String> formMap = ArrayListMultimap.create();
ArrayListMultimap<String, FileItem> fileMap = ArrayListMultimap.create();
// This is the iterator we can use to iterate over the contents of the request.
try {
FileItemIterator fileItemIterator = getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
if (item.isFormField()) {
String charset = NinjaConstant.UTF_8;
String contentType = item.getContentType();
if (contentType != null) {
charset = HttpHeaderUtils.getCharsetOfContentTypeOrUtf8(contentType);
}
// save the form field for later use from getParameter
String value = Streams.asString(item.openStream(), charset);
formMap.put(item.getFieldName(), value);
} else {
// process file as input stream and save for later use in getParameterAsFile or getParameterAsInputStream
FileItem fileItem = fileItemProvider.create(item);
fileMap.put(item.getFieldName(), fileItem);
}
}
} catch (FileUploadException | IOException e) {
throw new RuntimeException("Failed to parse multipart request data", e);
}
// convert both multimap<K,V> to map<K,List<V>>
formFieldsMap = toUnmodifiableMap(formMap);
fileFieldsMap = toUnmodifiableMap(fileMap);
}
use of ninja.uploads.FileItem in project ninja by ninjaframework.
the class UploadControllerAuto method uploadFinishAuto.
/**
*
* 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
*/
@FileProvider(DiskFileItemProvider.class)
public Result uploadFinishAuto(Context context, @Param("file") File file, @Params("file") File[] files, @Param("file") InputStream inputStream, @Params("file") InputStream[] inputStreams, @Param("file") FileItem fileItem, @Params("file") FileItem[] fileItems, @Param("file2") File file2) throws Exception {
StringBuilder sb = new StringBuilder();
// file
sb.append("file\n").append(IOUtils.toString(new FileInputStream(file))).append("\n");
// files
for (File f : files) {
sb.append("files\n").append(IOUtils.toString(new FileInputStream(f))).append("\n");
}
// file inputstream
sb.append("inputstream\n").append(IOUtils.toString(inputStream)).append("\n");
// files inputstream
for (InputStream is : inputStreams) {
sb.append("inputstreams\n").append(IOUtils.toString(is)).append("\n");
}
// file fileItem
sb.append("fileitem\n").append(IOUtils.toString(fileItem.getInputStream())).append("\n");
// file fileItems
for (FileItem fi : fileItems) {
sb.append("fileitems\n").append(IOUtils.toString(fi.getInputStream())).append("\n");
}
// context.getParameterAsFileItem
sb.append("getParameterAsFileItem\n").append(IOUtils.toString(context.getParameterAsFileItem("file").getInputStream())).append("\n");
// file2
sb.append("file2\n").append(IOUtils.toString(new FileInputStream(file2))).append("\n");
return Results.ok().renderRaw(sb.toString().getBytes());
}
Aggregations