Example usage for org.apache.commons.fileupload FileItem isFormField

List of usage examples for org.apache.commons.fileupload FileItem isFormField

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem isFormField.

Prototype

boolean isFormField();

Source Link

Document

Determines whether or not a FileItem instance represents a simple form field.

Usage

From source file:com.globalsight.everest.webapp.pagehandler.administration.vendors.VendorHelper.java

/**
 * Save the request parameters from the CV/Resume page
 *///from  ww w.  ja  v a2s. c o m
public static void saveCV(Vendor vendor, HttpServletRequest request) throws EnvoyServletException {
    // Create a new file upload handler
    DiskFileUpload upload = new DiskFileUpload();

    String radioValue = null;
    String resumeText = null;
    boolean doUpload = false;
    byte[] data = null;
    String filename = null;
    // Parse the request
    try {
        List /* FileItem */ items = upload.parseRequest(request);
        // 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 = EditUtil.utf8ToUnicode(item.getString());
                if (name.equals("radioBtn")) {
                    radioValue = value;
                } else if (name.equals("resumeText")) {
                    resumeText = value;
                }
            } else {
                filename = item.getName();
                if (filename == null || filename.equals("")) {
                    // user hit done button but didn't modify the page
                    continue;
                } else {
                    doUpload = true;
                    data = item.get();
                }
            }
        }
        if (radioValue != null) {
            if (radioValue.equals("doc") && doUpload) {
                vendor.setResume(filename, data);
                try {
                    ServerProxy.getVendorManagement().saveResumeFile(vendor);
                } catch (Exception e) {
                    throw new EnvoyServletException(e);
                }
            } else if (radioValue.equals("text")) {
                vendor.setResume(resumeText);
            }
        }
    } catch (FileUploadException fe) {
        throw new EnvoyServletException(fe);
    }
}

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  va2  s  .  c o  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.uniquesoft.uidl.servlet.UploadServlet.java

/**
 * Removes all FileItems stored in session under the session key, but in this case 
 * the user can specify whether the temporary data is removed from disk.
 * /*  w w  w .  j  a v  a  2s  . c  om*/
 * @param request
 * @param removeData 
 *                    true: the file data is deleted.
 *                    false: use it when you are referencing file items 
 *                    instead of copying them.
 */
public static void removeSessionFileItems(HttpServletRequest request, String sessionFilesKey,
        boolean removeData) {
    logger.debug("UPLOAD-SERVLET (" + request.getSession().getId() + ") removeSessionFileItems: removeData="
            + removeData);
    List<FileItem> sessionFiles = getSessionFileItems(request, sessionFilesKey);
    if (removeData && sessionFiles != null) {
        for (FileItem fileItem : sessionFiles) {
            if (fileItem != null && !fileItem.isFormField()) {
                fileItem.delete();
            }
        }
    }
    request.getSession().removeAttribute(sessionFilesKey);
}

From source file:com.certus.actions.uploadSliderImageAction.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = getServletContext().getRealPath("img/slider").replace("build/", "");
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {/*  www .  ja  v  a  2s  . c o m*/
            List<FileItem> multiparts = upload.parseRequest(request);
            StringBuilder sb = null;
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    double randomA = Math.random() * 1000000000;
                    int randA = (int) randomA;
                    String name = new File(item.getName()).getName();
                    sb = new StringBuilder(name);
                    sb.replace(0, name.length() - 4, "slider-" + randA);
                    item.write(new File(path + File.separator + sb));
                }
            }
            String pathTo = path + File.separator + sb;
            response.getWriter().write(pathTo.substring(pathTo.lastIndexOf("img")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:com.rampukar.controller.FileUploadHandler.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    //process only if its multipart content
    if (ServletFileUpload.isMultipartContent(request)) {
        try {/*w  ww. j  av a2s . c o  m*/
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                    out.print(name);
                }
            }

            //File uploaded successfully
            request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
            request.setAttribute("message", "File Upload Failed due to " + ex);
        }

    } else {
        //            request.setAttribute("message", "Sorry this Servlet only handles file upload request");
    }

    //        request.getRequestDispatcher("/result.jsp").forward(request, response);
}

From source file:com.tubes2.FileUploadHandler.FileUploadHandler.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //process only if its multipart content
    if (ServletFileUpload.isMultipartContent(request)) {
        try {/*from  w w w  .  j  a va 2s. c  o  m*/
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }

            //File uploaded successfully
            request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
            request.setAttribute("message", "File Upload Failed due to " + ex);
        }

    } else {
        request.setAttribute("message", "Sorry this Servlet only handles file upload request");
    }

    request.getRequestDispatcher("/result.jsp").forward(request, response);

}

From source file:com.softwarementors.extjs.djn.router.processor.standard.form.upload.UploadFormPostRequestProcessorTest.java

private FileItem mockFileItem(String name, String value) {
    FileItem fileItem = mock(FileItem.class);
    when(fileItem.isFormField()).thenReturn(true);
    when(fileItem.getFieldName()).thenReturn(name);
    when(fileItem.getString()).thenReturn(value);
    return fileItem;
}

From source file:com.epam.wilma.webapp.config.servlet.stub.upload.MultiPartFileParser.java

/**
 * Parses a list of multipart files and sends them to {@link MultiPartFileProcessor}.
 * @param fields a list of multipart files that will be processed
 * @return with the processing status message or "No file uploaded" when the list is empty
 * @throws IOException was thrown file parsing failed
 *///from  w ww . ja va 2 s  .  c o m
public String parseMultiPartFiles(final List<FileItem> fields) throws IOException {
    String msg = "";
    Iterator<FileItem> it = fields.iterator();
    if (!fields.isEmpty() && it.hasNext()) {
        while (it.hasNext()) {
            FileItem fileItem = it.next();
            if (!fileItem.isFormField()) {
                String uploadedFileName = fileItem.getName();
                InputStream uploadedResource = fileItem.getInputStream();
                String contentType = fileItem.getContentType();
                String fieldName = fileItem.getFieldName();
                msg += multiPartFileProcessor.processUploadedFile(uploadedResource, contentType, fieldName,
                        uploadedFileName);
            }
        }
    } else {
        msg = "No file uploaded";
    }

    return msg;
}

From source file:com.w4t.engine.requests.FileUploadRequest.java

public String getParameter(String name) {
    String result = null;//from w  ww  . j  a v  a2  s.  c  om
    FileItem item = (FileItem) parameters.get(name);
    if (item != null && item.isFormField()) {
        result = item.getString();
    }
    return result;
}

From source file:com.UploadAction.java

public String execute() throws Exception {
    try {/*from   w w w  .  j  ava  2  s .  co  m*/
        //long maxFileSize = (2 * 1024 * 1024);
        //int maxMemSize = (2 * 1024 * 1024);
        //final String path = "/tmp";
        HttpServletRequest request = ServletActionContext.getRequest();
        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        if (isMultiPart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(request);
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem fileItem = iter.next();
                if (fileItem.isFormField()) {
                    //processFormField(fileItem);
                } else {
                    flItem = fileItem;
                }
            }
        }
        HttpSession session = ServletActionContext.getRequest().getSession(false);
        String User = (String) session.getAttribute("username");
        Connection con = Connections.conn();
        PreparedStatement stat = con.prepareStatement("update user set image=? where UserName=?");
        stat.setString(2, User);
        stat.setBinaryStream(1, flItem.getInputStream(), (int) flItem.getSize());
        // stat.setBinaryStream(4, (InputStream) itemPhoto.getInputStream(), (int) itemPhoto.getSize());
        int rows = stat.executeUpdate();
        if (rows > 0) {
            return "success";
        }
    } catch (Exception e) {
    }
    return "success";
}