本文整理汇总了Java中javax.mail.Part.getDisposition方法的典型用法代码示例。如果您正苦于以下问题:Java Part.getDisposition方法的具体用法?Java Part.getDisposition怎么用?Java Part.getDisposition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Part
的用法示例。
在下文中一共展示了Part.getDisposition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: downloadAttachment
import javax.mail.Part; //导入方法依赖的package包/类
private void downloadAttachment(Part part, String folderPath) throws MessagingException, IOException {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedAttachmentName = null;
if (fileName != null) {
LOGGER.info("Attached File Name :: " + fileName);
decodedAttachmentName = MimeUtility.decodeText(fileName);
LOGGER.info("Decoded string :: " + decodedAttachmentName);
decodedAttachmentName = Normalizer.normalize(decodedAttachmentName, Normalizer.Form.NFC);
LOGGER.info("Normalized string :: " + decodedAttachmentName);
int extensionIndex = decodedAttachmentName.indexOf(EXTENSION_VALUE_46);
extensionIndex = extensionIndex == -1 ? decodedAttachmentName.length() : extensionIndex;
File parentFile = new File(folderPath);
LOGGER.info("Updating file name if any file with the same name exists. File : " + decodedAttachmentName);
decodedAttachmentName = FileUtils.getUpdatedFileNameForDuplicateFile(decodedAttachmentName.substring(0, extensionIndex), parentFile, -1)
+ decodedAttachmentName.substring(extensionIndex);
LOGGER.info("Updated file name : " + decodedAttachmentName);
}
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
File file = new File(folderPath + File.separator + decodedAttachmentName);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
示例2: parsePart
import javax.mail.Part; //导入方法依赖的package包/类
private void parsePart(Part part) throws Exception {
if (part.isMimeType("text/*")) {
content.append((String) part.getContent());
} else if (part.isMimeType("multipart/*")) {
Part p = null;
Multipart multipart = (Multipart) part.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
p = multipart.getBodyPart(i);
String disposition = p.getDisposition();
if (disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
attachments.add(MimeUtility.decodeText(p.getFileName()));
}
parsePart(p);
}
} else if (part.isMimeType("message/rfc822")) {
parsePart((Part) part.getContent());
}
}
示例3: getContent
import javax.mail.Part; //导入方法依赖的package包/类
public static String getContent(Part message) throws MessagingException,
IOException {
if (message.getContent() instanceof String) {
return message.getContentType() + ": " + message.getContent() + " \n\t";
} else if (message.getContent() != null && message.getContent() instanceof Multipart) {
Multipart part = (Multipart) message.getContent();
String text = "";
for (int i = 0; i < part.getCount(); i++) {
BodyPart bodyPart = part.getBodyPart(i);
if (!Message.ATTACHMENT.equals(bodyPart.getDisposition())) {
text += getContent(bodyPart);
} else {
text += "attachment: \n" +
"\t\t name: " + (StringUtils.isEmpty(bodyPart.getFileName()) ? "none"
: bodyPart.getFileName()) + "\n" +
"\t\t disposition: " + bodyPart.getDisposition() + "\n" +
"\t\t description: " + (StringUtils.isEmpty(bodyPart.getDescription()) ? "none"
: bodyPart.getDescription()) + "\n\t";
}
}
return text;
}
if (message.getContent() != null && message.getContent() instanceof Part) {
if (!Message.ATTACHMENT.equals(message.getDisposition())) {
return getContent((Part) message.getContent());
} else {
return "attachment: \n" +
"\t\t name: " + (StringUtils.isEmpty(message.getFileName()) ? "none"
: message.getFileName()) + "\n" +
"\t\t disposition: " + message.getDisposition() + "\n" +
"\t\t description: " + (StringUtils.isEmpty(message.getDescription()) ? "none"
: message.getDescription()) + "\n\t";
}
}
return "";
}
示例4: getMainTextPart
import javax.mail.Part; //导入方法依赖的package包/类
/**
* Returns the <code>Part</code> whose <code>content</code>
* should be displayed inline.
* @throws MessagingException
* @throws IOException
*/
private Part getMainTextPart() throws MessagingException, IOException {
List<Part> parts = getParts();
Part mostPreferable = this;
for (Part part: parts) {
String disposition = part.getDisposition();
if (!Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
// prefer plain text
if (part.isMimeType("text/plain"))
return part;
else if (part.isMimeType("text/html"))
mostPreferable = part;
}
}
return mostPreferable;
}
示例5: processMultipart
import javax.mail.Part; //导入方法依赖的package包/类
private String processMultipart(Part part) throws IOException,
MessagingException {
Multipart relatedparts = (Multipart)part.getContent();
for (int j = 0; j < relatedparts.getCount(); j++) {
Part rel = relatedparts.getBodyPart(j);
if (rel.getDisposition() == null) {
// again, if it's not an image or attachment(only those have disposition not null)
if (rel.isMimeType("multipart/alternative")) {
// last crawl through the alternative formats.
return extractAlternativeContent(rel);
}
}
}
return null;
}
示例6: 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;
}
示例7: 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;
}
示例8: extractAttachmentsFromMultipart
import javax.mail.Part; //导入方法依赖的package包/类
protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, DataHandler> map)
throws MessagingException, IOException {
for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
LOG.trace("Part #" + i + ": " + part);
if (part.isMimeType("multipart/*")) {
LOG.trace("Part #" + i + ": is mimetype: multipart/*");
extractAttachmentsFromMultipart((Multipart) part.getContent(), map);
} else {
String disposition = part.getDisposition();
String fileName = part.getFileName();
if (LOG.isTraceEnabled()) {
LOG.trace("Part #{}: Disposition: {}", i, disposition);
LOG.trace("Part #{}: Description: {}", i, part.getDescription());
LOG.trace("Part #{}: ContentType: {}", i, part.getContentType());
LOG.trace("Part #{}: FileName: {}", i, fileName);
LOG.trace("Part #{}: Size: {}", i, part.getSize());
LOG.trace("Part #{}: LineCount: {}", i, part.getLineCount());
}
if (validDisposition(disposition, fileName)
|| fileName != null) {
LOG.debug("Mail contains file attachment: {}", fileName);
if (!map.containsKey(fileName)) {
// Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
map.put(fileName, part.getDataHandler());
} else {
LOG.warn("Cannot extract duplicate file attachment: {}.", fileName);
}
}
}
}
}
示例9: getMailPartAsText
import javax.mail.Part; //导入方法依赖的package包/类
public static String getMailPartAsText(final Part part) throws MessagingException, IOException {
String partString = "";
if (part.isMimeType("text/*")) {
partString = (String) part.getContent();
} else if (part.isMimeType("image/*")) {
final String contentType = part.getContentType();
final String disposition = part.getDisposition();
final String fileName = part.getFileName();
final StringBuilder imageTextString = new StringBuilder();
imageTextString.append("[Image]");
imageTextString.append("[Inline]");
imageTextString.append(contentType);
imageTextString.append(fileName);
partString = imageTextString.toString();
final StringBuilder imageInfoLogString = new StringBuilder();
imageInfoLogString.append("Image in body.. with name:: ");
imageInfoLogString.append(fileName);
imageInfoLogString.append(" disposition:: ");
imageInfoLogString.append(disposition);
imageInfoLogString.append("HTML file tag:: ");
imageInfoLogString.append(partString);
LOGGER.info(imageInfoLogString.toString());
} else if (part.isMimeType("multipart/*")) {
final Multipart multiPart = (Multipart) part.getContent();
for (int i = 0; i < multiPart.getCount(); i++) {
partString = getMailPartAsText(multiPart.getBodyPart(i));
}
}
return partString;
}
示例10: handlePart
import javax.mail.Part; //导入方法依赖的package包/类
public static void handlePart(Part part) throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { // When just body
System.out.println("Null: " + contentType);
// Check if plain
if (contentType.length() >= 10 && contentType.toLowerCase().substring(0, 10).equals("text/plain")) {
part.writeTo(System.out);
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() + " : " + contentType);
MailClient.saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " + part.getFileName() + " : " + contentType);
MailClient.saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
System.out.println("Other: " + disposition);
}
}
示例11: getDisposition
import javax.mail.Part; //导入方法依赖的package包/类
public static String getDisposition( Part _mess ) throws MessagingException{
String dispos = _mess.getDisposition();
// getDisposition isn't 100% reliable
if ( dispos == null ){
String [] disposHdr = _mess.getHeader( "Content-Disposition" );
if ( disposHdr != null && disposHdr.length > 0 && disposHdr[0].startsWith( Part.ATTACHMENT ) ){
return Part.ATTACHMENT;
}
}
return dispos;
}
示例12: 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;
}
示例13: handlePart
import javax.mail.Part; //导入方法依赖的package包/类
public static void handlePart(String foldername, Part part) throws MessagingException, IOException
{
String disposition = part.getDisposition();
// String contentType = part.getContentType();
if ((disposition != null)
&& (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)))
{
saveFile(foldername, MimeUtility.decodeText(part.getFileName()), part.getInputStream());
}
}
示例14: 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;
}
示例15: extractAlternativeContent
import javax.mail.Part; //导入方法依赖的package包/类
private String extractAlternativeContent(Part part) throws IOException, MessagingException {
Multipart alternatives = (Multipart)part.getContent();
Object content = "";
for (int k = 0; k < alternatives.getCount(); k++) {
Part alternative = alternatives.getBodyPart(k);
if (alternative.getDisposition() == null) {
content = alternative.getContent();
}
}
return content.toString();
}