Google Docs Viewer+ springBoot 预览示列
时间: 2023-12-11 16:04:21 浏览: 42
很好,以下是一个使用Google Docs Viewer预览PDF文件的示例代码:
```java
@Controller
public class PreviewController {
@RequestMapping(value = "/preview", method = RequestMethod.GET)
public ModelAndView previewDocument(@RequestParam("url") String documentUrl) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("preview");
//构建Google Docs Viewer预览链接
String googleDocsUrl = "https://docs.google.com/viewer?url=" + documentUrl;
modelAndView.addObject("googleDocsUrl", googleDocsUrl);
return modelAndView;
}
}
```
在上面的代码中,我们使用了Spring Boot框架中的`@Controller`注解来定义一个控制器类。其中`@RequestMapping`注解指定了该控制器类的请求映射路径为`/preview`,请求方式为`GET`。`@RequestParam`注解则用于获取请求中的URL参数`url`,即要预览的PDF文件的URL地址。
然后,我们构建了一个以Google Docs Viewer为基础的预览链接,并将其作为模型对象添加到视图中。最后,我们返回一个含有预览链接的视图名称为`preview`的`ModelAndView`对象。
在`preview`视图中,我们只需要使用一个iframe标签来嵌入Google Docs Viewer预览页面即可:
```html
<!DOCTYPE html>
<html>
<head>
<title>PDF预览</title>
</head>
<body>
<iframe src="${googleDocsUrl}" width="100%" height="100%"></iframe>
</body>
</html>
```
这样,当用户访问`/preview?url=<PDF文件的URL>`地址时,就能够通过Google Docs Viewer预览PDF文件了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![crx](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)