下载文件时出现ServletException

时间:2018-03-04 21:58:07

标签: javascript ajax jsp servlets apache-poi

我试图实现下载功能。该项目是在Springframework中组织的。提交jsp表单后,将首先从DB查询记录,并将记录写入新的EXCEL文件。最后,将下载excel文件。

相关的jsp代码:

<form id="form1" name="form1" method="post" action="" onsubmit="javascript:searchList();">
	<input ></input>
   	…
</form>

function searchList(){
	…
	$.ajax({
  		url:”report/search”,
  		type:”POST”,
  		cache:false,
  		data: {xxx:xxx},
  		success:function(obj){
   			if (obj){
     				$.messager.alert(‘INFO’,obj.msg);
   			}
  		},
  		error:function(ajaxErrObj){
  			$.messager.alert(‘Error’, ajaxErrObj.status);
  		}
 	});
}

相关Java代码:

@RequestMapping(value = "/search", method = RequestMethod.POST)
public Object search(@RequestParam(“xxx”) String xxx, HttpServletResponse response
) {
try (OutputStream out = response.getOutputStream(); Workbook wb = new XSSFWorkbook();){
    Sheet oifSheet = wb.createSheet(“results”);

    //… generate content  

    String fileName = PRE_FILENAME + "_" + DateUtils.getDate(new Date().toString());
    response.reset();
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
    wb.write(out);
    wb.close();
    out.flush();
    out.close();
    dataResult.setErrorCode(0);
    dataResult.setMsg("success”);
    return dataResult;
} catch (IOException e) {
    e.printStackTrace();
    dataResult.setErrorCode(1);
    dataResult.setMsg(“Error1”);
    return dataResult;
} catch (Exception e) {
    e.printStackTrace();
    dataResult.setErrorCode(1);
    dataResult.setMsg(“Errors !”);
    return dataResult;
}
}

当测试功能时。我会在网页上看到“成功”信息。然而 1.虽然执行了代码(由记录器跟踪),但实际上没有下载文件 2. ServletException被抛出为:

SEVERE: Servlet.service() for servlet jsp threw exception

javax.servlet.ServletException: File &quot;/WEB-INF/views/report/search.jsp&quot; not found
 at org.apache.jasper.servlet.JspServlet.handleMissingResource(JspServlet.java:412)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
 at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
 at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
 at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:160)
 at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
 at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
 at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
 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)

/ report / search是我用来调用服务的URL,为什么它原来是一个文件(/ filepath)? 如何解决这个问题呢? 感谢。

1 个答案:

答案 0 :(得分:0)

View Resolver中的问题。检查您的View Resolver。您可能正在使用基于XML的配置或基于注释的配置。