Java Gmail附件处理,Base64DecoderStream到InputStream
我需要从GMail获取附件并上传到AmazonS3 我正在使用imap连接到GMail并能够访问附件, 使用javax.mail.internet.MimeBodyPart,它提供Base64DecoderStream中的getInputStream(),而不是FileInputStream或ByteArray输入流。 因为我的文件是二进制文件(比如.zip) 我需要InputStream将其上传到S3 那么如何将Base64DecoderStream转换为InputStream?Java Gmail附件处理,Base64DecoderStream到InputStream,java,amazon-s3,gmail,inputstream,attachment,Java,Amazon S3,Gmail,Inputstream,Attachment,我需要从GMail获取附件并上传到AmazonS3 我正在使用imap连接到GMail并能够访问附件, 使用javax.mail.internet.MimeBodyPart,它提供Base64DecoderStream中的getInputStream(),而不是FileInputStream或ByteArray输入流。 因为我的文件是二进制文件(比如.zip) 我需要InputStream将其上传到S3 那么如何将Base64DecoderStream转换为InputStream? public
public void processMails() {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
Session session = null;
Store store = null;
session = Session.getInstance(props, null);
Folder inboxFolder;
try {
store = session.getStore();
store.connect("imap.gmail.com", "test@gmail.com", "password");
inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_WRITE);
Message messages[] = inboxFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
for (Message msg : messages) {
try {
Multipart multiPart = (Multipart)msg.getContent();
for (int i = 0; i < multiPart.getCount(); i++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
InputStream stream = null;// need to convert part.getInputStream() to InputStream
processAttachment(stream);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
如果您需要整个味精流。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
msg.writeTo(bos);
bos.close();
InputStream in = new ByteArrayInputStream(bos.toByteArray());
如果您只需要内容,请尝试此功能
InputStream base64InputStream = (InputStream) part.getInputStream();
int i = 0;
byte[] byteArray = new byte[base64InputStream.available()];
while ((i = (int) ((InputStream) base64InputStream).available()) > 0) {
int result = (int) (((InputStream) base64InputStream).read(byteArray));
if (result == -1)
break;
}
InputStream inputStream = new ByteArrayInputStream(byteArray);
InputStream base64InputStream = (InputStream) part.getInputStream();
int i = 0;
byte[] byteArray = new byte[base64InputStream.available()];
while ((i = (int) ((InputStream) base64InputStream).available()) > 0) {
int result = (int) (((InputStream) base64InputStream).read(byteArray));
if (result == -1)
break;
}
InputStream inputStream = new ByteArrayInputStream(byteArray);