前端使用window.open即可
var url="file/preview.do?path="+response.path+"&fileName="+response.name;
top.window.open(url,response.name,"_blank");
接口代码如下文章来源:https://www.toymoban.com/news/detail-562355.html
@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文章来源地址https://www.toymoban.com/news/detail-562355.html
到了这里,关于不使用插件预览pdf等类型文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!