使用pdf.js预览实现读取服务器外部文件

[size=large]不知道大家使用百度网盘的文件预览功能,f12看过控制台没有。[/size]


[img]http://dl2.iteye.com/upload/attachment/0129/3898/e91f2083-958b-32de-b81f-b1db0a194913.png[/img]

[size=x-large]发现百度网盘使用的预览文件功能全是基于开源pdf .js的
[/size]

[color=red][size=x-large]接下来正题,我们在使用pdf.js默认是读取发布容器内部的文件,读取外部的文件需要自己实现,接下来拿读取桌面文件作为例子来展示。
[/size][/color]


[color=red][size=x-large]实现原理:返回一个外部流文件给pdf.js实现加载预览文件。[/size][/color]


[color=red][size=x-large]步骤一:把pdf.js中的view.js中的改为DEFAULT_URL路径改为下载接口即可[/size][/color]

[img]http://dl2.iteye.com/upload/attachment/0129/3906/2f87d26c-ae17-3931-89b6-d4594bcac326.png[/img]


[color=red][size=x-large]效果:[/size][/color]

[color=red][size=x-large]步骤二:后端实现,这里后端是采用jersey,springmvc也是一样的原理[/size][/color]


	@GET
@Path("/d")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download(@QueryParam("filemd5") String viFileMd5,@QueryParam("filename") String viFileName,@QueryParam("fileid") String viFileId,@Context HttpServletResponse response,@Context HttpServletRequest request) {
File nFile = null;
String nFileName = null;
try {
nFile = new File("C:\\Users\\Administrator\\Desktop\\test.pdf");
nFileName = URLEncoder.encode("大数据", "UTF-8");
response.setCharacterEncoding("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return Response.ok(nFile).header("Content-disposition","attachment;filename=" + nFileName+ ";filename*=utf-8''" + nFileName).header("Cache-Control", "no-cache").build();
}


[img]http://dl2.iteye.com/upload/attachment/0129/3914/9cbd3b31-6538-3ec6-9871-4d2857114a69.png[/img]

猜你喜欢

转载自blog.csdn.net/iteye_14584/article/details/81243433
今日推荐