protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doPost(request, response); try { if (!ServletFileUpload.isMultipartContent(request)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } ServletFileUpload sfU = new ServletFileUpload(); FileItemIterator items = sfU.getItemIterator(request); while (items.hasNext()) { FileItemStream item = items.next(); if (!item.isFormField()) { InputStream stream = item.openStream(); Document doc = editor.toDocument(stream); stream.close(); handleDocument(request, response, doc); return; } } response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No XML uploaded"); } catch (FileUploadException fuE) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, fuE.getMessage()); } catch (JDOMException jE) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, jE.getMessage()); } }
private void callMethodForMultiPart(HttpServletRequest req, HttpServletResponse resp) throws Exception { String pinfo = req.getPathInfo(); int pos = pinfo.indexOf('.'); String cname = pinfo.substring(1, pos).replace('/', '.'); String mname = pinfo.substring(pos + 1); MultiPartMap map = new MultiPartMap(); FileItemIterator ite = new FileUpload().getItemIterator(req); while (ite.hasNext()) { FileItemStream item = ite.next(); if (item.isFormField()) { map.put(item.getFieldName(), IOUtil.streamToString(item.openStream(), "UTF-8")); } else { FileItem val = new FileItem( item.getFileName(), item.getContentType(), IOUtil.streamToBytes(item.openStream())); map.put(item.getFieldName(), val); } } Class clazz = Class.forName(cname); Class[] types = new Class[] {MultiPartMap.class}; Method method = clazz.getMethod(mname, types); if (method == null) { throw new RuntimeException("Not found method " + mname + "(Map)"); } Object result = method.invoke(null, map); resp.setContentType(MIME_HTML + ";charset=utf-8"); resp.getWriter().write(result.toString()); }