我尝试将JRVirtualPrintPage对象写入OutputStream,以便将其用于持久性文件。
但是当我从InputStream中读取时,我无法加载我在执行百分位数计算时定义的类
我的代码如下:
private void writePages() throws IOException, JRException {
OutputStream pos = new OutputStream();
ObjectOutputStream oos = new ObjectOutputStream(pos);
List pages = print.getPages();
numPages = pages == null ? 0 : pages.size();
writeInt(numPages);
pageFilePointers = new long[pages.size()];
long pfpOffset = writeObject(pageFilePointers);
for(int i = 0; i < numPages; i++) {
if (interrupted)
return;
if (Thread.currentThread().isInterrupted()) {
throw new JRException("Current thread interrupted.");
}
JRVirtualPrintPage vpage = (JRVirtualPrintPage)pages.get(i);
oos.writeObject(vpage);
for (JRPrintElement jrPrintElement : elements) {
oos.writeObject(jrPrintElement);
}
pageFilePointers[i] = writeBytes(pos.getBytes());
}
long saveFP = file.getFilePointer();
file.seek(pfpOffset);
writeObject(pageFilePointers);
file.seek(saveFP);
}
private void readPages() throws IOException, ClassNotFoundException {
int numPages = readInt();
pageFilePointers = (long[])readObject();
AJPersistenceInputStream pis = new AJPersistenceInputStream();
file.seek(pageFilePointers[0]);
byte[] pageBytes = readBytes();
pis.reset(pageBytes);
ObjectInputStream ois = new ObjectInputStream(pis);
JRVirtualPrintPage page = (JRVirtualPrintPage)ois.readObject();
if (startIndex == endIndex) {
for(int i=1; i<=startIndex; i++)
page = getPrintPage(pis, ois, i);
print.addPage(page);
} else {
endIndex = Math.min(endIndex, pageFilePointers.length-1);
for(int i=0; i<=endIndex; i++) {
if (i>0) // else already read page index == 0
page = getPrintPage(pis, ois, i);
if (i >= startIndex)
print.addPage(page);
}
}
}
我在下面收到了错误:
java.lang.ClassNotFoundException: com.alu.vital.vq.jasper.aggregation.AJPercentileAggregator
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:623)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1610)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:499)
at net.sf.jasperreports.engine.fill.JRTemplatePrintText.readObject(JRTemplatePrintText.java:91)
&#34; com.alu.vital.vq.jasper.aggregation.AJPercentileAggregator&#34; class是我定义为执行百分位计算的类。它是在构建报告时编译的。
为什么我使用JRVirtualPrintPage对象写入OutputStream,当从InputStream读取时,我还将对象转换为JRVirtualPrintPage类型,为什么会发生此异常?
请帮我解释一下这个细节。我该如何解决?我认为它适用于新的Virtualize机制,因为只有当我将 JasperReports 从3.7.4升级到5.0.1时才会出现此问题
答案 0 :(得分:0)
答案在行
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:499)
从堆栈跟踪的底部。
您的JRVirtualPrintPage类似乎有一个AJPercentileAggregator类型的字段。因此,当您序列化JRVirtualPrintPage时,正在序列化其所有字段,因此AJPercentileAggregator也是如此。
你可以:
将自定义序列化程序写入JRVirtualPrintPage,跳过AJPercentileAggregator类型的字段。
将此字段标记为瞬态,从序列化中跳过