Javascript 在新浏览器窗口的响应中显示PDF ByTestStream

Javascript 在新浏览器窗口的响应中显示PDF ByTestStream,javascript,html,spring-mvc,pdf,Javascript,Html,Spring Mvc,Pdf,我正在尝试使用Javascript在新窗口中弹出PDF文件的字节流 在后端,我使用一个Spring控制器代码,如下所示 @RequestMapping(value = "/print", method = RequestMethod.POST, consumes="application/json") public ModelAndView printCompareChart(@RequestBody ChartGenerationRequest request, HttpSer

我正在尝试使用Javascript在新窗口中弹出PDF文件的字节流

在后端,我使用一个Spring控制器代码,如下所示

@RequestMapping(value = "/print", method = RequestMethod.POST, consumes="application/json")
public ModelAndView printCompareChart(@RequestBody ChartGenerationRequest request,
        HttpServletRequest httpRequest ) throws Exception {

   byte [] bytes =//bytestream of a pdf file
   ModelAndView mav = new ModelAndView();
   mav.addObject("byteArray", bytes);
   mav.setViewName("pdfByteArrayPrint");
   return mav;
 }
此post方法由JS的AJAX调用调用,如下所示

$.ajax({
    url: url,
    cache: false,
    type:'POST',
    data: JSON.stringify(data),
    contentType:"application/json; charset=UTF-8",
    success: function (responseData){
        var win=window.open('about:blank', target, windowProperties);
        with(win.document)
        {
          open();
          write(responseData);
          close();
        }
    }
});
从chrome开发者工具中,我可以看到响应数据是以字节的形式出现的,但是在新的浏览器窗口中,它并没有显示实际的pdf文件,而是显示字节本身

这是我得到的输出


如何在这里显示从字节流中提取的实际文件?

我根本无法实现这一点。所以我想出了一个替代方案。在JS端,我创建了一个隐藏表单并提交了数据,后端使用pdf ByTestStream进行响应,然后显示在新的浏览器窗口中。在本例中,我不得不牺牲自动json到java对象的转换,并从传递的httpRequest中分别处理每个参数

JS代码

openWithPost = function(url, title, data, target) {

  var form = document.createElement("form");
  form.action = url;
  form.method = "POST";
  form.target = target || "_self";
  if (data) {
    for ( var key in data) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = key;
        input.value = data[key];
        form.appendChild(input);
    }
  }
  form.style.display = 'none';
  document.body.appendChild(form);
  form.submit();
}
@RequestMapping(value = "/printCompareChart", method = RequestMethod.POST)
public ModelAndView printCompareChart(HttpServletRequest httpRequest)
{
   //get each parameter from httpRequest and handle it
   String fileName = httpRequest.getParameter("fileName");
   //logic to create the pdf file
   byte[] bytes = //get bytes of the pdf file
   ModelAndView mav = new ModelAndView();
   mav.addObject("byteArray", bytes);
   mav.setViewName("pdfByteArrayPrint");
   return mav;
}
后端Spring代码

openWithPost = function(url, title, data, target) {

  var form = document.createElement("form");
  form.action = url;
  form.method = "POST";
  form.target = target || "_self";
  if (data) {
    for ( var key in data) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = key;
        input.value = data[key];
        form.appendChild(input);
    }
  }
  form.style.display = 'none';
  document.body.appendChild(form);
  form.submit();
}
@RequestMapping(value = "/printCompareChart", method = RequestMethod.POST)
public ModelAndView printCompareChart(HttpServletRequest httpRequest)
{
   //get each parameter from httpRequest and handle it
   String fileName = httpRequest.getParameter("fileName");
   //logic to create the pdf file
   byte[] bytes = //get bytes of the pdf file
   ModelAndView mav = new ModelAndView();
   mav.addObject("byteArray", bytes);
   mav.setViewName("pdfByteArrayPrint");
   return mav;
}

我正在以stream
win的形式发送json数据并返回pdf。文档是一个HTML文档。你写什么并不重要。