pdf在线预览功能 非插件 后端响应方式

16 篇文章 0 订阅

vue可能形式不同 这里只记录js的

如果附件下载功能完成的话  只需获取文件流即可

/**
* 预览pdf
* @param id
* @param res
* @param request
* @return
*/
@RequestMapping("/pdfView/{id}")
public String previewFile(@PathVariable("id") String id, HttpServletResponse res, HttpServletRequest request) {
File file = fileService.get(id);
if(file == null){
return ("文件不存在");
}
String fullPath = FileUtil.UPLOAD_FILE_PATH + file.getUrl();
java.io.File downloadPath = new java.io.File(fullPath);
String fileName = file.getFileName();
try {
res.setContentType("application/pdf");
final String userAgent = request.getHeader("USER-AGENT");
if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
fileName = URLEncoder.encode(fileName,"UTF-8");
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
fileName = new String(fileName.getBytes(), "ISO8859-1");
}else{
fileName = URLEncoder.encode(fileName,"UTF-8");//其他浏览器
}
res.reset();// 非常重要
res.setHeader("Content-Disposition", "inline; filename=" + fileName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
//file = null;
try {
os = res.getOutputStream();
//file = new File(ConversionFactoryUtil.rootPath() + filePath);
if (file != null) {
bis = new BufferedInputStream(new FileInputStream(fullPath));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, i);
os.flush();
i = bis.read(buff);
}
return "success";
} else {
return "file not local exists ...";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
os.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

 后台接口接收文件id

通过fileService来查找文件

同时利用FileUtil工具获取完整路径,完整的文件名字

设置各个浏览器的解析方式

 

设置流 outputStream  以及fileinputStream读取完整文件路径

直接cv代码 更改部分代码即可达到效果

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值