List of usage examples for org.apache.commons.fileupload FileItem getFieldName
String getFieldName();
From source file:com.jyhon.servlet.user.SignInServlet.java
private UserEntity getUserEntity(List<FileItem> items) { UserEntity userEntity = new UserEntity(); for (FileItem item : items) { if (item.isFormField()) { String fileName = item.getFieldName(); if (fileName.equals("name")) { userEntity.setUserName(item.getString()); } else if (fileName.equals("psw")) { userEntity.setPassword(item.getString()); } else if (fileName.equals("tel")) { userEntity.setTel(item.getString()); } else if (fileName.equals("idCard")) { userEntity.setIdCard(item.getString()); }/*w ww.ja v a2 s . c om*/ System.out.print(item.getFieldName() + ":"); System.out.println(item.getString()); } else { String pic = item.getFieldName(); userEntity.setLicense(item.getName()); System.out.println(item.getName()); } } return userEntity; }
From source file:by.creepid.jsf.fileupload.UploadFilter.java
private void processFileField(FileItem fileField, HttpServletRequest request) { request.setAttribute(fileField.getFieldName(), fileField); }
From source file:controller.addGame.java
private void processFormField(FileItem item) { if (item.getFieldName().equals("AppName")) { AppName = item.getString();// w ww. jav a 2 s.c o m } else if (item.getFieldName().equals("RecRam")) { String a = item.getString(); RecRam = Integer.parseInt(a); } else if (item.getFieldName().equals("Code")) { Code = item.getString(); } else if (item.getFieldName().equals("MinCpu")) { MinCpu = item.getString(); } else if (item.getFieldName().equals("RecCpu")) { RecCpu = item.getString(); } else if (item.getFieldName().equals("MinGpu")) { MinGpu = item.getString(); } else if (item.getFieldName().equals("RecGpu")) { RecGpu = item.getString(); } else if (item.getFieldName().equals("MinCpuLvl")) { String a = item.getString(); MinCpuLvl = Integer.parseInt(a); } else if (item.getFieldName().equals("RecCpuLvl")) { String a = item.getString(); RecCpuLvl = Integer.parseInt(a); } else if (item.getFieldName().equals("MinGpuLvl")) { String a = item.getString(); MinGpuLvl = Integer.parseInt(a); } else if (item.getFieldName().equals("RecGpuLvl")) { String a = item.getString(); RecGpuLvl = Integer.parseInt(a); } else if (item.getFieldName().equals("MinRam")) { String a = item.getString(); MinRam = Integer.parseInt(a); } }
From source file:edu.caltech.ipac.firefly.server.servlets.WebPlotServlet.java
protected void processRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List /* FileItem */ items = upload.parseRequest(req); Map<String, String> params = new HashMap<String, String>(10); // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); params.put(name, value);/*from w w w.ja v a 2s .c om*/ } else { String fieldName = item.getFieldName(); try { File uf = new File(ServerContext.getTempWorkDir(), System.currentTimeMillis() + ".upload"); item.write(uf); params.put(fieldName, uf.getPath()); } catch (Exception e) { sendReturnMsg(res, 500, e.getMessage(), null); return; } } } // String result= ServerCommandAccess.doCommand(params); // sendReturnMsg(res, 200, "results", result); // test code.. // if (doTest) { // MultiPartPostBuilder builder = new MultiPartPostBuilder( // "http://localhost:8080/applications/Spitzer/SHA/servlet/Firefly_MultiPartHandler"); // builder.addParam("dummy1", "boo1"); // builder.addParam("dummy2", "boo2"); // builder.addParam("dummy3", "boo3"); // for(UploadFileInfo fi : data.getFiles()) { // builder.addFile(fi.getPname(), fi.getFile()); // } // StringWriter sw = new StringWriter(); // MultiPartPostBuilder.MultiPartRespnse pres = builder.post(sw); // LOG.briefDebug("uploaded status: " + pres.getStatusMsg()); // LOG.debug("uploaded response: " + sw.toString()); // } }
From source file:commands.SendAnalysisRequest.java
@Override public void execute(HttpServletRequest request, HttpServletResponse response, Controller controller) { //http://commons.apache.org/proper/commons-fileupload/using.html //process only if its multipart content String page = "Problem.jsp"; if (ServletFileUpload.isMultipartContent(request)) { try {/*from ww w. ja v a2 s .com*/ // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Configure a repository (to ensure a secure temp location is used) ServletContext servletContext = controller.getServletConfig().getServletContext(); File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); System.out.println("File repository absolute path: " + repository.getAbsolutePath()); factory.setRepository(repository); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); //parametros chave-valor (path valor) HashMap<String, String[]> params = new HashMap<String, String[]>(); //parametros chave-valor para multimedia (path e array de bytes) HashMap<String, byte[]> mediaParams = new HashMap<String, byte[]>(); // Tratando todos os parametros/itens da pagina (arquivos e no-arquivos) List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { //key (path) FileItem item = iter.next(); String key = item.getFieldName(); if (item.isFormField()) { //printFormField(item); //value String[] value = new String[1]; value[0] = item.getString(); params.put(key, value); } else { //printUploadedFile(item); byte[] value = item.get(); mediaParams.put(key, value); } } //File uploaded successfully // request.setAttribute("message", "File Uploaded Successfully"); long result = new ParamedicController().sendAnalysisRequest(params, 1, mediaParams); if (result == -1) { request.setAttribute("message", "Occurred a problem to sending analysis request"); } else { request.setAttribute("idAnalysis", result); request.setAttribute("message", "Analysis request sent successfully"); page = "AnalysisResponseSearch.jsp"; //RequestDispatcher reqDispatcher = request.getRequestDispatcher("AnalysisResponseSearch.jsp"); //reqDispatcher.forward(request, response); } // request.getRequestDispatcher("AnalysisResponseSearch.jsp").forward(request, response); } catch (Exception ex) { request.setAttribute("message", "File Upload Failed due to " + ex); ex.printStackTrace(); } } else { request.setAttribute("message", "Sorry this Servlet only handles file upload request"); } try { request.getRequestDispatcher(page).forward(request, response); } catch (ServletException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:de.ingrid.portal.forms.ContactForm.java
public void populate(List<FileItem> items) throws UnsupportedEncodingException { if (items != null) { clearInput();//ww w. ja v a2 s . c o m for (FileItem item : items) { if (item.getFieldName().equals(FIELD_MESSAGE)) { setInput(FIELD_MESSAGE, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_FIRSTNAME)) { setInput(FIELD_FIRSTNAME, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_LASTNAME)) { setInput(FIELD_LASTNAME, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_COMPANY)) { setInput(FIELD_COMPANY, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_PHONE)) { setInput(FIELD_PHONE, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_EMAIL)) { setInput(FIELD_EMAIL, item.getString("UTF-8")); } else if (item.getFieldName().equals(FIELD_ACTIVITY)) { setInput(FIELD_ACTIVITY, item.getString()); } else if (item.getFieldName().equals(FIELD_INTEREST)) { setInput(FIELD_INTEREST, item.getString()); } else if (item.getFieldName().equals(FIELD_NEWSLETTER)) { setInput(FIELD_NEWSLETTER, item.getString()); } else if (item.getFieldName().equals(FIELD_JCAPTCHA)) { setInput(FIELD_JCAPTCHA, item.getString()); } } } }
From source file:info.magnolia.cms.filters.CommonsFileUploadMultipartRequestFilter.java
/** * Add the <code>FileItem</code> as a paramter into the <code>MultipartForm</code>. */// w w w . j a va 2 s. c om private void addField(HttpServletRequest request, FileItem item, MultipartForm form) { String name = item.getFieldName(); String value; try { String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8"); value = item.getString(encoding); } catch (UnsupportedEncodingException ex) { value = item.getString(); } form.addParameter(name, value); String[] values = request.getParameterValues(name); if (values != null) { form.addparameterValues(name, values); } }
From source file:de.mpg.mpdl.service.rest.screenshot.service.HtmlScreenshotService.java
/** * Return the Field (param) with the given name as a {@link FileItem} * /* w w w.j a va 2 s .co m*/ * @param items * @param name * @return */ private String getFieldValue(List<FileItem> items, String name) { for (FileItem item : items) { if (item.isFormField() && name.equals(item.getFieldName())) { return item.getString(); } } return null; }
From source file:fr.gael.dhus.server.http.webapp.api.controller.UploadController.java
@PreAuthorize("hasRole('ROLE_UPLOAD')") @RequestMapping(value = "/upload", method = { RequestMethod.POST }) public void upload(Principal principal, HttpServletRequest req, HttpServletResponse res) throws IOException { // process only multipart requests if (ServletFileUpload.isMultipartContent(req)) { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request try {/*from w w w.j a va 2 s .co m*/ ArrayList<String> collectionIds = new ArrayList<>(); FileItem product = null; List<FileItem> items = upload.parseRequest(req); for (FileItem item : items) { if (COLLECTIONSKEY.equals(item.getFieldName())) { if (item.getString() != null && !item.getString().isEmpty()) { for (String cid : item.getString().split(",")) { collectionIds.add(cid); } } } else if (PRODUCTKEY.equals(item.getFieldName())) { product = item; } } if (product == null) { res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Your request is missing a product file to upload."); return; } productUploadService.upload(product, collectionIds); res.setStatus(HttpServletResponse.SC_CREATED); res.getWriter().print("The file was created successfully."); res.flushBuffer(); } catch (FileUploadException e) { LOGGER.error("An error occurred while parsing request.", e); res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occurred while parsing request : " + e.getMessage()); } catch (UserNotExistingException e) { LOGGER.error("You need to be connected to upload a product.", e); res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You need to be connected to upload a product."); } catch (UploadingException e) { LOGGER.error("An error occurred while uploading the product.", e); res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occurred while uploading the product : " + e.getMessage()); } catch (RootNotModifiableException e) { LOGGER.error("An error occurred while uploading the product.", e); res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occurred while uploading the product : " + e.getMessage()); } catch (ProductNotAddedException e) { LOGGER.error("Your product can not be read by the system.", e); res.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Your product can not be read by the system."); } } else { res.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "Request contents type is not supported by the servlet."); } }
From source file:gr.forth.ics.isl.x3mlEditor.upload.MultipartUploadParser.java
private void parseFormFields(List<FileItem> items) { for (FileItem item : items) { if (item.isFormField()) { String key = item.getFieldName(); String value = item.getString(); if (StringUtils.isNotBlank(key)) { params.put(key.toLowerCase(), StringUtils.defaultString(value)); }/*from w w w.j a v a 2s . c om*/ } else { files.add(item); } } }