请求通过 ajax 下载 servlet 文件(请不要 jquery..)

Requesting to a servlet file downloading via ajax(no jquery please..)

过程是这样的:

  1. 来自网络(jsp)我上传了一些 pdf 文件(通过 ajax 提交)
  2. 在后端我合并这些 pdf
  3. 我通过 ajax 收到响应(合并的 pdf) --> 开始文件下载...

我在执行第三步时遇到问题。

我只在提交要上传的文件(post 请求)和开始下载时包含了相关代码。 我还放了一个直接 link,它在 get 方法中调用相同的步骤并起作用。

我的问题在哪里? 提前致谢...

这里是jsp正文标签

<a href="/TestAjaxServletDownload/DownloadServlet" >
    download
</a>

<p><input id="sampleFile5" name="sampleFile5" type="file" /></p>

<p><input id="uploadBtn" type="button" value="Upload" onClick="javascript:performAjaxSubmit();"></input></p>

这是我的javascript标签内容

function performAjaxSubmit() {

        var sampleFile1 = document.getElementById("sampleFile5").files[0];
        var formdata = new FormData();

        formdata.append("sampleFile", sampleFile1);

        var xhr = new XMLHttpRequest();       

        xhr.onload = function() {
            if(xhr.readyState == 4 && xhr.status == 200) {
//              alert("ok..." + xhr.responseText);
                //?????????????????????????????
                document.location=xhr.responseText;               
            }
        };   

        xhr.open("POST","/TestAjaxServletDownload/DownloadServlet", true);
        xhr.send(formdata);

    }

这是我的 web.xml servelet 映射标签

<servlet>
    <description></description>
    <display-name>DownloadServlet</display-name>
    <servlet-name>DownloadServlet</servlet-name>
    <servlet-class>test.DownloadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/DownloadServlet</url-pattern>
  </servlet-mapping>

这是我的 servlet 代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("DO GET SERVLET MERGE");
        execute (request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("DO POST SERVLET MERGE");
        execute (request, response);
    }


    protected void execute(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        File downloadFile = new File("c:\data\example.pdf");
        System.out.println("++++" + downloadFile.getAbsolutePath());
//      System.out.println(uploadPathTemp+mergeFileName);
        FileInputStream inStream = new FileInputStream(downloadFile);
         // obtains ServletContext
         ServletContext context = getServletContext();

         // gets MIME type of the file
         String mimeType = context.getMimeType(downloadFile.getCanonicalPath());
         if (mimeType == null) {        
             // set to binary type if MIME mapping not found
             mimeType = "application/octet-stream";
         }

         // modifies response
         response.setContentType(mimeType);
         response.setContentLength((int) downloadFile.length());

         // forces download
         String headerKey = "Content-Disposition";
         String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
         System.out.println(downloadFile.getName());
         response.setHeader(headerKey, headerValue);

         // obtains response's output stream
         OutputStream outStream = response.getOutputStream();

         byte[] buffer = new byte[4096];
         int bytesRead = -1;

         while ((bytesRead = inStream.read(buffer)) != -1) {
             outStream.write(buffer, 0, bytesRead);
         }

         inStream.close();
         outStream.close();

    }

改变什么

<a href="/TestAjaxServletDownload/DownloadServlet" > download </a>

<a id="pdfLink" href="/TestAjaxServletDownload/DownloadServlet" > download </a>

然后使用document.getElementById('pdfLink').click()?