Java:获取MIME多部分BodyPart的内容

标签 java mime multipart

我正在尝试使用 BodyPart 检索 MIME 多部分的内容,如下所示

ByteArrayOutputStream baos = null;
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(inputStream, contentType));
int count = mp.getCount();
baos = new ByteArrayOutputStream();

for (int i = 0; i < count; i++) {
    BodyPart bodyPart = mp.getBodyPart(i);
    Object content = bodyPart.getContent();
    if (content instanceof InputStream) {

         // process inputStream

     }

bodyPart.writeTo(MIMEbaos);
String attachment = MIMEbaos.toString();

}

但是,当 attachment 包含时,当我只期望内容(没有内容类型、边界等)时,bodyPart.getContent() 提供与整个 MIME 消息相同的输入流整个 MIME 多部分正文部分,包括内容类型等。

InputStream 来自

ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);

byte[] bytes = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(bytes);

其中msgSOAPMessage MIME类型作为MTOM

最佳答案

我最终得到了

ByteArrayOutputStream baos = null;
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(inputStream, contentType));
int count = mp.getCount();
baos = new ByteArrayOutputStream();

for (int i = 0; i < count; i++) {
    BodyPart bodyPart = mp.getBodyPart(i);
    Object content = bodyPart.getContent();
    String content = new String(read(bodyPart));

    String partContentType =  bodyPart.getContentType();
}

bodyPart.writeTo(MIMEbaos);
String attachment = MIMEbaos.toString();

    private static byte[] read(BodyPart bodyPart) throws MessagingException, IOException
    {
        InputStream inputStream = bodyPart.getInputStream();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        int data = 0;
        byte[] buffer = new byte[1024];

        while ((data = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, data);
        }

        return outputStream.toByteArray();
    }

关于Java:获取MIME多部分BodyPart的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53787081/

相关文章:

flash - 解析 X-amf mime 类型数据

将文件上传到 REST URL 的 Python 3 脚本(多部分请求)

ubuntu - Linux : E_FAIL 上的 7zip 多部分存档

ios - 如何以多部分形式将多个图像文件发送到服务器?

java - 使用 spock 在测试中生成对象列表

java - Tomcat Spring 中的 SSLSocket

java.lang.NoClassDefFoundError : org/apache/logging/log4j/core/Layout while running integration test using ESIntegTestCase

java - 将当前 EST 与下午 2 点 EST 进行比较?如何?

php - 来自 PHP 的电子邮件已破坏主题 header 编码

python - 谷歌应用引擎上的 mimetypes.mime_guess() 行为异常