Getting ClassCastException in java
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开发者_JAVA百科 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...
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
}
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
}
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
}
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.
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.
精彩评论