use of org.apache.commons.fileupload.FileUploadException in project OpenClinica by OpenClinica.
the class FileUploadHelper method getFiles.
@SuppressWarnings("unchecked")
private List<File> getFiles(HttpServletRequest request, ServletContext context, String dirToSaveUploadedFileIn) {
List<File> files = new ArrayList<File>();
// FileCleaningTracker fileCleaningTracker =
// FileCleanerCleanup.getFileCleaningTracker(context);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(getFileProperties().getFileSizeMax());
try {
// Parse the request
List<FileItem> items = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
request.setAttribute(item.getFieldName(), item.getString());
// DO NOTHING , THIS SHOULD NOT BE Handled here
} else {
getFileProperties().isValidExtension(item.getName());
files.add(processUploadedFile(item, dirToSaveUploadedFileIn));
}
}
return files;
} catch (FileSizeLimitExceededException slee) {
throw new OpenClinicaSystemException("exceeds_permitted_file_size", new Object[] { String.valueOf(getFileProperties().getFileSizeMaxInMb()) }, slee.getMessage());
} catch (FileUploadException fue) {
throw new OpenClinicaSystemException("file_upload_error_occured", new Object[] { fue.getMessage() }, fue.getMessage());
}
}
use of org.apache.commons.fileupload.FileUploadException in project flow by vaadin.
the class StreamReceiverHandler method handleMultipartFileUploadFromInputStream.
private boolean handleMultipartFileUploadFromInputStream(VaadinSession session, VaadinRequest request, StreamReceiver streamReceiver, StateNode owner) throws IOException {
boolean success = true;
long contentLength = getContentLength(request);
// Parse the request
FileItemIterator iter;
try {
iter = getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
boolean itemSuccess = handleStream(session, streamReceiver, owner, contentLength, item);
success = success && itemSuccess;
}
} catch (FileUploadException e) {
success = false;
getLogger().warn("File upload failed.", e);
}
return success;
}
use of org.apache.commons.fileupload.FileUploadException in project application by collectionspace.
the class WebUIRequest method initRequest.
private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
this.request = request;
this.response = response;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter;
try {
iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
// InputStream stream = item.openStream();
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
contentHeaders = item.getHeaders();
uploadName = item.getName();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
if (item != null) {
InputStream stream = item.openStream();
IOUtils.copy(stream, byteOut);
new TeeInputStream(stream, byteOut);
}
bytebody = byteOut.toByteArray();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
body = IOUtils.toString(request.getInputStream(), "UTF-8");
}
this.ppath = p.toArray(new String[0]);
if (!(umbrella instanceof WebUIUmbrella))
throw new UIException("Bad umbrella");
this.umbrella = (WebUIUmbrella) umbrella;
session = calculateSessionId();
}
use of org.apache.commons.fileupload.FileUploadException in project tutorials by eugenp.
the class UploadController method handleUpload.
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(HttpServletRequest request) {
System.out.println(System.getProperty("java.io.tmpdir"));
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
factory.setFileCleaningTracker(null);
// Configure a repository (to ensure a secure temp location is used)
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// Parse the request
List<FileItem> items = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField()) {
try (InputStream uploadedStream = item.getInputStream();
OutputStream out = new FileOutputStream("file.mov")) {
IOUtils.copy(uploadedStream, out);
out.close();
}
}
}
// Parse the request with Streaming API
upload = new ServletFileUpload();
FileItemIterator iterStream = upload.getItemIterator(request);
while (iterStream.hasNext()) {
FileItemStream item = iterStream.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (!item.isFormField()) {
// Process the InputStream
} else {
// process form fields
String formFieldValue = Streams.asString(stream);
}
}
return "success!";
} catch (IOException | FileUploadException ex) {
return "failed: " + ex.getMessage();
}
}
use of org.apache.commons.fileupload.FileUploadException in project wechat by dllwh.
the class UploadUtils method initFields.
/**
* @Title:initFields
* @Description:处理上传内容
* @param request
* @return:void 返回类型
*/
@SuppressWarnings("unchecked")
private static Map<String, Object> initFields(HttpServletRequest request) {
// 存储表单字段和非表单字段
Map<String, Object> resultMap = new HashMap<String, Object>();
// 第一步:判断request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 第二步:解析request
if (isMultipart) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// 阀值,超过这个值才会写到临时目录,否则在内存中
factory.setSizeThreshold(1024 * 1024 * 10);
factory.setRepository(new File(tempPath));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
// 最大上传限制
upload.setSizeMax(maxSize);
/* FileItem */
List<FileItem> items = null;
// Parse the request
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
// 第3步:处理uploaded items
if (items != null && items.size() > 0) {
Iterator<FileItem> iter = items.iterator();
// 文件域对象
List<FileItem> list = new ArrayList<FileItem>();
// 表单字段
Map<String, String> fields = new HashMap<String, String>();
while (iter.hasNext()) {
FileItem item = iter.next();
// 处理所有表单元素和文件域表单元素
if (item.isFormField()) {
// 表单元素
String name = item.getFieldName();
String value = item.getString();
fields.put(name, value);
} else {
// 文件域表单元素
list.add(item);
}
}
resultMap.put(FORM_FIELDS, fields);
resultMap.put(FILE_FIELDS, list);
}
}
return resultMap;
}
Aggregations