Class MultipartUtils


  • public final class MultipartUtils
    extends Object
    Provides utilities to handle MIME parts.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.mail.Multipart createMultipart​(InputStream content, String contentType)
      Instantiates a Multipart object extracting content from the specified stream and 'Content-Type' header.
      static javax.mail.BodyPart getBodyPart​(javax.mail.Multipart multipart, String inputName)
      Returns the part matching the specified 'name' attribute in the 'Content-Disposition' header.
      static String getContentDispositionAttribute​(javax.mail.Part part, String name)
      Returns the value of the specified attribute name in the part 'Content-Disposition' header.
      static String getFileName​(javax.mail.Part part)
      Return the 'filename' attribute referenced in the 'Content-Disposition' header of the part.
      static javax.mail.BodyPart getFilePart​(javax.mail.Multipart multipart)
      Returns the first part that contains a file content.
      static String getStringContent​(javax.mail.Part part)
      Returns the part content as a String.
      static boolean isFilePart​(javax.mail.Part part)
      Determines whether the part contains file content.
      static boolean isMultipart​(String contentType)
      Determines whether the specified HTTP Content-Type is "multipart/form-data".
    • Method Detail

      • isMultipart

        public static boolean isMultipart​(String contentType)
        Determines whether the specified HTTP Content-Type is "multipart/form-data".
        Parameters:
        contentType - a HTTP Content-Type header value (may be null).
        Returns:
        true if the Content-Type is "multipart/form-data", false otherwise.
      • getFilePart

        public static javax.mail.BodyPart getFilePart​(javax.mail.Multipart multipart)
                                               throws javax.mail.MessagingException
        Returns the first part that contains a file content.
        Parameters:
        multipart - a multipart.
        Returns:
        the first part that contains a file content.
        Throws:
        javax.mail.MessagingException - if the multipart content cannot be read.
        See Also:
        isFilePart(Part)
      • isFilePart

        public static boolean isFilePart​(javax.mail.Part part)
                                  throws javax.mail.MessagingException
        Determines whether the part contains file content.
        Parameters:
        part - the part.
        Returns:
        true if the part contains a 'Content-Disposition' header that has a 'filename' attribute, false otherwise.
        Throws:
        javax.mail.MessagingException - if the part content cannot be read.
      • getFileName

        public static String getFileName​(javax.mail.Part part)
                                  throws javax.mail.MessagingException
        Return the 'filename' attribute referenced in the 'Content-Disposition' header of the part.
        Parameters:
        part - the part.
        Returns:
        the 'filename' attribute of the part 'Content-Disposition' header, null if none.
        Throws:
        javax.mail.MessagingException - if the part content cannot be read.
        See Also:
        isFilePart(Part)
      • getBodyPart

        public static javax.mail.BodyPart getBodyPart​(javax.mail.Multipart multipart,
                                                      String inputName)
                                               throws javax.mail.MessagingException
        Returns the part matching the specified 'name' attribute in the 'Content-Disposition' header.
        Parameters:
        multipart - the multipart.
        inputName - the 'name' attribute to look for.
        Returns:
        the part matching the specified 'name' attribute in the 'Content-Disposition' header, null if none.
        Throws:
        javax.mail.MessagingException - if the multipart content cannot be read.
      • getContentDispositionAttribute

        public static String getContentDispositionAttribute​(javax.mail.Part part,
                                                            String name)
                                                     throws javax.mail.MessagingException
        Returns the value of the specified attribute name in the part 'Content-Disposition' header.
        Parameters:
        part - the part.
        name - the attribute name to look for.
        Returns:
        the value of the specified attribute name in the part 'Content-Disposition' header, null if none.
        Throws:
        javax.mail.MessagingException - if the part content cannot be read.
      • getStringContent

        public static String getStringContent​(javax.mail.Part part)
                                       throws IOException,
                                              javax.mail.MessagingException
        Returns the part content as a String. The part content is decoded using UTF-8.
        Parameters:
        part - the part.
        Returns:
        the content of the part as a String.
        Throws:
        IOException - if the part content cannot be read.
        javax.mail.MessagingException - if the part content cannot be read.
      • createMultipart

        public static javax.mail.Multipart createMultipart​(InputStream content,
                                                           String contentType)
                                                    throws IOException,
                                                           javax.mail.MessagingException
        Instantiates a Multipart object extracting content from the specified stream and 'Content-Type' header.
        Parameters:
        content - the stream from where multipart content must be read.
        contentType - the multipart 'Content-Type'.
        Returns:
        a Multipart instance.
        Throws:
        IOException - if the multipart content cannot be read.
        javax.mail.MessagingException - if the multipart content cannot be read.