在java中上传Excel文件时出错

时间:2014-04-23 06:07:35

标签: java windows spring

您好我使用spring在netbeans中为上传文件制作了一个功能,但它不起作用,我发送下面的控制器代码请更正代码

    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j                                                                                        ava:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j                                                                                        ava:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal                                                                                        ve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav                                                                                        a:408)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(                                                                                        AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoin                                                                                        t.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.                                                                                        java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor                                                                                        .java:615)
    at java.lang.Thread.run(Thread.java:744)
    java.lang.RuntimeException: D:/101-200 (2003).xls (No such file or directory)
    at Controller.AdminExelSheetController.read(AdminExelSheetController.jav                                                                                        a:323)
    at Controller.AdminExelSheetController.onSubmit(AdminExelSheetController                                                                                        .java:93)
    at Controller.AdminExelSheetController.onSubmit(AdminExelSheetController                                                                                        .java:93)
    at org.springframework.web.servlet.mvc.SimpleFormController.processFormS                                                                                        ubmission(SimpleFormController.java:272)
    at org.springframework.web.servlet.mvc.AbstractFormController.handleRequ                                                                                        estInternal(AbstractFormController.java:268)
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(                                                                                        AbstractController.java:153)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.ha                                                                                        ndle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch                                                                                        erServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(Dispatche                                                                                        rServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(Frame                                                                                        workServlet.java:882)
     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServ

在上面的代码中,我使用FileInputStream导入文件,但此代码无效。我没有上传文件,我得到错误消息。在这个项目中我使用spring框架和hibernate
在上面的代码告诉我问题在哪里

我的控制器代码在

之下
public static Vector read(File fileName) {
    Vector rowStore = new Vector();
    try {
        FileInputStream inputFile = null;
        try {
            inputFile = new FileInputStream("D:/" + fileName);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        POIFSFileSystem systemFile = new POIFSFileSystem(inputFile);
        HSSFWorkbook wb = new HSSFWorkbook(systemFile);
        HSSFSheet sheet = wb.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        if (!"".equals(rows.toString())) {
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                Vector cellStore = new Vector();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    cellStore.addElement(cell);
                }
                rowStore.addElement(cellStore);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rowStore;
}

1 个答案:

答案 0 :(得分:0)

在您说自己在Windows操作系统上的标签中,您的错误在路径定义中。在Windows中,Path由" \"声明。而不是" /"。

在java中,\符号是转义符号,所以你必须转义才能使用它(sry for the many" escape":D):代码看起来像这样:

inputFile = new FileInputStream("D:\\" + fileName);