在java中获取ClassCastException

发布于 2024-12-01 21:34:27 字数 1254 浏览 3 评论 0原文

您好,我在线程“main”java.lang.ClassCastException 中收到以下异常

:javax.mail.util.SharedByteArrayInputStream 无法转换为 javax.mail.Multipart

我在 Eclipse IDE 中没有收到任何编译异常,但是当我尝试构建项目时我收到此异常。

构建项目后,我通过 java -jar 运行该项目,因此它不满足 if(content instanceof Multipart) 条件,但当我从 Eclipse IDE 运行时,它工作正常。任何指针都会对我有很大帮助

从 eclipse IDE 我得到 megssage.getContent() 作为 javax.mail.internet.MimeMultipart@1dc0e7a 但是当使用 jar 文件运行时,我得到的内容为 javax.mail.util.SharedByteArrayInputStream@2f0d54

请您告诉我它们之间有什么区别。

修改后的代码是:

 InputStream inStream = null;
    if(!message.getContentType().contains("text/plain")){
        Object content = message.getContent();          
        if (message.isMimeType("multipart/*")) {  
            //message.isMimeType("multipart/*")||
            Multipart multipart = (Multipart) content;
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                inStream = bodyPart.getInputStream();
                fileName=bodyPart.getFileName();
                } 
        }
        else{
        System.out.println("content not instance of multipart");    
        }`enter code here`  

请任何人都可以帮我解决这个问题..

提前致谢...

Hi I am getting the following exception

Exception in thread "main" java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.Multipart

I am not getting any compilation exception in Eclipse IDE, but when I am trying build the project I am getting this exception.

After building the project, I am running the project through java -jar, so its not satisfying the if(content instanceof Multipart) condition, but when i am running from the Eclipse IDE its working fine. Any pointers will greatly helpful to me

From the eclipse IDE I'm getting the megssage.getContent() as javax.mail.internet.MimeMultipart@1dc0e7a
but when running using the jar file I'm getting the content as
javax.mail.util.SharedByteArrayInputStream@2f0d54

Please can you tell me what is the difference between them.

Modified code is:

 InputStream inStream = null;
    if(!message.getContentType().contains("text/plain")){
        Object content = message.getContent();          
        if (message.isMimeType("multipart/*")) {  
            //message.isMimeType("multipart/*")||
            Multipart multipart = (Multipart) content;
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                inStream = bodyPart.getInputStream();
                fileName=bodyPart.getFileName();
                } 
        }
        else{
        System.out.println("content not instance of multipart");    
        }`enter code here`  

Please can anyone help me out in solving this issue..

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆萌少年 2024-12-08 21:34:27

message.getContent() 此处返回 javax.mail.util.SharedByteArrayInputStream,但 SharedByteArrayInputStream 无法转换为 Multipart 实例,因为您不一定有多部分消息。

您可以检查它的 mimetype 是否是多部分的任何内容:

if (message.isMimeType("multipart/*") {
    Multipart mp = (Multipart)message.getContent();
    // more stuff
}

或者您可以执行...的实例

if (message.getContent() instanceof Multipart) {
    Multipart mp = (Multipart)message.getContent();
    // more
}

message.getContent() returns javax.mail.util.SharedByteArrayInputStream here, but the SharedByteArrayInputStream is not cast-able to a Multipart instance, because you may not necessarily have a multipart message.

You could check if its mimetype is a multipart anything:

if (message.isMimeType("multipart/*") {
    Multipart mp = (Multipart)message.getContent();
    // more stuff
}

Or you can do instance of...

if (message.getContent() instanceof Multipart) {
    Multipart mp = (Multipart)message.getContent();
    // more
}
小红帽 2024-12-08 21:34:27

您遇到异常,因为 getContent 的返回值是对 javax.mail.util.SharedByteArrayInputStream 的引用,并且该类未实现 Multipart< /代码>。据推测这不是多部分邮件消息。

正如 Part.getContent 的文档所述:

将内容作为 Java 对象返回。返回对象的类型当然取决于内容本身。例如,为“text/plain”内容返回的对象通常是 String 对象。为“多部分”内容返回的对象始终是 Multipart 子类。对于 DataHandler 系统未知的内容类型,输入流将作为内容返回

因此基本上,如果您想以特定方式处理多部分消息,则需要使用:

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // ...
}
else
{
    // Handle non-multipart content
}

You're getting an exception because the return value of getContent is a reference to a javax.mail.util.SharedByteArrayInputStream and that class doesn't implement Multipart. Presumably this isn't a multipart mail message.

As the documentation for Part.getContent states:

Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass. For content-types that are unknown to the DataHandler system, an input stream is returned as the content

So basically if you want to handle multipart messages in a particular way, you'll need to use:

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // ...
}
else
{
    // Handle non-multipart content
}
给我一枪 2024-12-08 21:34:27

好的,这就是正在发生的事情。看起来您正在尝试从实现 javax.mail.Part 的对象获取内容,但格式未知,在这种情况下 MimeMessage 将返回输入流。在本例中,它返回一个 javax.mail.util.SharedByteArrayInputStream。无论如何,输入流无法转换为 Multipart 接口。

您可以使用 isMimeType (birrryree的建议):

if (message.isMimeType("multipart/*") 
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}

或者您可以测试直接匹配(我最初的建议):

// other string comparisons will work here too.
if(message.getContentType().equals("multipart"))
{
     Multipart multipart = (Multipart) message.getContent();
    // what you have above.
}
else
{
    // it is not multi-part
}

getContentType 也在 Part 接口上。其文档可以在此处找到.
您可以在此处查看所有可能的内容类型的列表。

或者您可以根据instanceof的结果进行测试(Jon Skeet的回答):

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}

Ok, so here is what is happening. It looks like you're trying to get the content from an object which implements javax.mail.Part, but the format is unknown, in which case MimeMessage will return an input stream. In this case, it is returning a javax.mail.util.SharedByteArrayInputStream. Regardless, an input stream cannot be converted to the Multipart interface.

You can test to see if it is an implementer of multipart using isMimeType (birryree's suggestion):

if (message.isMimeType("multipart/*") 
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}

Or you can test for a direct match (my original suggestion):

// other string comparisons will work here too.
if(message.getContentType().equals("multipart"))
{
     Multipart multipart = (Multipart) message.getContent();
    // what you have above.
}
else
{
    // it is not multi-part
}

getContentType is also on the Part interface. Its documentation can be found here.
You can see a list of all possible content types here.

Or you can test based on the result of instanceof (Jon Skeet's answer):

Object content = message.getContent();
if (content instanceof Multipart)
{
    Multipart multipart = (Multipart) content;
    // what you have above.
}
else
{
    // it is not multi-part
}
荒人说梦 2024-12-08 21:34:27

对于未知的 mime 类型,MimeMessage 类返回 ShraedByteArrayInputstream 作为 文档说。

使用 instanceof 检查返回类型,然后进行强制转换。

更新:

如果您使用与 Eclipse 中相同的源,并且 getContent() 方法的响应仍然不同,那么您可以尝试修改 file.encoding 属性。

示例:

java -Dfile.encoding=UTF8 -jar something.jar

Update2:

也许您的 jar 中使用了加载类的旧版本。请检查您的类路径中是否有已加载的类。

For unknown mime-types the MimeMessage class returns with ShraedByteArrayInputstream as the documentation says.

Check the returning type with instanceof then cast.

Update:

If you're using the same source as you did in Eclipse and the response of getContent() method is still different, then you can try modifying the file.encoding property.

Example:

java -Dfile.encoding=UTF8 -jar something.jar

Update2:

Maybe an older version of the loaded class is used in your jar. Please check your classpath for the loaded classes.

我们的影子 2024-12-08 21:34:27

导出Runnable jar文件时,选择将所需的库打包到生成的JAR中,即可解决问题。

这可能是因为无法正确找到某些字符集,因此无法解析返回的多部分对象。

When you export the Runnable jar file, choose package required libraries into generated JAR, which solves the problem.

This might because some charset can't be found properly, so the returned multipart object is not parsed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文