下载文件时出现ServletException

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了下载文件时出现ServletException相关的知识,希望对你有一定的参考价值。

我试图实现下载功能。该项目是在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)?如何解决这个问题呢?谢谢。

答案

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

以上是关于下载文件时出现ServletException的主要内容,如果未能解决你的问题,请参考以下文章

在 Tomcat 上实现 Jersey 时出现“javax.servlet.ServletException: java.lang.NoClassDefFoundError”

尝试下载文件时出现 Swift Alamofire 错误

通过ruby OpenURI下载文件时出现间歇性EOFError

从android下载文件时出现套接字异常

从sharepoint rest api下载文件时出现400错误

IE11下载文件时出现文件名乱码