List of usage examples for org.apache.commons.fileupload FileItem getFieldName
String getFieldName();
From source file:azkaban.web.MultipartParser.java
@SuppressWarnings("unchecked") public Map<String, Object> parseMultipart(HttpServletRequest request) throws IOException, ServletException { ServletFileUpload upload = new ServletFileUpload(_uploadItemFactory); List<FileItem> items = null; try {/*w w w . j a v a2 s .co m*/ items = upload.parseRequest(request); } catch (FileUploadException e) { throw new ServletException(e); } Map<String, Object> params = new HashMap<String, Object>(); for (FileItem item : items) { if (item.isFormField()) params.put(item.getFieldName(), item.getString()); else params.put(item.getFieldName(), item); } return params; }
From source file:com.w4t.engine.requests.FileUploadRequest.java
public FileUploadRequest(final HttpServletRequest request) throws ServletException { super(request); IConfiguration configuration = ConfigurationReader.getConfiguration(); IFileUpload fileUpload = configuration.getFileUpload(); UploadRequestFileItemFactory factory = new UploadRequestFileItemFactory(); DiskFileUpload upload = new DiskFileUpload(factory); upload.setSizeThreshold(fileUpload.getMaxMemorySize()); upload.setSizeMax(fileUpload.getMaxMemorySize()); upload.setRepositoryPath(FileUploadRequest.SYSTEM_TEMP_DIR); try {/* w w w .j av a 2s. c o m*/ List items = upload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); parameters.put(item.getFieldName(), item); } } catch (FileUploadException e) { throw new ServletExceptionAdapter(e); } }
From source file:hudson.util.MultipartFormDataParser.java
public MultipartFormDataParser(HttpServletRequest request) throws ServletException { try {/*w ww .j a va 2s. c o m*/ for (FileItem fi : (List<FileItem>) upload.parseRequest(request)) byName.put(fi.getFieldName(), fi); } catch (FileUploadException e) { throw new ServletException(e); } }
From source file:de.micromata.genome.gwiki.page.impl.actionbean.CommonMultipartRequest.java
public void addFormField(FileItem item) { final String fn = item.getFieldName(); final String fv; try {// ww w . j ava2 s .co m fv = item.getString("UTF-8"); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } if (multipartParams.containsKey(fn) == false) { multipartParams.put(fn, new String[] { fv }); } else { String[] ci = multipartParams.get(fn); multipartParams.put(fn, (String[]) ArrayUtils.add(ci, fv)); } }
From source file:ilarkesto.form.UploadFormField.java
@Override public void update(Map<String, String> data, Collection<FileItem> uploadedFiles) { maxFileSizeExceeded = false;/*from w ww . j a va2 s.c om*/ for (FileItem item : uploadedFiles) { if (item.getFieldName().equals(getName())) { if (item.getSize() == 0) { file = null; return; } if (maxFilesize != null && item.getSize() > maxFilesize) { maxFileSizeExceeded = true; return; } file = new File(applicationTempDir + "/" + folderIdGenerator.generateId() + "/" + item.getName()); file.getParentFile().mkdirs(); try { item.write(file); } catch (Exception ex) { throw new RuntimeException(ex); } } } }
From source file:com.exilant.exility.core.HttpUtils.java
/*** * Simplest way of handling files that are uploaded from form. Just put them * as name-value pair into service data. This is useful ONLY if the files * are reasonably small./*from ww w. j a va2 s . c o m*/ * * @param req * @param data * data in which */ public void simpleExtract(HttpServletRequest req, ServiceData data) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { /** * ServerFileUpload still deals with raw types. we have to have * workaround that */ List<?> list = upload.parseRequest(req); if (list != null) { for (Object item : list) { if (item instanceof FileItem) { FileItem fileItem = (FileItem) item; data.addValue(fileItem.getFieldName(), fileItem.getString()); } else { Spit.out("Servlet Upload retruned an item that is not a FileItem. Ignorinig that"); } } } } catch (FileUploadException e) { Spit.out(e); data.addError("Error while parsing form data. " + e.getMessage()); } }
From source file:com.artofsolving.jodconverter.web.DocumentConverterServlet.java
private FileItem getInputFileUpload(HttpServletRequest request, ServletFileUpload fileUpload) throws ServletException { FileItem inputFileUpload = null;/*ww w. j a v a 2 s. co m*/ try { List fileItems = fileUpload.parseRequest(request); for (Iterator it = fileItems.iterator(); it.hasNext();) { FileItem item = (FileItem) it.next(); if ("inputDocument".equals(item.getFieldName())) { inputFileUpload = item; } } } catch (FileUploadException e) { throw new ServletException("file upload failed", e); } return inputFileUpload; }
From source file:com.adobe.epubcheck.web.EpubCheckServlet.java
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); if (!ServletFileUpload.isMultipartContent(req)) { out.println("Invalid request type"); return;//from w ww . j a v a2s .c o m } try { DiskFileItemFactory itemFac = new DiskFileItemFactory(); // itemFac.setSizeThreshold(20000000); // bytes File repositoryPath = new File("upload"); repositoryPath.mkdir(); itemFac.setRepository(repositoryPath); ServletFileUpload servletFileUpload = new ServletFileUpload(itemFac); List fileItemList = servletFileUpload.parseRequest(req); Iterator list = fileItemList.iterator(); FileItem book = null; while (list.hasNext()) { FileItem item = (FileItem) list.next(); String paramName = item.getFieldName(); if (paramName.equals("file")) book = item; } if (book == null) { out.println("Invalid request: no epub uploaded"); return; } File bookFile = File.createTempFile("work", "epub"); book.write(bookFile); EpubCheck epubCheck = new EpubCheck(bookFile, out); if (epubCheck.validate()) out.println("No errors or warnings detected"); book.delete(); } catch (Exception e) { out.println("Internal Server Error"); e.printStackTrace(out); } }
From source file:MainServer.UploadServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); DiskFileItemFactory factory = new DiskFileItemFactory(); String upload = this.getServletContext().getRealPath("/upload/"); String temp = System.getProperty("java.io.tmpdir"); factory.setSizeThreshold(1024 * 1024 * 5); factory.setRepository(new File(temp)); ServletFileUpload servleFileUplaod = new ServletFileUpload(factory); try {//from w w w . j a v a 2 s . c om List<FileItem> list = servleFileUplaod.parseRequest(request); for (FileItem item : list) { String name = item.getFieldName(); InputStream is = item.getInputStream(); if (name.contains("content")) { System.out.println(inputStream2String(is)); } else { if (name.contains("file")) { try { inputStream2File(is, upload + "\\" + item.getName()); } catch (Exception e) { } } } } out.write("success"); } catch (FileUploadException | IOException e) { out.write("failure"); } out.flush(); out.close(); }
From source file:com.sa.osgi.jetty.UploadServlet.java
private void processUploadedFile(FileItem item) { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); System.out.println("file name: " + fileName); try {/* w ww . j ava 2 s .c o m*/ // InputStream inputStream = item.getInputStream(); // FileOutputStream fout = new FileOutputStream("/tmp/aa"); // fout.write(inputStream.rea); String newFileName = "/tmp/" + fileName; item.write(new File(newFileName)); ServiceFactory factory = ServiceFactory.INSTANCE; MaoService maoService = factory.getMaoService(); boolean b = maoService.installBundle(newFileName); System.out.println("Installation Result: " + b); } catch (IOException ex) { Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex); } }