【问题标题】:Gmail Attachment processing, Base64DecoderStream to InputStreamGmail 附件处理,Base64DecoderStream 转 InputStream
【发布时间】:2014-11-26 07:57:37
【问题描述】:

我需要从 GMail 中获取附件并将​​其上传到 Amazon S3。

我正在使用 imap 连接到 GMail 并能够访问附件, 使用 javax.mail.internet.MimeBodyPart,它提供了 Base64DecoderStream 中的 getInputStream(),而不是 FileInputStream 或 ByteArray 输入流。 由于我的文件是二进制文件(如 .zip)。

我需要 InputStream 将其上传到 S3。

那么如何将 Base64DecoderStream 转换为 InputStream?

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);
            }
        }
}

//上传功能如下

public void processAttachment(InputStream asset) {

        ObjectMetadata meta = new ObjectMetadata();
            if (asset instanceof FileInputStream) {
                meta.setContentLength(((FileInputStream)asset).available());
            } else if (asset instanceof ByteArrayInputStream) {
                meta.setContentLength(((ByteArrayInputStream)asset).available());
            } 
            else {
                meta.setContentLength(asset.available());
            } 

        s3.putObject(new PutObjectRequest(bucket, "parentfolder/subfolder/abc.zip", asset, meta));

    }

【问题讨论】:

    标签: java amazon-s3 gmail inputstream attachment


    【解决方案1】:

    如果您需要将整个消息发送到流中。

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多