前端使用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

技术
今日推荐
PPT
阅读数 131
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信