![](/img/trans.png)
[英]java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
[英]org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException
我正在使用org.apache.commons.lang.SerializationUtils,但是我收到了一个错误。 如果您需要更多信息我是Java新手,请告诉我代码:
Profile profile2 = new Profile();
profile2.setFileName(path);
profile2.setStatus("UPLOADED");
byte[] payload2 = SerializationUtils.serialize(profile2);
profile = (Profile) SerializationUtils.deserialize(payload2);
运行时错误输出:
org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException: com.xxx.xxx.Profile
at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:166)
at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:193)
人们说Profile不在classpath中。 如果确实如此,则会在“new Profile()”上发生错误。 我对吗?
我找到了一个解决方法:
profile = (EveSuccessCriteriaProfile) SerializationUtils.deserialize(payload2);
取而代之
InputStream fis = null;
fis = new ByteArrayInputStream(payload2);
ObjectInputStream o = new ObjectInputStream(fis);
profile = (Profile) o.readObject();
它工作正常
问题是在OSGi中不仅有一次加载所有类的类加载器。 每个包有一个类加载器。 因此,如果您执行Profile.class.getClassLoader(),您将获得包含Profile的包的类加载器。 如果您执行SerializationUtils.class.getClassLoder(),那么您将获得commons.lang包的类加载器。 由于commons.lang没有一个Import-Package,因为包中的Profile不会看到它。 因此,如果SerializationUtils.deserialize使用自己的类加载器来加载Profile类,它将无法找到它。
我很确定它会尝试使用ThreadContext类加载器来加载用户类。 所以它可能有助于做Thread.currentThread().setContextClassLoader(Profile.class.getClassLoader());
在调用SerializationUtils.deserialize之前。
这只是一种解决方法。 应该增强反序列化调用以接受ClassLoader作为第二个参数。 因此,不要进入habbit,将ContextClassLoader视为最佳实践。
不幸的是,SerializationUtils在OSGi中不起作用。 您将不得不等待解决此问题: https : //issues.apache.org/jira/browse/LANG-1049
您应该尝试使用https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ClassLoaderObjectInputStream.html ,这样可以在没有伙伴类加载或类似的情况下干净地进行反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.