Package org.apache.commons.fileupload

Examples of org.apache.commons.fileupload.MultipartStream


            throw new IOException("the request was rejected because no multipart boundary was found");
        }
        byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();

        ByteArrayInputStream input = new ByteArrayInputStream(body);
        MultipartStream multi = new MultipartStream(input, boundary);

        boolean nextPart = multi.skipPreamble();
        while (nextPart) {
            try {
                output = new ByteArrayOutputStream();
                multi.readBodyData(output);
                outputArray = output.toByteArray();
                multiPartBuffer = new StringBuffer(50);
                isFile = false;
                File jarFileInTempDir;
                j = 0;

                for (int i = 0; i < outputArray.length; i++) {
                    //first check for \r\n end of line
                    if (outputArray[i] == 13 && outputArray[i + 1] == 10) {
                        //we've come to the end of a line
                        headerMap = parseMultiPartHeader(multiPartBuffer);
                        if (headerMap.get(NAME) != null) {
                            fileName = (String) headerMap.get(NAME);
                        }

                        //add the filename if there is one
                        if (fileName != null && headerMap.get(FILENAME) != null) {
                            this.formParams.put(fileName, headerMap.get(FILENAME));
                            isFile = true;
                        }

                        if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) {
                            //we've reached the blank line
                            i+=4;
                            j = i;
                            break;
                        } else {
                            i++;
                        }

                        multiPartBuffer = new StringBuffer(50);
                    } else {
                        multiPartBuffer.append((char) outputArray[i]);
                    }
                }

                //here we know that we have a file and that we need to write it
                if (isFile) {
                    //create file
                    jarFileInTempDir = new File((String) this.formParams.get(fileName));
                    if (!jarFileInTempDir.exists()) {
                        jarFileInTempDir.createNewFile();
                    }

                    //write the byte array to the file
                    fos = new FileOutputStream(jarFileInTempDir);
                    fos.write(outputArray, j, outputArray.length-j);
                    fos.close();
                } else { //form data, not a file
                    multiPartBuffer = new StringBuffer(outputArray.length-j);
                    for (int i = j; i < outputArray.length; i++) {
                        multiPartBuffer.append((char)outputArray[i]);
                    }

                    this.formParams.put(
                        fileName,
                        multiPartBuffer.toString());
                }

                nextPart = multi.readBoundary();
            } catch (MultipartStream.MalformedStreamException mse) {
                throw new IOException(mse.getMessage());
            }
        }
    }
View Full Code Here


        {
            byte[] boundary = contentType.substring(
                                contentType.indexOf("boundary=")+9).getBytes();
            InputStream input = (InputStream)req.getInputStream();

            MultipartStream multi = new MultipartStream(input, boundary);
            multi.setHeaderEncoding(encoding);
            boolean nextPart = multi.skipPreamble();
            while(nextPart)
            {
                Map headers = parseHeaders(multi.readHeaders());
                String fieldName = getFieldName(headers);
                if (fieldName != null)
                {
                    String subContentType = getHeader(headers, CONTENT_TYPE);
                    if (subContentType != null && subContentType
                                                .startsWith(MULTIPART_MIXED))
                    {
                        // Multiple files.
                        byte[] subBoundary =
                            subContentType.substring(
                                subContentType
                                .indexOf("boundary=")+9).getBytes();
                        multi.setBoundary(subBoundary);
                        boolean nextSubPart = multi.skipPreamble();
                        while (nextSubPart)
                        {
                            headers = parseHeaders(multi.readHeaders());
                            if (getFileName(headers) != null)
                            {
                                FileItem item = createItem(path, headers,
                                                           requestSize);
                                OutputStream os = item.getOutputStream();
                                try
                                {
                                    multi.readBodyData(os);
                                }
                                finally
                                {
                                    os.close();
                                }
                                params.append(getFieldName(headers), item);
                            }
                            else
                            {
                                // Ignore anything but files inside
                                // multipart/mixed.
                                multi.discardBodyData();
                            }
                            nextSubPart = multi.readBoundary();
                        }
                        multi.setBoundary(boundary);
                    }
                    else
                    {
                        if (getFileName(headers) != null)
                        {
                            // A single file.
                            FileItem item = createItem(path, headers,
                                                       requestSize);
                            OutputStream os = item.getOutputStream();
                            try
                            {
                                multi.readBodyData(os);
                            }
                            finally
                            {
                                os.close();
                            }
                            params.append(getFieldName(headers), item);
                        }
                        else
                        {
                            // A form field.
                            FileItem item = createItem(path, headers,
                                                       requestSize);
                            OutputStream os = item.getOutputStream();
                            try
                            {
                                multi.readBodyData(os);
                            }
                            finally
                            {
                                os.close();
                            }
                            params.append(getFieldName(headers),
                                          new String(item.get()));
                        }
                    }
                }
                else
                {
                    // Skip this part.
                    multi.discardBodyData();
                }
                nextPart = multi.readBoundary();
            }
        }
        catch(IOException e)
        {
            throw new TurbineException("Processing of " + MULTIPART_FORM_DATA
View Full Code Here

            throw new IOException("the request was rejected because no multipart boundary was found");
        }
        byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();

        ByteArrayInputStream input = new ByteArrayInputStream(body);
        MultipartStream multi = new MultipartStream(input, boundary);

        boolean nextPart = multi.skipPreamble();
        while (nextPart) {
            try {
                output = new ByteArrayOutputStream();
                multi.readBodyData(output);
                outputArray = output.toByteArray();
                multiPartBuffer = new StringBuffer(50);
                isFile = false;
                File jarFileInTempDir;
                j = 0;

                for (int i = 0; i < outputArray.length; i++) {
                    //first check for \r\n end of line
                    if (outputArray[i] == 13 && outputArray[i + 1] == 10) {
                        //we've come to the end of a line
                        headerMap = parseMultiPartHeader(multiPartBuffer);
                        if (headerMap.get(NAME) != null) {
                            fileName = (String) headerMap.get(NAME);
                        }

                        //add the filename if there is one
                        if (fileName != null && headerMap.get(FILENAME) != null) {
                            this.formParams.put(fileName, headerMap.get(FILENAME));
                            isFile = true;
                        }

                        if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) {
                            //we've reached the blank line
                            i+=4;
                            j = i;
                            break;
                        } else {
                            i++;
                        }

                        multiPartBuffer = new StringBuffer(50);
                    } else {
                        multiPartBuffer.append((char) outputArray[i]);
                    }
                }

                //here we know that we have a file and that we need to write it
                if (isFile) {
                    //create file
                    jarFileInTempDir = new File((String) this.formParams.get(fileName));
                    if (!jarFileInTempDir.exists()) {
                        jarFileInTempDir.createNewFile();
                    }

                    //write the byte array to the file
                    fos = new FileOutputStream(jarFileInTempDir);
                    fos.write(outputArray, j, outputArray.length-j);
                    fos.close();
                } else { //form data, not a file
                    multiPartBuffer = new StringBuffer(outputArray.length-j);
                    for (int i = j; i < outputArray.length; i++) {
                        multiPartBuffer.append((char)outputArray[i]);
                    }

                    this.formParams.put(
                        fileName,
                        multiPartBuffer.toString());
                }

                nextPart = multi.readBoundary();
            } catch (MultipartStream.MalformedStreamException mse) {
                throw new IOException(mse.getMessage());
            }
        }
    }
View Full Code Here

            throw new IOException("the request was rejected because no multipart boundary was found");
        }
        byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();

        ByteArrayInputStream input = new ByteArrayInputStream(body);
        MultipartStream multi = new MultipartStream(input, boundary);

        boolean nextPart = multi.skipPreamble();
        while (nextPart) {
            try {
                output = new ByteArrayOutputStream();
                multi.readBodyData(output);
                outputArray = output.toByteArray();
                multiPartBuffer = new StringBuffer(50);
                isFile = false;
                File jarFileInTempDir;
                j = 0;

                for (int i = 0; i < outputArray.length; i++) {
                    //first check for \r\n end of line
                    if (outputArray[i] == 13 && outputArray[i + 1] == 10) {
                        //we've come to the end of a line
                        headerMap = parseMultiPartHeader(multiPartBuffer);
                        if (headerMap.get(NAME) != null) {
                            fileName = (String) headerMap.get(NAME);
                        }

                        //add the filename if there is one
                        if (fileName != null && headerMap.get(FILENAME) != null) {
                            this.formParams.put(fileName, headerMap.get(FILENAME));
                            isFile = true;
                        }

                        if (outputArray[i + 2] == 13 && outputArray[i + 3] == 10) {
                            //we've reached the blank line
                            i+=4;
                            j = i;
                            break;
                        } else {
                            i++;
                        }

                        multiPartBuffer = new StringBuffer(50);
                    } else {
                        multiPartBuffer.append((char) outputArray[i]);
                    }
                }

                //here we know that we have a file and that we need to write it
                if (isFile) {
                    //create file
                    jarFileInTempDir = new File((String) this.formParams.get(fileName));
                    if (!jarFileInTempDir.exists()) {
                        jarFileInTempDir.createNewFile();
                    }

                    //write the byte array to the file
                    fos = new FileOutputStream(jarFileInTempDir);
                    fos.write(outputArray, j, outputArray.length-j);
                    fos.close();
                } else { //form data, not a file
                    multiPartBuffer = new StringBuffer(outputArray.length-j);
                    for (int i = j; i < outputArray.length; i++) {
                        multiPartBuffer.append((char)outputArray[i]);
                    }

                    this.formParams.put(
                        fileName,
                        multiPartBuffer.toString());
                }

                nextPart = multi.readBoundary();
            } catch (MultipartStream.MalformedStreamException mse) {
                throw new IOException(mse.getMessage());
            }
        }
    }
View Full Code Here

  }

  public static Array getParts(byte[] barr,String contentTypeHeader) throws IOException, PageException {
    String boundary = extractBoundary(contentTypeHeader,"");
    ByteArrayInputStream bis = new ByteArrayInputStream(barr);
    MultipartStream stream;
    Array result = new ArrayImpl();
    stream = new MultipartStream(bis,getBytes(boundary,"UTF-8"));//
   
    boolean hasNextPart = stream.skipPreamble();
    while (hasNextPart) {
      result.append(getPartData(stream));
      hasNextPart = stream.readBoundary();
    }
    return result;
  }
View Full Code Here

              /*
               * Note this API has been marked as deprecated though
               */
              logger.debug("Parsing...");
              MultipartStream multipartStream =  new MultipartStream(input, boundary);
             
              ArrayList<RetrieveMMSMessageResponse.Attachment> attachments=new ArrayList<RetrieveMMSMessageResponse.Attachment>();
             
              boolean nextPart = multipartStream.skipPreamble();
              while(nextPart) {
                String headerSection = multipartStream.readHeaders();
               
                String partContentType=null;
                String partContentName=null;
               
                if (headerSection!=null) {
                  String[] headers=headerSection.split("\n");
                  for (String header:headers) {
                    header=header.trim();
                    if (header.startsWith("Content-Type:")) {
                      String value=header.substring(header.indexOf(":")+1).trim();
                      if (value.indexOf(";")!= -1) {
                        value=value.substring(0, value.indexOf(";"));
                      }
                      partContentType=value;
                    }
                    if (header.startsWith("Content-Disposition:")) {
                      String value=header.substring(header.indexOf(":")+1).trim();
                      if (value.indexOf(";")!= -1) {
                        value=value.substring(0, value.indexOf(";"));
                      }
                      logger.debug("got content disposition="+value);    
                     
                      if (header.indexOf("name=")!=-1) {
                        String name=header.substring(header.indexOf("name=")+5).replaceAll("\\\"", "");
                        logger.debug("Parsed name="+name);
                        partContentName=name;
                      }
                    }
                  }
                }
               
//                System.out.println("Headers: [" + headers+"]");
                ByteArrayOutputStream data = new ByteArrayOutputStream();
                multipartStream.readBodyData(data);
               
                if (partContentType!=null && partContentType.equals("application/json") && partContentName!=null && partContentName.equals("root-fields")) {
                  logger.debug("Have the JSON response part:"+(new String(data.toByteArray())));
                 
                  ObjectMapper mapper=new ObjectMapper();
                  ByteArrayInputStream bais = new ByteArrayInputStream(data.toByteArray());
                  InboundMessageWrapper inboundMessageWrapper=mapper.readValue(bais, InboundMessageWrapper.class);
                  if (inboundMessageWrapper!=null && inboundMessageWrapper.getInboundMessage()!=null) response.setInboundMessage(inboundMessageWrapper.getInboundMessage());                 
                } else {
                  RetrieveMMSMessageResponse.Attachment attachment=new RetrieveMMSMessageResponse.Attachment();
                  attachment.setAttachmentContentType(partContentType);
                  attachment.setAttachmentName(partContentName);
                  attachment.setAttachmentData(data.toByteArray());
                  attachments.add(attachment);
                }

                nextPart = multipartStream.readBoundary();
              }
             
              response.setAttachments(attachments);
             
//              logger.debug("starting conversion to mime message");
View Full Code Here

TOP

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

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.