Spring MVC开放PDF无效

时间:2017-03-06 08:27:33

标签: spring-mvc

尝试按照另一个答案中的建议在Chrome中打开PDF。但是,我得到的是二进制表示(见图)。 Content-disposition =附件有效,但内联不起作用。我做错了什么?

结果:

enter image description here

Javaconfig(我认为这是默认启用的,但添加或删除此代码并没有什么区别):

public class WebConfig extends WebMvcConfigurationSupport {
    ...

    @Bean
    public ResourceHttpMessageConverter httpConverter() {
        return new ResourceHttpMessageConverter();
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(httpConverter());
        super.configureMessageConverters(converters);
    }
}

控制器:

@RequestMapping(value="/man", method = RequestMethod.GET)
@ResponseBody
public FileSystemResource manual(HttpServletResponse response) {
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", "inline; filename=manual.pdf");
    return new FileSystemResource(servletContext.getRealPath("/resources/manual.pdf"));
}   

1 个答案:

答案 0 :(得分:1)

您需要定义produces

@RequestMapping(value="/man", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)