在网页中显示PDF [英] Display PDF in a webpage

查看:76
本文介绍了在网页中显示PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC,我想从本地到网页显示PDF文件.我不知道该对控制器执行什么操作.我看到一个类似的问题,答案是返回ResponseEntity<byte[]>,例如问题,但这是给ajax的,如果我没记错的话,这不是我的要求.

I'm using Spring MVC, I want to display PDF file from my local to webpage. I don't know what should I do to my controller to do this. I see some similar problem with an answer that returning a ResponseEntity<byte[]> like in this question but this one is for ajax if I'm not mistaken, this is not my requirement.

更新:

这是我到目前为止尝试过的:

This is what I tried so far:

<a href="#" onClick="test();" >test</a>

function test(){
      $.ajax({
    type: "POST",
    url: "../admin/module/id.do",
    data: '{ file_id }',
    success: function(response){
           alert(response);
             }
      });
}

和控制器:

@RequestMapping( value = "/admin/module/id", method = RequestMethod.POST )
    public ResponseEntity<byte[]> getPDF( @PathVariable( "id" )
    int id, Model model )
    {
        System.out.println( "test" );
        Path path = Paths.get( "C:/Users/FORSAK~1/AppData/Local/Temp/spring_tutorial.pdf" );
        byte[] contents = null;
        try
        {
            contents = Files.readAllBytes( path );
        }
        catch( IOException e )
        {

            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType( MediaType.parseMediaType( "application/pdf" ) );
        String filename = "spring_tutorial.pdf";
        headers.setContentDispositionFormData( filename, filename );
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>( contents, headers, HttpStatus.OK );
        return response;
    }

alert(response)不起作用,System.out.println( "test" );

萤火虫的错误是"NetworkError: 500 Internal Server Error - http://localhost:8080/ThesisProject/admin/module/id.do"

Stacktrace:

Stacktrace:

java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:857)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:710)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:360)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
    ....

推荐答案

首先,例外非常简单

java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping

你有

@RequestMapping( value = "/admin/module/id", method = RequestMethod.POST )

不声明路径变量,仅声明特定路径. id作为请求参数传递

which does not declare a path variable, only a specific path. The id is passed as a request parameter

data: '{ file_id }',

这是错误的,我认为应该是

This is wrong by the way, I think it should be

data: { id: file_id },

改为使用@RequestParam(value = "id")注释参数.

这篇关于在网页中显示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆