undertow 下读取 excel 的问题

使用版本 jfinal 3.7 , undertow 3.6 , poi-ooxxml 4.0 , xmlbeans 3.1

读取excel代码

public static List<Map> xxsfExcel(){
    File filePath = new File("d:\\test.xlsx");
    List<Map> listmap = new ArrayList<Map>();
    try {
        //读取文件
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
        //获取第一个sheet , 0 ,有标题的可以getstring
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        //获取总行数,要删除空行列,否则都会读取
        int rowNums = xssfSheet.getLastRowNum();
        //获取总列数 , 第一行的总列数
        int columns  = xssfSheet.getRow(0).getPhysicalNumberOfCells();
        System.out.println(rowNums);
        for(int i =1;i<=rowNums;i++){
            Row row = xssfSheet.getRow(i);
            Map map = new HashMap();
            for(int j=0;j<=columns;j++){
                if (row.getCell(j)!=null  && !row.getCell(j).equals("")) {
                    Cell cell = row.getCell(j);
                    cell.setCellType(CellType.STRING);
                    switch (j) {
                        case 3:
                            System.out.println(cell.getStringCellValue());
                            map.put("certinfo",cell.getStringCellValue());
                            break;
                        case 5:
                            System.out.println(cell.getStringCellValue());
                            map.put("agentcode",cell.getStringCellValue());
                            break;
                        default:
                            break;
                    }
                }
            }
            listmap.add(map);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return listmap;
}

错误信息

org.apache.poi.ooxml.POIXMLException: loader constraint violation: loader (instance of sun/misc/Launcher$AppClassLoader) previously initiated loading for a different type with name "org/apache/xmlbeans/XmlOptions"
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:66)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:657)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:282)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:303)
at com.myinsure.utils.excel.ReadExcel.xxsfExcel(ReadExcel.java:27)
at com.myinsure.common.controller.ExcelToDbController.etod(ExcelToDbController.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:75)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:89)
at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:48)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:89)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:63)
... 59 more
Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of sun/misc/Launcher$AppClassLoader) previously initiated loading for a different type with name "org/apache/xmlbeans/XmlOptions"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.xmlbeans.XmlBeans.buildCompilationMethod(XmlBeans.java:228)
at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:134)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.getTypeLoader(Unknown Source)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
... 65 more


问题,直接使用 main 方法执行读取是没问题的,更换到tomcat读取也是没问题的,去掉xmlbeans会报错没有该类。在undertow环境下才会报错。 

评论区

JFinal

2019-09-29 23:58

在 undertow.txt 中添加一个配置试试:
undertow.hotSwapClassPrefix=org.apache.xmlbeans.

JFinal

2019-09-29 23:59

注意看文档:
https://www.jfinal.com/doc/1-5

看一下第 4 小节《4、类型转换异常、子类对象无法赋值到基类变量》

logger

2019-09-30 08:13

@JFinal 感谢波总,我认真学习一下

logger

2019-09-30 14:16

@JFinal 波总,看过热加载的文档了,不仅加了undertow.hotSwapClassPrefix=org.apache.xmlbeans.
还增加了undertow.hotSwapClassPrefix=org.apache.poi.
undertow.hotSwapClassPrefix=org.openxmlformats.schemas. 更换了多个版本的jar包,报的错也是五花八门,仍然是没办法解决。

logger

2019-09-30 14:47

刚换了个undertow的环境,试了一下是可以的,看来不是这方面的问题,是我引入的包有冲突

logger

2019-09-30 14:59

找到原因了,是我在启动的时候增加了org.apache. 热加载,修改为org.apache.shiro就可以了
public static void main(String[] args) {
UndertowServer.create(CommonConfig.class).configWeb(builder->{
builder.addListener("org.apache.shiro.web.env.EnvironmentLoaderListener");
builder.addFilter("shiro", "org.apache.shiro.web.servlet.ShiroFilter");
builder.addFilterUrlMapping("shiro", "/*");
}).addHotSwapClassPrefix("org.apache.").start();}

热门反馈

扫码入社