在线预览文件
如果不是pdf格式,可以通过第三方工具OpenOffice转换为PDF。具体可以查看我这篇文章 使用Java代码将word、execl、ppt文件转换为pdf格式
一、jsp预览pdf
1、后台代码
/**
* @param path 文件路径
*/
@RequestMapping("/viewPdf")
public String viewPdf(ModelMap model, String path){
model.addAttribute("path",path);
return "/WEB-INF/pdf";
}
2、前台代码
<%@ page language="java" import="java.util.*,java.io.*,util.openoffice.OpenOfficeUtil" pageEncoding="UTF-8"%>
<html>
<body>
<%
String path = request.getAttribute("path").toString();
%>
<%
out.clear();
out = pageContext.pushBody();
response.setContentType("application/pdf");
try {
//判断该路径下的文件是否存在
File file = new File(path);
int lastIndex = file.getName().lastIndexOf(".")+1;
String type = file.getName().substring(lastIndex, file.getName().length());
if (file.exists()) {
if(!"pdf".equals(type)){
// 通过openoffice转为pdf文件
FileInputStream fileInputStream = new FileInputStream(file);
file = OpenOfficeUtil.fileToPdf(file, fileInputStream, "D:/OpenOfficeFile", type);
}
DataOutputStream temps = new DataOutputStream(response.getOutputStream());
DataInputStre