加载资源失败:使用ajax调用servlet时,服务器以500()状态响应

时间:2019-04-19 13:49:04

标签: ajax servlets

我有一个servlet,它的名称为copyImage,而servlet代码是这样的。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String docpath = null;
    String docname = null;
    File original=new File(docpath);
    File dest=new File("T:\\Temp\\");
    try {
        FileUtils.copyFileToDirectory(original, dest);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String newPath="T:\\Temp\\"+docname;
    request.getSession().setAttribute("newPath", newPath);
    doGet(request, response);
}

我想使用$ .ajax帖子来调用servlet。我编写了这段代码以使用ajax调用servlet。但这在控制台日志中给了我这个错误Failed to load resource: the server responded with a status of 500 ()。在月食中我得到这个错误

java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at org.solr.copyImage.doPost(copyImage.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我的ajax编码如下。

<script>
function getImages(name,path){
    var documentname=name;
    var documentpath=path;
    $.ajax({
        type:"POST",
        url:"copyImage",
        data:{
            docname:documentname,
            docpath:documentpath,
            },
        success:function(data){
            document.getElementById("showImages").src=data;
        },
    });
}
</script>

发送数据有问题吗?我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的代码可以:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String docpath = null;
    String docname = null;
    File original=new File(docpath);

根据documentation表示采用字符串的File构造函数:

  

投掷:

     

NullPointerException-如果pathname参数为null

创建新的docpath时,代码中的

File为空。