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代码 更改部分代码即可达到效果