本文整理汇总了Java中javax.mail.Part.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java Part.getInputStream方法的具体用法?Java Part.getInputStream怎么用?Java Part.getInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Part
的用法示例。
在下文中一共展示了Part.getInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveEmailAttachment
import javax.mail.Part; //导入方法依赖的package包/类
public static void saveEmailAttachment(final File saveFile, final Part part) throws FileNotFoundException, IOException,
MessagingException {
InputStream inputStream = null;
try {
if (part != null) {
inputStream = part.getInputStream();
if (inputStream != null) {
FileUtils.writeFileViaStream(saveFile, inputStream);
}
}
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (final IOException e) {
LOGGER.error("Error while closing the input stream.", e);
}
}
}
示例2: getStringContent
import javax.mail.Part; //导入方法依赖的package包/类
/**
* 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;
}
示例3: getHumanReadableSize
import javax.mail.Part; //导入方法依赖的package包/类
/** Returns the size of an email attachment in a user-friendly format
* @throws MessagingException
* @throws IOException */
public static String getHumanReadableSize(Part part) throws IOException, MessagingException {
// find size in bytes
InputStream inputStream = part.getInputStream();
byte[] buffer = new byte[32*1024];
long totalBytes = 0;
int bytesRead;
do {
bytesRead = inputStream.read(buffer, 0, buffer.length);
if (bytesRead > 0)
totalBytes += bytesRead;
} while (bytesRead > 0);
// format to a string
return getHumanReadableSize(totalBytes);
}
示例4: getInputStream
import javax.mail.Part; //导入方法依赖的package包/类
private static InputStream getInputStream(
Part bodyPart)
throws MessagingException
{
try
{
if (bodyPart.isMimeType("multipart/signed"))
{
throw new MessagingException("attempt to create signed data object from multipart content - use MimeMultipart constructor.");
}
return bodyPart.getInputStream();
}
catch (IOException e)
{
throw new MessagingException("can't extract input stream: " + e);
}
}
示例5: getInputStream
import javax.mail.Part; //导入方法依赖的package包/类
private static InputStream getInputStream(
Part bodyPart,
int bufferSize)
throws MessagingException
{
try
{
InputStream in = bodyPart.getInputStream();
if (bufferSize == 0)
{
return new BufferedInputStream(in);
}
else
{
return new BufferedInputStream(in, bufferSize);
}
}
catch (IOException e)
{
throw new MessagingException("can't extract input stream: " + e);
}
}
示例6: SubethaEmailMessagePart
import javax.mail.Part; //导入方法依赖的package包/类
/**
* 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());
}
}
示例7: handleMimeMessages
import javax.mail.Part; //导入方法依赖的package包/类
protected String handleMimeMessages(Message message,Map<String,byte[]> attachmentMap)
throws Exception{
String emailBody = null;
Multipart multipart = (Multipart) message.getContent();
for (int j = 0, m = multipart.getCount(); j < m; j++) {
Part part = multipart.getBodyPart(j);
String disposition = part.getDisposition();
boolean isAttachment = false;
if (disposition != null && (disposition.equals(Part.ATTACHMENT)
|| (disposition.equals(Part.INLINE)))) {
isAttachment = true;
InputStream input = part.getInputStream();
String fileName = part.getFileName();
String contentType = part.getContentType();
ByteArrayOutputStream output = new ByteArrayOutputStream();
if (saveAttachment(input, output)) {
byte[] encodedByte = new Base64().encode(output.toByteArray());
if (encodedByte != null && fileName != null && contentType != null) {
attachmentMap.put(fileName+"~"+contentType, encodedByte);
}
}
}
if (!isAttachment && part.getContentType().startsWith("text/")){
emailBody = (String) part.getContent();
}
}
return emailBody;
}
示例8: getAttachments
import javax.mail.Part; //导入方法依赖的package包/类
/**
* Extracts the attachments from the mail.
*
* @param message
* The message.
* @return Collection of attachments as {@link AttachmentTO}.
* @throws IOException
* Exception.
* @throws MessagingException
* Exception.
*/
public static Collection<AttachmentTO> getAttachments(Message message)
throws MessagingException, IOException {
Collection<AttachmentTO> attachments = new ArrayList<AttachmentTO>();
Collection<Part> parts = getAllParts(message);
for (Part part : parts) {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (StringUtils.containsIgnoreCase(disposition, "inline")
|| StringUtils.containsIgnoreCase(disposition, "attachment")
|| StringUtils.containsIgnoreCase(contentType, "name=")) {
String fileName = part.getFileName();
Matcher matcher = FILENAME_PATTERN.matcher(part.getContentType());
if (matcher.matches()) {
fileName = matcher.group(1);
fileName = StringUtils.substringBeforeLast(fileName, ";");
}
if (StringUtils.isNotBlank(fileName)) {
fileName = fileName.replace("\"", "").replace("\\\"", "");
fileName = MimeUtility.decodeText(fileName);
if (fileName.endsWith("?=")) {
fileName = fileName.substring(0, fileName.length() - 2);
}
fileName = fileName.replace("?", "_");
AttachmentTO attachmentTO = new AttachmentStreamTO(part.getInputStream());
attachmentTO.setContentLength(part.getSize());
attachmentTO.setMetadata(new ContentMetadata());
attachmentTO.getMetadata().setFilename(fileName);
if (StringUtils.isNotBlank(contentType)) {
contentType = contentType.split(";")[0].toLowerCase();
}
attachmentTO.setStatus(AttachmentStatus.UPLOADED);
attachments.add(attachmentTO);
}
}
}
return attachments;
}
示例9: saveEmailAttachment
import javax.mail.Part; //导入方法依赖的package包/类
private int saveEmailAttachment(File saveFile, Part part) throws IOException, MessagingException {
BufferedOutputStream bos = null;
InputStream inputStream = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
byte[] buff = new byte[BUFFER_SIZE_2048];
inputStream = part.getInputStream();
ret = inputStream.read(buff);
while (ret > 0) {
bos.write(buff, 0, ret);
count += ret;
ret = inputStream.read(buff);
}
} finally {
try {
if (bos != null) {
bos.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ioe) {
LOGGER.error("Error while closing the stream.", ioe);
}
}
return count;
}
示例10: readTextPart
import javax.mail.Part; //导入方法依赖的package包/类
private String readTextPart(Part p)
throws IOException, MessagingException
{
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
{
is = new BufferedInputStream(is);
}
return IO.readText(is, true);
}
示例11: getFiles
import javax.mail.Part; //导入方法依赖的package包/类
/**
* 获取消息附件中的文件.
* @return List<ResourceFileBean>
* 文件的集合(每个 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;
}
示例12: getRelatedPart
import javax.mail.Part; //导入方法依赖的package包/类
/**
* 获取消息附件中的文件.
* @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);
}
}
}
示例13: getMessageByContentTypes
import javax.mail.Part; //导入方法依赖的package包/类
protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception {
Map<String, String> messageMap = new HashMap<>();
if (message.isMimeType(TEXT_PLAIN)) {
messageMap.put(TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString()));
} else if (message.isMimeType(TEXT_HTML)) {
messageMap.put(TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString())));
} else if (message.isMimeType(MULTIPART_MIXED) || message.isMimeType(MULTIPART_RELATED)) {
messageMap.put(MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet));
} else {
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
for (int i = 0, n = mpart.getCount(); i < n; i++) {
Part part = mpart.getBodyPart(i);
if (decryptMessage && part.getContentType() != null &&
part.getContentType().equals(ENCRYPTED_CONTENT_TYPE)) {
part = decryptPart((MimeBodyPart)part);
}
String disposition = part.getDisposition();
String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";"));
if (disposition == null) {
if (part.getContent() instanceof MimeMultipart) {
// multipart with attachment
MimeMultipart mm = (MimeMultipart) part.getContent();
for (int j = 0; j < mm.getCount(); j++) {
if (mm.getBodyPart(j).getContent() instanceof String) {
BodyPart bodyPart = mm.getBodyPart(j);
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
String contentType = bodyPart.getHeader(CONTENT_TYPE)[0];
contentType = contentType
.replace(contentType.substring(contentType.indexOf("=") + 1), characterSet);
bodyPart.setHeader(CONTENT_TYPE, contentType);
}
String partContentType1 = bodyPart
.getContentType().substring(0, bodyPart.getContentType().indexOf(";"));
messageMap.put(partContentType1,
MimeUtility.decodeText(bodyPart.getContent().toString()));
}
}
} else {
//multipart - w/o attachment
//if the user has specified a certain characterSet we decode his way
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
InputStream istream = part.getInputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream));
int count = bis.available();
byte[] bytes = new byte[count];
count = bis.read(bytes, 0, count);
messageMap.put(partContentType,
MimeUtility.decodeText(new String(bytes, 0, count, characterSet)));
} else {
messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString()));
}
}
}
} //for
} //else
return messageMap;
}
示例14: getAttachedFileNames
import javax.mail.Part; //导入方法依赖的package包/类
protected String getAttachedFileNames(Part part) throws Exception {
String fileNames = "";
Object content = part.getContent();
if (!(content instanceof Multipart)) {
if (decryptMessage && part.getContentType() != null &&
part.getContentType().equals(ENCRYPTED_CONTENT_TYPE)) {
part = decryptPart((MimeBodyPart) part);
}
// non-Multipart MIME part ...
// is the file name set for this MIME part? (i.e. is it an attachment?)
if (part.getFileName() != null && !part.getFileName().equals("") && part.getInputStream() != null) {
String fileName = part.getFileName();
// is the file name encoded? (consider it is if it's in the =?charset?encoding?encoded text?= format)
if (fileName.indexOf('?') == -1) {
// not encoded (i.e. a simple file name not containing '?')-> just return the file name
return fileName;
}
// encoded file name -> remove any chars before the first "=?" and after the last "?="
return fileName.substring(fileName.indexOf("=?"), fileName.length() -
((new StringBuilder(fileName)).reverse()).indexOf("=?"));
}
} else {
// a Multipart type of MIME part
Multipart mpart = (Multipart) content;
// iterate through all the parts in this Multipart ...
for (int i = 0, n = mpart.getCount(); i < n; i++) {
if (!"".equals(fileNames)) {
fileNames += STR_COMMA;
}
// to the list of attachments built so far append the list of attachments in the current MIME part ...
fileNames += getAttachedFileNames(mpart.getBodyPart(i));
}
}
return fileNames;
}
示例15: getAttachmentsOfMultipartMessage
import javax.mail.Part; //导入方法依赖的package包/类
/**
* Returns an attachment list for a given Mime Multipart object.
*
* @param mp the input Mime part of the email's content
* @param level debug variable
* @return list of files where the attachments are saved
* @throws MessagingException
* @throws IOException
*/
private List<Attachment> getAttachmentsOfMultipartMessage(Multipart mp, boolean onlyInfo, int level) throws MessagingException, IOException {
List<Attachment> files = new LinkedList<Attachment>();
for (int j = 0; j < mp.getCount(); j++) {
Part bp = mp.getBodyPart(j);
String contentType = bp.getContentType().toLowerCase();
if (!Part.ATTACHMENT.equalsIgnoreCase(bp.getDisposition())) {
if (contentType.indexOf("multipart/") != -1) {
files.addAll(getAttachmentsOfMultipartMessage((Multipart)(bp.getContent()), onlyInfo, level + 1));
}
continue;
}
String fName = bp.getFileName() == null ? "noname" : bp.getFileName();
files.add(new Attachment(MimeUtility.decodeText(fName), bp.getSize()));
if (!onlyInfo) {
InputStream is = bp.getInputStream();
File f = new File(this.attachmentFolder + bp.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while((bytesRead = is.read(buf))!=-1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
}
return files;
}