List of usage examples for org.apache.commons.fileupload FileItem getContentType
String getContentType();
null
if not defined. From source file:org.mojavemvc.core.HttpParameterMapSource.java
private void processUploadedFile(FileItem item, Map<String, Object> paramMap) throws IOException { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); InputStream uploadedStream = item.getInputStream(); paramMap.put(fieldName, new UploadedFile(fileName, uploadedStream, contentType, isInMemory, sizeInBytes)); }
From source file:org.muse.mneme.impl.AttachmentServiceImpl.java
/** * {@inheritDoc}//from w w w . ja v a 2s .co m */ public Reference addAttachment(String application, String context, String prefix, boolean uniqueHolder, FileItem file) { pushAdvisor(); try { String name = file.getName(); if (name != null) { name = massageName(name); } String type = file.getContentType(); // TODO: change to file.getInputStream() for after Sakai 2.3 more efficient support // InputStream body = file.getInputStream(); byte[] body = file.get(); long size = file.getSize(); // detect no file selected if ((name == null) || (type == null) || (body == null) || (size == 0)) { // TODO: if using input stream, close it // if (body != null) body.close(); return null; } Reference rv = doAdd(contentHostingId(name, application, context, prefix, uniqueHolder), name, type, body, size, false); // if this failed, and we are not using a uniqueHolder, try it with a uniqueHolder if ((rv == null) && !uniqueHolder) { rv = doAdd(contentHostingId(name, application, context, prefix, true), name, type, body, size, false); } // TODO: we might not want a thumb (such as for submission uploads to essay/task // if we added one if (rv != null) { // if it is an image if (type.toLowerCase().startsWith("image/")) { addThumb(rv, name, body); } } return rv; } finally { popAdvisor(); } }
From source file:org.mycore.frontend.editor.MCREditorServlet.java
private void sendToDebug(HttpServletResponse res, Document unprocessed, MCREditorSubmission sub) throws IOException, UnsupportedEncodingException { res.setContentType("text/html; charset=UTF-8"); PrintWriter pw = res.getWriter(); pw.println("<html><body><p><pre>"); for (int i = 0; i < sub.getVariables().size(); i++) { MCREditorVariable var = (MCREditorVariable) sub.getVariables().get(i); pw.println(var.getPath() + " = " + var.getValue()); FileItem file = var.getFile(); if (file != null) { pw.println(" is uploaded file " + file.getContentType() + ", " + file.getSize() + " bytes"); }//from w w w .j av a2 s . co m } pw.println("</pre></p><p>"); XMLOutputter outputter = new XMLOutputter(); Format fmt = Format.getPrettyFormat(); fmt.setLineSeparator("\n"); fmt.setOmitDeclaration(true); outputter.setFormat(fmt); Element pre = new Element("pre"); pre.addContent(outputter.outputString(unprocessed)); outputter.output(pre, pw); pre = new Element("pre"); pre.addContent(outputter.outputString(sub.getXML())); outputter.output(pre, pw); pw.println("</p></body></html>"); pw.close(); }
From source file:org.nuxeo.ecm.webengine.forms.FormData.java
protected Blob getBlob(FileItem item) { StreamSource src;/* www. ja va2 s .c o m*/ if (item.isInMemory()) { src = new ByteArraySource(item.get()); } else { try { src = new InputStreamSource(item.getInputStream()); } catch (IOException e) { throw WebException.wrap("Failed to get blob data", e); } } String ctype = item.getContentType(); StreamingBlob blob = new StreamingBlob(src, ctype == null ? "application/octet-stream" : ctype); try { blob.persist(); } catch (IOException e) { log.error(e, e); } blob.setFilename(item.getName()); return blob; }
From source file:org.nuxeo.ecm.webengine.util.FormData.java
protected Blob getBlob(FileItem item) throws WebException { StreamSource src;/*from ww w. ja va 2s . c o m*/ if (item.isInMemory()) { src = new ByteArraySource(item.get()); } else { try { src = new InputStreamSource(item.getInputStream()); } catch (IOException e) { throw new WebException("Failed to get blob data", e); } } String ctype = item.getContentType(); StreamingBlob blob = new StreamingBlob(src, ctype == null ? "application/octet-stream" : ctype); blob.setFilename(item.getName()); return blob; }
From source file:org.nuxeo.opensocial.container.server.handler.AbstractActionHandler.java
protected static Blob getBlob(FileItem item) { StreamSource src;// w w w . ja v a 2s . c om if (item.isInMemory()) { src = new ByteArraySource(item.get()); } else { try { src = new InputStreamSource(item.getInputStream()); } catch (IOException e) { throw WebException.wrap("Failed to get blob data", e); } } String ctype = item.getContentType(); StreamingBlob blob = new StreamingBlob(src, ctype == null ? "application/octet-stream" : ctype); blob.setFilename(item.getName()); return blob; }
From source file:org.nuxeo.theme.webwidgets.Manager.java
public static String uploadFile(HttpServletRequest request, String providerName, String uid, String dataName) throws WidgetException, ProviderException { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<?> fileItems = null; try {/*from www.j av a2 s . c o m*/ fileItems = upload.parseRequest(request); } catch (FileUploadException e) { log.error("Could not upload file", e); } if (fileItems == null) { log.error("No file upload found."); return ""; } WidgetData data = null; Iterator<?> it = fileItems.iterator(); if (it.hasNext()) { FileItem fileItem = (FileItem) it.next(); if (!fileItem.isFormField()) { /* The file item contains an uploaded file */ final String contentType = fileItem.getContentType(); final byte[] fileData = fileItem.get(); final String filename = fileItem.getName(); data = new WidgetData(contentType, filename, fileData); } } Manager.setWidgetData(providerName, uid, dataName, data); return String.format( "<script type=\"text/javascript\">window.parent.NXThemesWebWidgets.getUploader('%s', '%s', '%s').complete();</script>", providerName, uid, dataName); }
From source file:org.ofbiz.content.content.UploadContentAndImage.java
public static String uploadContentStuff(HttpServletRequest request, HttpServletResponse response) { try {/* www . ja va 2s .c o m*/ HttpSession session = request.getSession(); GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); ServletFileUpload dfu = new ServletFileUpload( new DiskFileItemFactory(10240, FileUtil.getFile("runtime/tmp"))); //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]DiskFileUpload " + dfu, module); List<FileItem> lst = null; try { lst = UtilGenerics.checkList(dfu.parseRequest(request)); } catch (FileUploadException e4) { request.setAttribute("_ERROR_MESSAGE_", e4.getMessage()); Debug.logError("[UploadContentAndImage.uploadContentAndImage] " + e4.getMessage(), module); return "error"; } //if (Debug.infoOn()) Debug.logInfo("[UploadContentAndImage]lst " + lst, module); if (lst.size() == 0) { request.setAttribute("_ERROR_MESSAGE_", "No files uploaded"); Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module); return "error"; } Map<String, Object> passedParams = FastMap.newInstance(); FileItem fi = null; FileItem imageFi = null; byte[] imageBytes = {}; passedParams.put("userLogin", userLogin); for (int i = 0; i < lst.size(); i++) { fi = lst.get(i); //String fn = fi.getName(); String fieldName = fi.getFieldName(); if (fi.isFormField()) { String fieldStr = fi.getString(); passedParams.put(fieldName, fieldStr); } else if (fieldName.startsWith("imageData")) { imageFi = fi; String fileName = fi.getName(); passedParams.put("drObjectInfo", fileName); String contentType = fi.getContentType(); passedParams.put("drMimeTypeId", contentType); imageBytes = imageFi.get(); passedParams.put(fieldName, imageBytes); if (Debug.infoOn()) { Debug.logInfo("[UploadContentAndImage]imageData: " + imageBytes.length, module); } } } if (Debug.infoOn()) { Debug.logInfo("[UploadContentAndImage]passedParams: " + passedParams, module); } // The number of multi form rows is retrieved int rowCount = UtilHttp.getMultiFormRowCount(request); if (rowCount < 1) { rowCount = 1; } TransactionUtil.begin(); for (int i = 0; i < rowCount; i++) { String suffix = "_o_" + i; if (i == 0) { suffix = ""; } String returnMsg = processContentUpload(passedParams, suffix, request); if (returnMsg.equals("error")) { try { TransactionUtil.rollback(); } catch (GenericTransactionException e2) { ServiceUtil.setMessages(request, e2.getMessage(), null, null); return "error"; } return "error"; } } TransactionUtil.commit(); } catch (Exception e) { Debug.logError(e, "[UploadContentAndImage] ", module); request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); try { TransactionUtil.rollback(); } catch (GenericTransactionException e2) { request.setAttribute("_ERROR_MESSAGE_", e2.getMessage()); return "error"; } return "error"; } return "success"; }
From source file:org.ofbiz.content.layout.LayoutWorker.java
/** * Uploads image data from a form and stores it in ImageDataResource. * Expects key data in a field identitified by the "idField" value * and the binary data to be in a field id'd by uploadField. *///from w w w . ja v a2 s .c om public static Map<String, Object> uploadImageAndParameters(HttpServletRequest request, String uploadField) { //Debug.logVerbose("in uploadAndStoreImage", ""); Locale locale = UtilHttp.getLocale(request); Map<String, Object> results = FastMap.newInstance(); Map<String, String> formInput = FastMap.newInstance(); results.put("formInput", formInput); ServletFileUpload fu = new ServletFileUpload( new DiskFileItemFactory(10240, new File(new File("runtime"), "tmp"))); List<FileItem> lst = null; try { lst = UtilGenerics.checkList(fu.parseRequest(request)); } catch (FileUploadException e4) { return ServiceUtil.returnError(e4.getMessage()); } if (lst.size() == 0) { String errMsg = UtilProperties.getMessage(err_resource, "layoutEvents.no_files_uploaded", locale); request.setAttribute("_ERROR_MESSAGE_", errMsg); //Debug.logWarning("[DataEvents.uploadImage] No files uploaded", module); return ServiceUtil .returnError(UtilProperties.getMessage(err_resource, "layoutEvents.no_files_uploaded", locale)); } // This code finds the idField and the upload FileItems FileItem fi = null; FileItem imageFi = null; for (int i = 0; i < lst.size(); i++) { fi = lst.get(i); String fieldName = fi.getFieldName(); String fieldStr = fi.getString(); if (fi.isFormField()) { formInput.put(fieldName, fieldStr); request.setAttribute(fieldName, fieldStr); //Debug.logVerbose("in uploadAndStoreImage, fieldName:" + fieldName + " fieldStr:" + fieldStr, ""); } if (fieldName.equals(uploadField)) { imageFi = fi; //MimeType of upload file results.put("uploadMimeType", fi.getContentType()); } } if (imageFi == null) { String errMsg = UtilProperties.getMessage(err_resource, "layoutEvents.image_null", UtilMisc.toMap("imageFi", imageFi), locale); request.setAttribute("_ERROR_MESSAGE_", errMsg); //Debug.logWarning("[DataEvents.uploadImage] imageFi(" + imageFi + ") is null", module); return null; } byte[] imageBytes = imageFi.get(); ByteBuffer byteWrap = ByteBuffer.wrap(imageBytes); results.put("imageData", byteWrap); results.put("imageFileName", imageFi.getName()); //Debug.logVerbose("in uploadAndStoreImage, results:" + results, ""); return results; }
From source file:org.ofbiz.product.imagemanagement.ImageManagementServices.java
public static String multipleUploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException { HttpSession session = request.getSession(true); GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); Map<String, String> formInput = FastMap.newInstance(); ServletFileUpload fu = new ServletFileUpload( new DiskFileItemFactory(10240, FileUtil.getFile("runtime/tmp"))); List<FileItem> lst = null; try {//from www . j ava2 s. com lst = UtilGenerics.checkList(fu.parseRequest(request)); } catch (FileUploadException e4) { return e4.getMessage(); } FileItem fi = null; FileItem imageFi = null; byte[] imageBytes = {}; for (int i = 0; i < lst.size(); i++) { fi = lst.get(i); String fieldName = fi.getFieldName(); if (fi.isFormField()) { String fieldStr = fi.getString(); formInput.put(fieldName, fieldStr); } else if (fieldName.startsWith("imageData")) { Map<String, Object> passedParams = FastMap.newInstance(); Map<String, Object> contentLength = FastMap.newInstance(); if (josonMap == null) { josonMap = FastList.newInstance(); } imageFi = fi; String fileName = fi.getName(); String contentType = fi.getContentType(); imageBytes = imageFi.get(); ByteBuffer byteWrap = ByteBuffer.wrap(imageBytes); passedParams.put("userLogin", userLogin); passedParams.put("productId", formInput.get("productId")); passedParams.put("productContentTypeId", "IMAGE"); passedParams.put("_uploadedFile_fileName", fileName); passedParams.put("_uploadedFile_contentType", contentType); passedParams.put("uploadedFile", byteWrap); passedParams.put("imageResize", formInput.get("imageResize")); contentLength.put("imageSize", imageFi.getSize()); josonMap.add(contentLength); if (passedParams.get("productId") != null) { try { dispatcher.runSync("addMultipleuploadForProduct", passedParams); } catch (GenericServiceException e) { Debug.logError(e, module); return e.getMessage(); } } } } return "success"; }