Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以可保存附件的形式返回ModelAndView Spring MVC PDF对象_Java_Spring_Spring Mvc_Pdf - Fatal编程技术网

Java 以可保存附件的形式返回ModelAndView Spring MVC PDF对象

Java 以可保存附件的形式返回ModelAndView Spring MVC PDF对象,java,spring,spring-mvc,pdf,Java,Spring,Spring Mvc,Pdf,我没有太多的Spring MVC经验。我继承了一些RESTful控制器代码,它在数据库中查找记录并使用 org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView to create a PDF view of the record. @RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET) @ResponseB

我没有太多的Spring MVC经验。我继承了一些RESTful控制器代码,它在数据库中查找记录并使用

org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView to create a PDF view of the record.
@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
方法如下:

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
不走运。我还尝试添加内容类型:

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
response.setContentType("application/pdf");
还是不走运

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}

我需要学习如何将ModelAndView对象作为文件附件发回,或者将其转换为字节流并手动将其流回??这里没有线索。任何想法都将不胜感激。

您需要做的是将PDF文件的字节直接流式传输到输出流并刷新响应。在春天,你可以这样做:

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
@RequestMapping(value="/displayProcessFile/{processInstanceId}", method=RequestMethod.GET)
public ResponseEntity<byte[]> displayProcessFile(@PathVariable String processInstanceId) throws UnauthorizedUserAccessException{
    Document processFile=null;
    try {
        processFile = documentService.retrieveProcessFile(Long.parseLong(processInstanceId));
    } catch (ProcessFileNotFoundExpection e) {
        e.printStackTrace();
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.add("content-disposition", "inline;filename=" + processFile.getDocName());
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(processFile.getContent(), headers, HttpStatus.OK);
    return response;
}
@RequestMapping(value=“/displayProcessFile/{processInstanceId}”,method=RequestMethod.GET)
public ResponseEntity displayProcessFile(@PathVariable String processInstanceId)引发未经授权的DuserAccess异常{
文档processFile=null;
试一试{
processFile=documentService.retrieveProcessFile(Long.parseLong(processInstanceId));
}捕获(ProcessFileNotFoundExpection e){
e、 printStackTrace();
}
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.parseMediaType(“application/pdf”);
headers.add(“内容处置”、“内联;文件名=“+processFile.getDocName());
ResponseEntity response=newresponseEntity(processFile.getContent(),headers,HttpStatus.OK);
返回响应;
}

也许您必须更改浏览器设置,以便它在每次直接打开文件之前都会询问

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
请找到以下链接:

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
此外,您还必须设置:

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
response.setContentType(“应用程序/下载”)

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
setHeader(“内容处置”、“附件;文件名=profile.pdf”)

默认情况下,将
内容配置
设置为
内联
,但前提是标题未预配置为其他内容

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
JasperReportsPdfView
有一个名为
headers
的属性,您可以在其中配置或覆盖要随生成的PDF一起发送的头。如果您想在此中包含
内容处置
标题,您可以将其设置为其他内容,例如
附件

@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}
<bean id="yourreport" class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView">
    <property name="headers">
        <map>
            <entry key="Content-Disposition" value="attachment; profile.pdf" />
        </map>
    <property>
</bean>


类似这样的东西应该可以做到这一点(您也可以使用
props
元素,而不是
map

是的,但问题仍然是,如何将ModelAndView对象转换为字节数组?该对象上似乎没有任何方法可以帮助我。这是我们的回退选项,我不想让3000名最终用户更改他们的浏览器设置。:)将
JasperReportsPdfView
配置为使用正确的头应该可以做到这一点。好的,我来看看。Brb.视图有一个名为
headers
的属性,默认情况下
内容配置
设置为
内联
。通过在配置中提供硬编码,您可以将其切换到附件。缺点是您不能指定文档的名称,因为它也是静态的。就是这样!非常感谢你!我只是简单地添加了一个属性对象,其中有一个条目:“Content Disposition”,“attachment;filename=profile.pdf”,然后将该属性对象添加到带有setHeaders(属性)的JasperReportPdfView中,它会弹出保存/打开窗口!!!非常感谢你。如果你回答我,我会接受你的回答谢谢戴纳姆,这就成功了。我添加了一个属性对象,其中有一个条目:“Content Disposition”、“attachment;filename=profile.pdf”,然后使用setHeaders(属性)将该属性对象添加到JasperReportPdfView对象中,现在对web服务的调用会弹出保存/打开窗口。再次感谢!
@RequestMapping(value = "/profile/{uniqueId}.pdf", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getProfilePdf(HttpServletRequest request, @PathVariable("uniqueId") String uniqueId {
    Profile profile = getProfileByUniqueId(request, uniqueId);
    Map<String, Object> reportData = buildReportData(profile);
    return new ModelAndView(profileReport, reportData);
}