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

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

Introduction

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

Prototype

boolean isFormField();

Source Link

Document

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

Usage

From source file:io.lightlink.servlet.MultipartParameters.java

private void parseUpToNextStream() throws FileUploadException, IOException {
    while (fileItemIterator.hasNext()) {
        FileItemStream fItemStream = fileItemIterator.next();
        String name = fItemStream.getFieldName();
        if (fItemStream.isFormField()) {
            paramsMap.put(name, Streams.asString(fItemStream.openStream()));
        } else {//  w w w  . j a  v a  2 s.c o  m
            this.fileItemStream = fItemStream;
            break; // returns and waits while the the stream is consumed
        }
    }
}

From source file:com.boazlev.fnf.web.IndexerServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/xml;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.println("<?xml-stylesheet type=\"text/xsl\" href=\"out.xslt\"?>");
    out.println("<catalog>");

    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload fileUpload = new ServletFileUpload(factory);
    try {//from w  w w .  j a  v  a 2s  . c om
        FileItemIterator iterator = fileUpload.getItemIterator(request);
        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream mathMLContent = item.openStream();
            if (!item.isFormField()) {
                Map<String, Integer> index = indexer.index(mathMLContent, "./lines.txt");
                for (Map.Entry<String, Integer> mapEntry : index.entrySet()) {
                    out.println("<cd>");
                    out.print("<index>");
                    out.print(mapEntry.getKey());
                    out.println("</index>");
                    out.print("<count>");
                    out.print(mapEntry.getValue());
                    out.println("</count>");
                    out.println("</cd>");
                }
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }
    out.print("</catalog>");
}

From source file:in.co.sneh.controller.CargaExcelRural.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  ww  . j a  v a2s . c o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        CargaExcelReqRural lee = new CargaExcelReqRural();
        String Unidad = "";

        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        if (isMultiPart) {
            ServletFileUpload upload = new ServletFileUpload();
            try {
                HttpSession sesion = request.getSession(true);
                FileItemIterator itr = upload.getItemIterator(request);
                while (itr.hasNext()) {
                    FileItemStream item = itr.next();
                    if (item.isFormField()) {
                        String fielName = item.getFieldName();
                        InputStream is = item.openStream();
                        byte[] b = new byte[is.available()];
                        is.read(b);
                        String value = new String(b);
                        response.getWriter().println(fielName + ":" + value + "<br/>");
                    } else {
                        String path = getServletContext().getRealPath("/");
                        if (CargaExcelRuralModel.processFile(path, item)) {
                            //response.getWriter().println("file uploaded successfully");
                            if (lee.obtieneArchivo(path, item.getName())) {
                                out.println("<script>alert('Se carg el Folio Correctamente')</script>");
                                out.println(
                                        "<script>window.location='facturacionRural/cargaRequerimento.jsp'</script>");
                            }
                            //response.sendRedirect("cargaFotosCensos.jsp");
                        } else {
                            //response.getWriter().println("file uploading falied");
                            //response.sendRedirect("cargaFotosCensos.jsp");
                        }
                    }
                }
            } catch (FileUploadException fue) {
                fue.printStackTrace();
            }
            out.println("<script>alert('No se pudo cargar el Folio, verifique las celdas')</script>");
            out.println("<script>window.location='requerimiento.jsp'</script>");
            //response.sendRedirect("carga.jsp");
        }
    } finally {
        out.close();
    }
}

From source file:com.jythonui.server.upload.UpLoadFile.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ServletFileUpload upload = new ServletFileUpload();

    IAddNewBlob addB = SHolder.getAddBlob();

    PrintWriter out = response.getWriter();
    boolean first = true;
    try {/*w ww.java  2  s.co  m*/
        FileItemIterator iter = upload.getItemIterator(request);

        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            // only uploaded files
            if (item.isFormField())
                continue;

            String fName = item.getName();
            // nothing uploaded
            if (CUtil.EmptyS(fName))
                continue;
            InputStream stream = item.openStream();
            // may be set initial size not default
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int len;
            while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                bout.write(buffer, 0, len);
            }
            bout.close();
            // store blob content
            String bkey = addB.addNewBlob(ICommonConsts.BLOBUPLOAD_REALM, ICommonConsts.BLOBUPLOAD_KEY,
                    bout.toByteArray());
            if (!first)
                out.print(',');
            first = false;
            out.print(ICommonConsts.BLOBUPLOAD_REALM);
            out.print(':');
            out.print(bkey);
            out.print(':');
            out.print(fName);
        } // while

    } catch (Exception e) {
        out.print(ICommonConsts.UPLOADFILEERROR);
        IGetLogMess iLog = SHolder.getM();
        String mess = iLog.getMess(IErrorCode.ERRORCODE77, ILogMess.ERRORWHILEUPLOADING);
        log.log(Level.SEVERE, mess, e);
    }
    out.close();

}

From source file:fedora.server.management.UploadServlet.java

/**
 * The servlet entry point. http://host:port/fedora/management/upload
 *//*from   w  ww.j a va  2 s.  c  o m*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Context context = ReadOnlyContext.getContext(Constants.HTTP_REQUEST.REST.uri, request);
    try {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();

        // Parse the request, looking for "file"
        InputStream in = null;
        FileItemIterator iter = upload.getItemIterator(request);
        while (in == null && iter.hasNext()) {
            FileItemStream item = iter.next();
            LOG.info("Got next item: isFormField=" + item.isFormField() + " fieldName=" + item.getFieldName());
            if (!item.isFormField() && item.getFieldName().equals("file")) {
                in = item.openStream();
            }
        }
        if (in == null) {
            sendResponse(HttpServletResponse.SC_BAD_REQUEST, "No data sent.", response);
        } else {
            sendResponse(HttpServletResponse.SC_CREATED, s_management.putTempStream(context, in), response);
        }
    } catch (AuthzException ae) {
        throw RootException.getServletException(ae, request, "Upload", new String[0]);
    } catch (Exception e) {
        e.printStackTrace();
        sendResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                e.getClass().getName() + ": " + e.getMessage(), response);
    }
}

From source file:com.vmware.photon.controller.api.frontend.resources.image.ImagesResource.java

private void flushRequest(FileItemIterator iterator, InputStream itemStream) {
    try {/*from  w  ww . j a  va2 s .  c  o  m*/
        // close any streams left open due to error.
        if (itemStream != null) {
            itemStream.close();
        }

        // iterate through the remaining fields an flush the fields that contain the file data.
        while (null != iterator && iterator.hasNext()) {
            FileItemStream item = iterator.next();
            if (!item.isFormField()) {
                item.openStream().close();
            }
        }
    } catch (IOException | FileUploadException ex) {
        logger.warn("Unexpected exception flushing upload request.", ex);
    }
}

From source file:com.runwaysdk.web.WebFileUploadServlet.java

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    ClientRequestIF clientRequest = (ClientRequestIF) req.getAttribute(ClientConstants.CLIENTREQUEST);

    // capture the session id
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);

    if (!isMultipart) {
        // TODO Change exception type
        String msg = "The HTTP Request must contain multipart content.";
        throw new RuntimeException(msg);
    }/*ww  w . ja v  a  2s.  c  o  m*/

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload();

    upload.setFileItemFactory(factory);

    try {
        // Parse the request
        FileItemIterator iter = upload.getItemIterator(req);

        String fileName = null;
        String extension = null;
        InputStream stream = null;
        String uploadPath = null;
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            InputStream input = item.openStream();
            if (item.isFormField() && item.getFieldName().equals(WEB_FILE_UPLOAD_PATH_FIELD_NAME)) {
                uploadPath = Streams.asString(input);
            } else if (!item.isFormField()) {
                String fullName = item.getName();
                int extensionInd = fullName.lastIndexOf(".");
                fileName = fullName.substring(0, extensionInd);
                extension = fullName.substring(extensionInd + 1);
                stream = input;
            }
        }

        if (stream != null) {
            clientRequest.newFile(uploadPath, fileName, extension, stream);
        }
    } catch (FileUploadException e) {
        throw new FileWriteExceptionDTO(e.getLocalizedMessage());
    }
}

From source file:com.boundlessgeo.geoserver.api.controllers.ApiController.java

protected FileItemIterator doFileUpload(final HttpServletRequest request)
        throws FileUploadException, IOException {
    final ServletFileUpload upload = newFileUpload();
    //Delegate FileItemIterator to only return files
    return new FileItemIterator() {
        FileItemIterator delegate = upload.getItemIterator(request);
        FileItemStream next = null;/*from www . j av a  2 s .co  m*/

        @Override
        public boolean hasNext() throws FileUploadException, IOException {
            if (next != null) {
                return true;
            }
            while (delegate.hasNext()) {
                FileItemStream item = delegate.next();
                if (!item.isFormField()) {
                    next = item;
                    break;
                }
            }
            return next != null;
        }

        @Override
        public FileItemStream next() throws FileUploadException, IOException {
            if (hasNext()) {
                FileItemStream current = next;
                next = null;
                return current;
            }
            throw new NoSuchElementException();
        }
    };
}

From source file:com.woonoz.proxy.servlet.HttpEntityEnclosingRequestHandler.java

private HttpEntity createMultipartEntity(HttpServletRequest request) throws FileUploadException, IOException {
    DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
    MultipartEntity multipartEntity = new MultipartEntity();
    FileItemIterator iterator = servletFileUpload.getItemIterator(request);
    while (iterator.hasNext()) {
        FileItemStream fileItem = iterator.next();
        final String partName = fileItem.getFieldName();
        if (fileItem.isFormField()) {
            multipartEntity.addPart(partName, buildStringBody(fileItem));
        } else {/*from   www . java2  s. c om*/
            multipartEntity.addPart(partName, buildContentBodyFromFileItem(fileItem));
        }
    }
    return multipartEntity;
}

From source file:com.woonoz.proxy.servlet.HttpPostRequestHandler.java

private HttpEntity createMultipartEntity(HttpServletRequest request, HttpPost httpPost)
        throws FileUploadException, IOException {
    DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
    MultipartEntity multipartEntity = new MultipartEntity();
    FileItemIterator iterator = servletFileUpload.getItemIterator(request);
    while (iterator.hasNext()) {
        FileItemStream fileItem = iterator.next();
        final String partName = fileItem.getFieldName();
        if (fileItem.isFormField()) {
            multipartEntity.addPart(partName, buildStringBody(fileItem));
        } else {//from  www  .  ja v  a 2  s . c o  m
            multipartEntity.addPart(partName, buildContentBodyFromFileItem(fileItem));
        }
    }
    return multipartEntity;
}