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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.opencms.ugc.CmsUgcUploadHelper.java

/**
 * Passes the form data with the given ID to the handler object, then removes it and deletes its stored data.<p>
 *
 * The form data is removed even if an exception is thrown while calling the form data handler.
 *
 * @param formDataId the id of the form data to process
 * @param handler the handler to which the form data should be passed
 * @throws Exception if something goes wrong
 *///from   ww  w. ja v  a2s  . c  om
public void consumeFormData(String formDataId, I_CmsFormDataHandler handler) throws Exception {

    List<FileItem> items = m_storedFormData.get(formDataId);

    if (items != null) {
        Map<String, I_CmsFormDataItem> itemMap = Maps.newHashMap();
        LOG.debug(formDataId + ": Processing file items");
        for (FileItem item : items) {
            LOG.debug(formDataId + ": " + item.toString());
            if (!item.isFormField() && CmsStringUtil.isEmptyOrWhitespaceOnly(item.getName())) {
                LOG.debug(formDataId + ": skipping previous file field because it is empty");
            } else {
                itemMap.put(item.getFieldName(), new CmsUgcDataItem(item));
            }
        }
        Exception storedException = null;
        try {
            handler.handleFormData(itemMap);
        } catch (Exception e) {
            storedException = e;
        }
        for (FileItem item : items) {
            item.delete();
        }
        m_storedFormData.remove(formDataId);
        if (storedException != null) {
            throw storedException;
        }
    }
}

From source file:org.tinygroup.weblayer.webcontext.parser.valueparser.impl.ParameterParserImpl.java

private Object[] processValues(String key, boolean isHtmlField, boolean[] filtering) {
    Object[] values = getObjects(key);
    List<Object> paramValues = new ArrayList<Object>();
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (value instanceof ItemFileObject) {
            FileItem fileItem = ((ItemFileObject) value).getFileItem();
            if (fileItem.isFormField()) {//???String
                value = fileItem.toString();
            }/*  ww  w. jav  a2 s.c  o  m*/
        }
        //??
        if (value instanceof String) {
            stringValueFilter(key, isHtmlField, filtering, value, paramValues);
        } else if (value instanceof ItemFileObject) {
            fileItemFilter(key, filtering, value, paramValues);
        }

    }
    if (paramValues.size() > 0) {
        if (paramValues.size() == 1) {
            webContext.put(key, paramValues.get(0));
        } else {
            webContext.put(key, paramValues.toArray());
        }
    }
    return paramValues.toArray();
}

From source file:org.xwoot.xwootApp.web.servlets.StateManagement.java

private boolean upload(FileItem item) throws ServletException {
    System.out.println(item.toString());
    if (item.getSize() == 0) {
        return false;
    }//from   ww  w  . ja  va  2  s. c o  m
    try {
        item.write(this.temp);
        return true;
    } catch (Exception e) {
        throw new ServletException(e);
    }
}

From source file:Servlet.TestServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    out.println("Hello<br/>");

    boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
    if (!isMultipartContent) {
        out.println("You are not trying to upload<br/>");
        return;/*  w  w w.  j  a v  a 2  s .c  o  m*/
    }
    out.println("You are trying to upload<br/>");

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List<FileItem> fields = upload.parseRequest(request);
        out.println("Number of fields: " + fields.size() + "<br/><br/>");
        Iterator<FileItem> it = fields.iterator();
        if (!it.hasNext()) {
            out.println("No fields found");
            return;
        }
        out.println("<table border=\"1\">");
        while (it.hasNext()) {
            out.println("<tr>");
            FileItem fileItem = it.next();
            boolean isFormField = fileItem.isFormField();
            if (isFormField) {
                out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName()
                        + "<br/>STRING: " + fileItem.getString());
                out.println("</td>");
            } else {
                out.println("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName()
                        + "<br/>STRING: " + fileItem.getString() + "<br/>NAME: " + fileItem.getName()
                        + "<br/>CONTENT TYPE: " + fileItem.getContentType() + "<br/>SIZE (BYTES): "
                        + fileItem.getSize() + "<br/>TO STRING: " + fileItem.toString());
                out.println("</td>");
            }
            out.println("</tr>");
        }
        out.println("</table>");
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
}

From source file:test.TestServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, FileUploadException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("Hello<br/>");

    boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
    if (!isMultipartContent) {
        out.println("You are not trying to upload<br/>");
        return;//from  w w w .  j a  v  a2 s.co m
    }
    out.println("You are trying to upload<br/>");

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    //      upload.setSizeMax(MAX_UPLOAD_IN_MEGS * 1024 * 1024);

    TestProgressListener testProgressListener = new TestProgressListener();
    upload.setProgressListener(testProgressListener);

    HttpSession session = request.getSession();
    session.setAttribute("testProgressListener", testProgressListener);

    try {
        List<FileItem> fields = upload.parseRequest(request);
        out.println("Number of fields: " + fields.size() + "<br/><br/>");
        Iterator<FileItem> it = fields.iterator();
        if (!it.hasNext()) {
            out.println("No fields found");
            return;
        }
        out.println("<table border=\"1\">");
        while (it.hasNext()) {
            out.println("<tr>");
            FileItem fileItem = it.next();
            boolean isFormField = fileItem.isFormField();
            if (isFormField) {
                out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName()
                        + "<br/>STRING: " + fileItem.getString());
                out.println("</td>");
            } else {
                out.println("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName() + //                     "<br/>STRING: " + fileItem.getString() +
                        "<br/>NAME: " + fileItem.getName() + "<br/>CONTENT TYPE: " + fileItem.getContentType()
                        + "<br/>SIZE (BYTES): " + fileItem.getSize() + "<br/>TO STRING: "
                        + fileItem.toString());
                out.println("</td>");
            }
            out.println("</tr>");
        }
        out.println("</table>");
    } catch (FileUploadException e) {
        out.println("Error: " + e.getMessage());
    }
}