コード例 #1
0
 /**
  * Returns the filename associated with this body part.
  *
  * <p>This method returns the value of the "filename" parameter from the Content-Disposition
  * header field. If the latter is not available, it returns the value of the "name" parameter from
  * the Content-Type header field.
  */
 public String getFileName() throws MessagingException {
   String filename = null;
   String header = getHeader(CONTENT_DISPOSITION_NAME, null);
   if (header != null) {
     ContentDisposition cd = new ContentDisposition(header);
     filename = cd.getParameter("filename");
   }
   if (filename == null) {
     header = getHeader(CONTENT_TYPE_NAME, null);
     if (header != null) {
       try {
         ContentType contentType = new ContentType(header);
         filename = contentType.getParameter("name");
       } catch (ParseException e) {
       }
     }
   }
   PrivilegedAction a = new GetSystemPropertyAction("mail.mime.decodefilename");
   if ("true".equals(AccessController.doPrivileged(a))) {
     try {
       filename = MimeUtility.decodeText(filename);
     } catch (UnsupportedEncodingException e) {
       throw new MessagingException(e.getMessage(), e);
     }
   }
   return filename;
 }
コード例 #2
0
  /**
   * Updates the headers of this part, based on the content.
   *
   * @exception IllegalWriteException if the underlying implementation does not support modification
   * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder
   */
  protected void updateHeaders() throws MessagingException {
    if (getDataHandler() != null) {
      try {
        String contentType = dh.getContentType();
        ContentType ct = new ContentType(contentType);
        if (ct.match("multipart/*")) {
          MimeMultipart mmp = (MimeMultipart) dh.getContent();
          mmp.updateHeaders();
        } else if (ct.match("message/rfc822")) {
        } else {
          // Update Content-Transfer-Encoding
          if (getHeader(CONTENT_TRANSFER_ENCODING_NAME) == null) {
            setHeader(CONTENT_TRANSFER_ENCODING_NAME, MimeUtility.getEncoding(dh));
          }
        }

        // Update Content-Type if nonexistent,
        // and Content-Type "name" with Content-Disposition "filename"
        // parameter(see setFilename())
        if (getHeader(CONTENT_TYPE_NAME) == null) {
          String disposition = getHeader(CONTENT_DISPOSITION_NAME, null);
          if (disposition != null) {
            ContentDisposition cd = new ContentDisposition(disposition);
            String filename = cd.getParameter("filename");
            if (filename != null) {
              ct.setParameter("name", filename);
              contentType = ct.toString();
            }
          }
          setHeader(CONTENT_TYPE_NAME, contentType);
        }
      } catch (IOException e) {
        throw new MessagingException("I/O error", e);
      }
    }
  }