javax.mail.Part#getInputStream ( )源码实例Demo

下面列出了javax.mail.Part#getInputStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: eml-to-pdf-converter   文件: MimeMessageParser.java
/**
 * Get the String Content of a MimePart.
 * @param p MimePart
 * @return Content as String
 * @throws IOException
 * @throws MessagingException
 */
private static String getStringContent(Part p) throws IOException, MessagingException {
	Object content = null;
	
	try {
		content = p.getContent();
	} catch (Exception e) {
		Logger.debug("Email body could not be read automatically (%s), we try to read it anyway.", e.toString());
		
		// most likely the specified charset could not be found
		content = p.getInputStream();
	}
	
	String stringContent = null;
	
	if (content instanceof String) {
		stringContent = (String) content;
	} else if (content instanceof InputStream) {
		stringContent = new String(ByteStreams.toByteArray((InputStream) content), "utf-8");
	}
	
	return stringContent;
}
 
源代码2 项目: james-project   文件: AttachmentFileNameIs.java
/**
 * Checks if <I>part</I> is a zip containing a file that matches with at least one of the <CODE>masks</CODE>.
 *
 *@param part
 */
protected boolean matchFoundInZip(Part part) throws MessagingException, IOException {

    try (ZipInputStream zis = new ZipInputStream(part.getInputStream())) {
        while (true) {
            ZipEntry zipEntry = zis.getNextEntry();
            if (zipEntry == null) {
                break;
            }
            String fileName = zipEntry.getName();
            if (matchFound(fileName)) {
                if (isDebug) {
                    LOGGER.debug("matched {}({})", part.getFileName(), fileName);
                }
                return true;
            }
        }
        return false;
    }
}
 
/**
 * Object can be built on existing message part only.
 * 
 * @param messagePart Message part.
 */
public SubethaEmailMessagePart(Part messagePart)
{
    ParameterCheck.mandatory("messagePart", messagePart);

    try
    {
        fileSize = messagePart.getSize();
        fileName = messagePart.getFileName();
        contentType = messagePart.getContentType();

        Matcher matcher = encodingExtractor.matcher(contentType);
        if (matcher.find())
        {
            encoding = matcher.group(1);
            if (!Charset.isSupported(encoding))
            {
                throw new EmailMessageException(ERR_UNSUPPORTED_ENCODING, encoding);
            }
        }

        try
        {
            contentInputStream = messagePart.getInputStream(); 
        }
        catch (Exception ex)
        {
            throw new EmailMessageException(ERR_FAILED_TO_READ_CONTENT_STREAM, ex.getMessage());
        }
    }
    catch (MessagingException e)
    {
        throw new EmailMessageException(ERR_INCORRECT_MESSAGE_PART, e.getMessage());
    }
}
 
源代码4 项目: baleen   文件: EmailReader.java
private void writeFileToDisk(File destFile, Part part) throws IOException, MessagingException {
  try (FileOutputStream output = new FileOutputStream(destFile);
      InputStream input = part.getInputStream()) {

    byte[] buffer = new byte[4096];
    int byteRead;

    while ((byteRead = input.read(buffer)) != -1) {
      output.write(buffer, 0, byteRead);
    }
  } catch (IOException ex) {
    throw new IOException("Unable to save attachment", ex);
  }
}
 
源代码5 项目: translationstudio8   文件: MessageParser.java
/**
 * 获取消息附件中的文件.
 * @return List&lt;ResourceFileBean&gt;
 * 				文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public List<ResourceFileBean> getFiles() throws MessagingException, IOException {
	List<ResourceFileBean> resourceList = new ArrayList<ResourceFileBean>();
	Object content = message.getContent();
	Multipart mp = null;
	if (content instanceof Multipart) {
		mp = (Multipart) content;
	} else {
		return resourceList;
	}
	for (int i = 0, n = mp.getCount(); i < n; i++) {
		Part part = mp.getBodyPart(i);
		//此方法返回 Part 对象的部署类型。
		String disposition = part.getDisposition();
		//Part.ATTACHMENT 指示 Part 对象表示附件。
		//Part.INLINE 指示 Part 对象以内联方式显示。
		if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
			//part.getFileName():返回 Part 对象的文件名。
			String fileName = MimeUtility.decodeText(part.getFileName());
			//此方法为 Part 对象返回一个 InputStream 对象
			InputStream is = part.getInputStream();
			resourceList.add(new ResourceFileBean(fileName, is));
		} else if (disposition == null) {
			//附件也可以没有部署类型的方式存在
			getRelatedPart(part, resourceList);
		}
	}
	return resourceList;
}
 
源代码6 项目: translationstudio8   文件: MessageParser.java
/**
 * 获取消息附件中的文件.
 * @param part
 *            Part 对象
 * @param resourceList
 *            文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
	//验证 Part 对象的 MIME 类型是否与指定的类型匹配。
	if (part.isMimeType("multipart/related")) {
		Multipart mulContent = (Multipart) part.getContent();
		for (int j = 0, m = mulContent.getCount(); j < m; j++) {
			Part contentPart = mulContent.getBodyPart(j);
			if (!contentPart.isMimeType("text/*")) {
				String fileName = "Resource";
				//此方法返回 Part 对象的内容类型。
				String type = contentPart.getContentType();
				if (type != null) {
					type = type.substring(0, type.indexOf("/"));
				}
				fileName = fileName + "[" + type + "]";
				if (contentPart.getHeader("Content-Location") != null
						&& contentPart.getHeader("Content-Location").length > 0) {
					fileName = contentPart.getHeader("Content-Location")[0];
				}
				InputStream is = contentPart.getInputStream();
				resourceList.add(new ResourceFileBean(fileName, is));
			}
		}
	} else {
		Multipart mp = null;
		Object content = part.getContent();
		if (content instanceof Multipart) {
			mp = (Multipart) content;
		} else {
			return;
		}
		for (int i = 0, n = mp.getCount(); i < n; i++) {
			Part body = mp.getBodyPart(i);
			getRelatedPart(body, resourceList);
		}
	}
}
 
源代码7 项目: tmxeditor8   文件: MessageParser.java
/**
 * 获取消息附件中的文件.
 * @return List&lt;ResourceFileBean&gt;
 * 				文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public List<ResourceFileBean> getFiles() throws MessagingException, IOException {
	List<ResourceFileBean> resourceList = new ArrayList<ResourceFileBean>();
	Object content = message.getContent();
	Multipart mp = null;
	if (content instanceof Multipart) {
		mp = (Multipart) content;
	} else {
		return resourceList;
	}
	for (int i = 0, n = mp.getCount(); i < n; i++) {
		Part part = mp.getBodyPart(i);
		//此方法返回 Part 对象的部署类型。
		String disposition = part.getDisposition();
		//Part.ATTACHMENT 指示 Part 对象表示附件。
		//Part.INLINE 指示 Part 对象以内联方式显示。
		if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
			//part.getFileName():返回 Part 对象的文件名。
			String fileName = MimeUtility.decodeText(part.getFileName());
			//此方法为 Part 对象返回一个 InputStream 对象
			InputStream is = part.getInputStream();
			resourceList.add(new ResourceFileBean(fileName, is));
		} else if (disposition == null) {
			//附件也可以没有部署类型的方式存在
			getRelatedPart(part, resourceList);
		}
	}
	return resourceList;
}
 
源代码8 项目: tmxeditor8   文件: MessageParser.java
/**
 * 获取消息附件中的文件.
 * @param part
 *            Part 对象
 * @param resourceList
 *            文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
	//验证 Part 对象的 MIME 类型是否与指定的类型匹配。
	if (part.isMimeType("multipart/related")) {
		Multipart mulContent = (Multipart) part.getContent();
		for (int j = 0, m = mulContent.getCount(); j < m; j++) {
			Part contentPart = mulContent.getBodyPart(j);
			if (!contentPart.isMimeType("text/*")) {
				String fileName = "Resource";
				//此方法返回 Part 对象的内容类型。
				String type = contentPart.getContentType();
				if (type != null) {
					type = type.substring(0, type.indexOf("/"));
				}
				fileName = fileName + "[" + type + "]";
				if (contentPart.getHeader("Content-Location") != null
						&& contentPart.getHeader("Content-Location").length > 0) {
					fileName = contentPart.getHeader("Content-Location")[0];
				}
				InputStream is = contentPart.getInputStream();
				resourceList.add(new ResourceFileBean(fileName, is));
			}
		}
	} else {
		Multipart mp = null;
		Object content = part.getContent();
		if (content instanceof Multipart) {
			mp = (Multipart) content;
		} else {
			return;
		}
		for (int i = 0, n = mp.getCount(); i < n; i++) {
			Part body = mp.getBodyPart(i);
			getRelatedPart(body, resourceList);
		}
	}
}
 
源代码9 项目: ogham   文件: EmailUtils.java
/**
 * Get the content as byte array of a particular part.
 * 
 * <p>
 * If the part is null or the content stream is null, then
 * null is returned.
 * 
 * @param part
 *            the part
 * @return the content as byte array or null
 * @throws IOException
 *             when part can't be read
 * @throws MessagingException
 *             when message can't be read
 */
@SuppressWarnings("squid:S1168")	// return null on purpose (to be able to distinguish empty content from no content at all in tests)
public static byte[] getContent(Part part) throws IOException, MessagingException {
	if (part == null) {
		return null;
	}
	InputStream stream = part.getInputStream();
	if (stream == null) {
		return null;
	}
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	IOUtils.copy(stream, baos);
	return baos.toByteArray();
}