Package org.apache.commons.fileupload

Examples of org.apache.commons.fileupload.FileItemStream


            FileItemIterator iter = upload.getItemIterator(requestContextWrapper);
            List<UploadContext> uploadContexts = new ArrayList<UploadContext>(7);
            List<NamedString> formParameters = new ArrayList<NamedString>(7);
            while (iter.hasNext())
            {
               FileItemStream item = iter.next();
               InputStream stream = item.openStream();
               if (!item.isFormField())
               {
                  String contentType = item.getContentType();
                  if (log.isDebugEnabled())
                  {
                     log.debug("File field " + item.getFieldName() + " with file name " + item.getName()
                        + " and content type " + contentType + " detected.");
                  }

                  BufferedInputStream bufIn = new BufferedInputStream(stream);
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  BufferedOutputStream bos = new BufferedOutputStream(baos);

                  int c = bufIn.read();
                  while (c != -1)
                  {
                     bos.write(c);
                     c = bufIn.read();
                  }

                  bos.flush();
                  baos.flush();
                  bufIn.close();
                  bos.close();

                  final byte[] uploadData = baos.toByteArray();
                  if (uploadData.length != 0)
                  {
                     UploadContext uploadContext = WSRPTypeFactory.createUploadContext(contentType, uploadData);

                     List<NamedString> mimeAttributes = new ArrayList<NamedString>(2);

                     String value = FileUpload.FORM_DATA + ";" + " name=\"" + item.getFieldName() + "\";" + " filename=\"" + item.getName() + "\"";
                     NamedString mimeAttribute = WSRPTypeFactory.createNamedString(FileUpload.CONTENT_DISPOSITION, value);
                     mimeAttributes.add(mimeAttribute);

                     mimeAttribute = WSRPTypeFactory.createNamedString(FileUpload.CONTENT_TYPE, item.getContentType());
                     mimeAttributes.add(mimeAttribute);

                     uploadContext.getMimeAttributes().addAll(mimeAttributes);

                     uploadContexts.add(uploadContext);
                  }
                  else
                  {
                     log.debug("Ignoring empty file " + item.getName());
                  }
               }
               else
               {
                  NamedString formParameter = WSRPTypeFactory.createNamedString(item.getFieldName(), Streams.asString(stream));
                  formParameters.add(formParameter);
               }
            }

            result.getUploadContexts().addAll(uploadContexts);
View Full Code Here


      // multipart request
      try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
          FileItemStream item = iter.next();
          String name = item.getFieldName();
          InputStream stream = item.openStream();
          if (item.isFormField()) {
            requestParams.put(name, Streams.asString(stream));
          } else {
            String fileName = item.getName();
            if (fileName != null && !"".equals(fileName.trim())) {
              listFiles.add(item);

              ByteArrayOutputStream os = new ByteArrayOutputStream();
              IOUtils.copy(stream, os);
View Full Code Here

      upload.setSizeMax(MAX_FILE_SIZE);

      try {
        FileItemIterator iterator = upload.getItemIterator(request);
        while (iterator.hasNext()) {
          FileItemStream item = iterator.next();
          InputStream in = item.openStream();
          if ((!item.isFormField()) && (item.getFieldName().equals("file1"))) {
           
            String fileName = item.getName();
            if (fileName.length()>0){
              try {
                LineIterator it =  IOUtils.lineIterator(in, "UTF-8");
                while (it.hasNext()) {
                  String line = it.nextLine();
View Full Code Here

      upload.setSizeMax(maxUploadSize);
      upload.setProgressListener(listener);
     
      FileItemIterator iter = upload.getItemIterator(request);
      while (iter.hasNext()) {
          FileItemStream item = iter.next();
          String name = item.getFieldName();
          InputStream stream = item.openStream();
          if (item.isFormField()) {
            String value = Streams.asString(stream);

            logger.info("Upload "+name+":"+value);
           
            if( "progressId".equals(name) ) {
              progressId = value;
              listener.setProgressId(value);
            } else {
              // Add parameter to map
              List<String> paramList = parameterMap.get(name);
              if( null == paramList ) {
                paramList = new Vector<String>();
                parameterMap.put(name,paramList);
              }
              paramList.add(value);
            }
           
          } else {
           
            LoadedFileImpl loadedFile = new LoadedFileImpl();
            loadedFile.setParameterName(name);

            // Get original filename
            String fileName = item.getName();
              if (fileName != null) {
                fileName = fileName.trim();
                if( 0 == fileName.length() ) {
                  fileName = null;
                } else {
View Full Code Here

        FileItemIterator iter = upload.getItemIterator(context);
          //byte[] value;
          InputStream is;
          ArrayList<URLItem> list=new ArrayList<URLItem>();
      while (iter.hasNext()) {
          FileItemStream item = iter.next();

          is=IOUtil.toBufferedInputStream(item.openStream());
          if (item.getContentType()==null || StringUtil.isEmpty(item.getName())) {
            list.add(new URLItem(item.getFieldName(),new String(IOUtil.toBytes(is),encoding),false));      
          }
          else {
            tempFile=tempDir.getRealResource(getFileName());
            fileItems.put(item.getFieldName().toLowerCase(),
                new Item(tempFile,item.getContentType(),item.getName(),item.getFieldName()));
          String value=tempFile.toString();
            IOUtil.copy(is, tempFile,true);
           
            list.add(new URLItem(item.getFieldName(),value,false));      
          }      
      }
     
      raw= list.toArray(new URLItem[list.size()]);
      fillDecoded(raw,encoding,scriptProteced,pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
View Full Code Here

                Map<String, String[]> params = new HashMap<String, String[]>();
                InputStream fileStream = null;
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) {
                    FileItemStream fileItemStream = iter.next();
                    if (fileItemStream.isFormField()) {
                        String fieldName = fileItemStream.getFieldName();
                        if ("content".equals(fieldName)) {
                            utf8 = true;
                            String[] parser = params.get("parser");
                            if (parser != null && parser[0].startsWith("xml")) {
                                contentType = "application/xml";
                            } else {
                                contentType = "text/html";
                            }
                            request.setAttribute("nu.validator.servlet.MultipartFormDataFilter.type", "textarea");
                            fileStream = fileItemStream.openStream();
                            break;
                        } else {
                            putParam(
                                    params,
                                    fieldName,
                                    utf8ByteStreamToString(fileItemStream.openStream()));
                        }
                    } else {
                        String fileName = fileItemStream.getName();
                        if (fileName != null) {
                            putParam(params, fileItemStream.getFieldName(),
                                    fileName);
                            Matcher m = EXTENSION.matcher(fileName);
                            if (m.matches()) {
                                contentType = EXTENSION_TO_TYPE.get(m.group(1));
                            }
                        }
                        if (contentType == null) {
                            contentType = fileItemStream.getContentType();
                        }
                        request.setAttribute("nu.validator.servlet.MultipartFormDataFilter.type", "file");
                        fileStream = fileItemStream.openStream();
                        break;
                    }
                }
                if (fileStream == null) {
                    fileStream = new ByteArrayInputStream(new byte[0]);
View Full Code Here

    ServletFileUpload upload = new ServletFileUpload();
    upload.setSizeMax(50000);
    try {
      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        InputStream in = item.openStream();
        if (item.isFormField()) {
          String fieldName = item.getFieldName();
          if (fieldName.equals("phoneNumber")) {
            phoneNumber = Streams.asString(in, "UTF-8");
          } else if (fieldName.equals("defaultCountry")) {
            defaultCountry = Streams.asString(in).toUpperCase();
          } else if (fieldName.equals("languageCode")) {
View Full Code Here

    try {

      // Parse the request
      FileItemIterator iter = upload.getItemIterator(req);
      while (iter.hasNext()) {
        FileItemStream item = iter.next();
        String name = item.getFieldName();
        try (InputStream stream = item.openStream()) {
          if (item.isFormField()) {
            // TODO What to do with this?
            log.info("Form field {} with value {} detected.", name,
                Streams.asString(stream));
          } else {

            // TODO Must we support multiple files uploading?
            log.info("File field {} with file name detected.",
                name, item.getName());

            log.info("Start to receive bytes (estimated bytes)",
                Integer.toString(req.getContentLength()));
            int bytes = IOUtils.copy(stream, repoItemOutputStrem);
            resp.setStatus(SC_OK);
View Full Code Here

        } else if (format.equals("stream")) {
            try {
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iter = upload.getItemIterator(requestGetter.getRequest());
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    return item.openStream();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
View Full Code Here

                checkMaxSize = true;
            }
           
            while (iter.hasNext())
            {
                final FileItemStream item = iter.next();
                FileItem fileItem = fac.createItem(item.getFieldName(), item
                        .getContentType(), item.isFormField(), item.getName());

                long allowedLimit = 0L;
                try
                {
                    if (maxFileSize != -1L || checkMaxSize)
                    {
                        if (checkMaxSize)
                        {
                            allowedLimit = maxSize > maxFileSize ? maxFileSize : maxSize;
                        }
                        else
                        {
                            //Just put the limit
                            allowedLimit = maxFileSize;
                        }
                       
                        long contentLength = getContentLength(item.getHeaders());

                        //If we have a content length in the header we can use it
                        if (contentLength != -1L && contentLength > allowedLimit)
                        {
                            throw new FileUploadIOException(
                                    new FileSizeLimitExceededException(
                                            "The field "
                                                    + item.getFieldName()
                                                    + " exceeds its maximum permitted "
                                                    + " size of " + allowedLimit
                                                    + " characters.",
                                            contentLength, allowedLimit));
                        }

                        //Otherwise we must limit the input as it arrives (NOTE: we cannot rely
                        //on commons upload to throw this exception as it will close the
                        //underlying stream
                        final InputStream itemInputStream = item.openStream();
                       
                        InputStream limitedInputStream = new LimitedInputStream(
                                itemInputStream, allowedLimit)
                        {
                            protected void raiseError(long pSizeMax, long pCount)
                                    throws IOException
                            {
                                throw new FileUploadIOException(
                                        new FileSizeLimitExceededException(
                                                "The field "
                                                        + item.getFieldName()
                                                        + " exceeds its maximum permitted "
                                                        + " size of "
                                                        + pSizeMax
                                                        + " characters.",
                                                pCount, pSizeMax));
                            }
                        };

                        //Copy from the limited stream
                        long bytesCopied = Streams.copy(limitedInputStream, fileItem
                                .getOutputStream(), true);
                       
                        // Decrement the bytesCopied values from maxSize, so the next file copied
                        // takes into account this value when allowedLimit var is calculated
                        // Note the invariant before the line is maxSize >= bytesCopied, since if this
                        // is not true a FileUploadIOException is thrown first.
                        maxSize -= bytesCopied;
                    }
                    else
                    {
                        //We can just copy the data
                        Streams.copy(item.openStream(), fileItem
                                .getOutputStream(), true);
                    }
                }
                catch (FileUploadIOException e)
                {
                    try
                    {
                        throw (FileUploadException) e.getCause();
                    }
                    catch (FileUploadBase.FileSizeLimitExceededException se)
                    {
                        request
                                .setAttribute(
                                        "org.apache.myfaces.custom.fileupload.exception",
                                        "fileSizeLimitExceeded");
                        String fieldName = fileItem.getFieldName();
                        request.setAttribute(
                                "org.apache.myfaces.custom.fileupload."+fieldName+".maxSize",
                                new Integer((int)allowedLimit));
                    }
                }
                catch (IOException e)
                {
                    throw new IOFileUploadException("Processing of "
                            + FileUploadBase.MULTIPART_FORM_DATA
                            + " request failed. " + e.getMessage(), e);
                }
                if (fileItem instanceof FileItemHeadersSupport)
                {
                    final FileItemHeaders fih = item.getHeaders();
                    ((FileItemHeadersSupport) fileItem).setHeaders(fih);
                }
                if (fileItem != null)
                {
                    items.add(fileItem);
View Full Code Here

TOP

Related Classes of org.apache.commons.fileupload.FileItemStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.