public static ClassifiableContentIF getFileUploadContent(HttpServletRequest request) { // Handle file upload String contentType = request.getHeader("content-type"); // out.write("CT: " + contentType + " " + tm + " " + id); if (contentType != null && contentType.startsWith("multipart/form-data")) { try { FileUpload upload = new FileUpload(new DefaultFileItemFactory()); for (FileItem item : upload.parseRequest(request)) { if (item.getSize() > 0) { // ISSUE: could make use of content type if known byte[] content = item.get(); ClassifiableContent cc = new ClassifiableContent(); String name = item.getName(); if (name != null) cc.setIdentifier("fileupload:name:" + name); else cc.setIdentifier("fileupload:field:" + item.getFieldName()); cc.setContent(content); return cc; } } } catch (Exception e) { throw new OntopiaRuntimeException(e); } } return null; }
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Logger logger = MiscUtils.getLogger(); String simulationData = null; boolean simulationError = false; try { FileUpload upload = new FileUpload(new DefaultFileItemFactory()); @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(request); for(FileItem item:items) { if(item.isFormField()) { String name = item.getFieldName(); if(name.equals("simulateError")) { simulationError=true; } } else { if(item.getFieldName().equals("simulateFile")) { InputStream is = item.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); simulationData = writer.toString(); } } } if(simulationData != null && simulationData.length()>0) { if(simulationError) { Driver.readResponseFromXML(request, simulationData); simulationData = (String)request.getAttribute("olisResponseContent"); request.getSession().setAttribute("errors",request.getAttribute("errors")); } request.getSession().setAttribute("olisResponseContent", simulationData); request.setAttribute("result", "File successfully uploaded"); } }catch(Exception e) { MiscUtils.getLogger().error("Error",e); } return mapping.findForward("success"); }
/** * INTERNAL: Builds the Parameters object from an HttpServletRequest * object. * @since 2.0 */ public static Parameters decodeParameters(HttpServletRequest request, String charenc) throws ServletException, IOException { String ctype = request.getHeader("content-type"); log.debug("Content-type: " + ctype); Parameters params = new Parameters(); if (ctype != null && ctype.startsWith("multipart/form-data")) { // special file upload request, so use FileUpload to decode log.debug("Decoding with FileUpload; charenc="+ charenc); try { FileUpload upload = new FileUpload(new DefaultFileItemFactory()); Iterator iterator = upload.parseRequest(request).iterator(); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); log.debug("Reading: " + item); if (item.isFormField()) { if (charenc != null) params.addParameter(item.getFieldName(), item.getString(charenc)); else params.addParameter(item.getFieldName(), item.getString()); } else params.addParameter(item.getFieldName(), new FileParameter(item)); } } catch (FileUploadException e) { throw new ServletException(e); } } else { // ordinary web request, so retrieve info and stuff into Parameters object log.debug("Normal parameter decode, charenc=" + charenc); if (charenc != null) request.setCharacterEncoding(charenc); Enumeration enumeration = request.getParameterNames(); while (enumeration.hasMoreElements()) { String param = (String) enumeration.nextElement(); params.addParameter(param, request.getParameterValues(param)); } } return params; }