前端使用window.open即可
var url="file/preview.do?path="+response.path+"&fileName="+response.name;
top.window.open(url,response.name,"_blank");
接口代碼如下文章來源:http://www.zghlxwxcb.cn/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;//把需要預(yù)覽的文件轉(zhuǎn)成文件流
OutputStream outputStream = resp.getOutputStream();
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
//把doc文檔轉(zhuǎn)pdf
if (fileSuffix.matches("doc|docx"))
{
// spire.doc.free
Document document = new Document();
document.loadFromStream(inputStream);
document.saveToStream(outputStream, FileFormat.PDF);
}
//把excel轉(zhuǎn)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轉(zhuǎn)為pdf,然后再進行預(yù)覽的話需要引入spire.doc.free或者spire.xls.free的jar文章來源地址http://www.zghlxwxcb.cn/news/detail-562355.html
到了這里,關(guān)于不使用插件預(yù)覽pdf等類型文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!