在java中获取ClassCastException
您好,我在线程“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
message.getContent()
此处返回javax.mail.util.SharedByteArrayInputStream
,但SharedByteArrayInputStream
无法转换为Multipart
实例,因为您不一定有多部分消息。您可以检查它的 mimetype 是否是多部分的任何内容:
或者您可以执行...的实例
message.getContent()
returnsjavax.mail.util.SharedByteArrayInputStream
here, but theSharedByteArrayInputStream
is not cast-able to aMultipart
instance, because you may not necessarily have a multipart message.You could check if its mimetype is a multipart anything:
Or you can do instance of...
您遇到异常,因为
getContent
的返回值是对javax.mail.util.SharedByteArrayInputStream
的引用,并且该类未实现Multipart< /代码>。据推测这不是多部分邮件消息。
正如
Part.getContent
的文档所述:因此基本上,如果您想以特定方式处理多部分消息,则需要使用:
You're getting an exception because the return value of
getContent
is a reference to ajavax.mail.util.SharedByteArrayInputStream
and that class doesn't implementMultipart
. Presumably this isn't a multipart mail message.As the documentation for
Part.getContent
states:So basically if you want to handle multipart messages in a particular way, you'll need to use:
好的,这就是正在发生的事情。看起来您正在尝试从实现 javax.mail.Part 的对象获取内容,但格式未知,在这种情况下 MimeMessage 将返回输入流。在本例中,它返回一个 javax.mail.util.SharedByteArrayInputStream。无论如何,输入流无法转换为 Multipart 接口。
您可以使用 isMimeType (birrryree的建议):
或者您可以测试直接匹配(我最初的建议):
getContentType 也在
Part
接口上。其文档可以在此处找到.您可以在此处查看所有可能的内容类型的列表。
或者您可以根据instanceof的结果进行测试(Jon Skeet的回答):
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 theMultipart
interface.You can test to see if it is an implementer of multipart using isMimeType (birryree's suggestion):
Or you can test for a direct match (my original suggestion):
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):
对于未知的 mime 类型,MimeMessage 类返回
ShraedByteArrayInputstream
作为 文档说。使用
instanceof
检查返回类型,然后进行强制转换。更新:
如果您使用与 Eclipse 中相同的源,并且
getContent()
方法的响应仍然不同,那么您可以尝试修改file.encoding
属性。示例:
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 thefile.encoding
property.Example:
Update2:
Maybe an older version of the loaded class is used in your jar. Please check your classpath for the loaded classes.
导出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.