不使用插件预览pdf等类型文件
前端使用window.open即可
var url="file/preview.do?path="+response.path+"&fileName="+response.name;
top.window.open(url,response.name,"_blank");
接口代码如下
@RequestMapping(value = "/file/preview.do")
public @ResponseBody String preview(HttpServletRequest request, HttpServletResponse resp, String path, String fileName) throws IllegalStateException, IOException
{
InputStream inputStream = xxxx;//把需要预览的文件转成文件流
OutputStream outputStream = resp.getOutputStream();
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
//把doc文档转pdf
if (fileSuffix.matches("doc|docx"))
{
// spire.doc.free
Document document = new Document();
document.loadFromStream(inputStream);
document.saveToStream(outputStream, FileFormat.PDF);
}
//把excel转pdf
else if (fileSuffix.matches("xls|xlsx"))
{
//spire.xls.free
Workbook workbook = new Workbook();
workbook.loadFromStream(inputStream);
workbook.saveToStream(outputStream, FileFormat.PDF);
}
else
{
if (!fileSuffix.matches("jpg|jpeg|png|gif|bmp|tiff|ai|cdr|eps|pdf"))
{
resp.setContentType("application/octet-stream;charset=UTF-8");
resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
}
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0)
{
outputStream.write(b, 0, length);
}
}
outputStream.flush();
outputStream.close();
inputStream.close();
return null;
}
如果需要把doc文档或者excel转为pdf,然后再进行预览的话需要引入spire.doc.free或者spire.xls.free的jar