700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > PDF预览 支持ie 谷歌等主流浏览器

PDF预览 支持ie 谷歌等主流浏览器

时间:2020-07-19 04:17:46

相关推荐

PDF预览 支持ie 谷歌等主流浏览器

PDFJS:详见本人资源列表(高版本pdfjs不支持低版本的ie)

将空间放在项目静态文件的根目录下,要不然访问服务端方法时404;

页面调用方式:

本人这个是通过后台返回文件流进行处理,所以需要调用服务端接口;

服务端方法:获取文件是从另一台服务器上请求,返回的是FileBase64的文件流;

/*** 展示pdf* @param id* @param response* @throws UnsupportedEncodingException*/@RequestMapping(value = "showSwfV1-{id}")public void showSwfUrlV1(@PathVariable("id") String id,HttpServletRequest request,HttpServletResponse response, ModelMap model) throws UnsupportedEncodingException {String getfileDataUrl= WSUtils.getProperty("dataOpenServiceUrl")+"/creditPromiseViewSwf";Map<String,String> param=new HashMap<String,String>();param.put("id",id);String data = "";try {//调用接口 接口文件的base64的文件流data=ApacheHttpUtils.postForm(getfileDataUrl,param);} catch (Exception e) {e.printStackTrace();}OutputStream outputStream=null;try {outputStream = response.getOutputStream();//将base64的文件流转换为输出流GenerateFile(data,outputStream);} catch (IOException e) {e.printStackTrace();}finally {IOUtils.closeQuietly(outputStream);}}//将base64的文件流转换为输出流public boolean GenerateFile(String data, OutputStream out) {if (data == null){return false;}BASE64Decoder decoder = new BASE64Decoder();try {// Base64解码byte[] b = decoder.decodeBuffer(data);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}out.write(b);out.flush();out.close();return true;} catch (Exception e) {return false;}}

接口提供方:

FileToBase64Utils工具类

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class FileToBase64Utils {public FileToBase64Utils() {}public static String encodeBase64File(String path) throws Exception {File file = new File(path);FileInputStream inputFile = new FileInputStream(file);byte[] buffer = new byte[(int)file.length()];inputFile.read(buffer);inputFile.close();return (new BASE64Encoder()).encode(buffer);}public static void decoderBase64File(String base64Code, String targetPath) throws Exception {byte[] buffer = (new BASE64Decoder()).decodeBuffer(base64Code);FileOutputStream out = new FileOutputStream(targetPath);out.write(buffer);out.close();}public static void toFile(String base64Code, String targetPath) throws Exception {byte[] buffer = base64Code.getBytes();FileOutputStream out = new FileOutputStream(targetPath);out.write(buffer);out.close();}public static void main(String[] args) {try {String base64Code = encodeBase64File("/Users/Crazy/Pictures/zyb2.jpg");System.out.println(base64Code);decoderBase64File(base64Code, "/Users/Crazy/Desktop/zyb.png");toFile(base64Code, "/Users/Crazy/Desktop/zyb.txt");} catch (Exception var2) {var2.printStackTrace();}}}

页面需要一个下载功能,顺手记录下吧:

/*** 文件下载 调用外部接口 直接下载* @param request* @param response* @param model* @return*/@RequestMapping(value = "/CreditPromiseDownLoadV1.jspx")public void CreditPromiseDownLoadV1(@RequestParam String id,@RequestParam String fileName,HttpServletRequest request, HttpServletResponse response,ModelMap model) {try {fileName=URLDecoder.decode(fileName,"utf-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}String getfileDataUrl= WSUtils.getProperty("dataOpenServiceUrl")+"/creditPromiseViewSwf";Map<String,String> param=new HashMap<String,String>();param.put("id",id);String data = "";try {//调用接口 接口文件的base64的文件流data=ApacheHttpUtils.postForm(getfileDataUrl,param);} catch (Exception e) {e.printStackTrace();}OutputStream os = null;InputStream is = null;try {os = response.getOutputStream();response.reset();response.addHeader("Content-Disposition", "attachment; filename=\""+ new String(fileName.getBytes("gbk"),"iso-8859-1")+"\"");response.setContentType("application/octet-stream; charset=utf-8");GenerateFile(data,os);} catch (Exception e) {e.printStackTrace();}finally{IOUtils.closeQuietly(is);IOUtils.closeQuietly(os);}}/*** 文件下载本地文件下载** @param request* @param response* @param model* @return*/@RequestMapping(value = "/CreditPromiseDownLoad.jspx")public void CreditPromiseDownLoad(@RequestParam String filePath,@RequestParam String fileName,HttpServletRequest request, HttpServletResponse response,ModelMap model) {try {fileName=URLDecoder.decode(fileName,"utf-8");filePath=URLDecoder.decode(filePath,"utf-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}OutputStream os = null;InputStream is = null;ZipOutputStream zos = null;try {os = response.getOutputStream();response.reset();String promiseFile = WSUtils.getProperty("promiseFile");;File file = FileUtils.getFile("G:\\基础资料-西部资信\\学习资料\\Java并发编程实战.pdf");OutputStream out = response.getOutputStream();is = new FileInputStream(file);response.addHeader("Content-Disposition", "attachment; filename=\""+ new String(fileName.getBytes("gbk"),"iso-8859-1")+"\"");response.setContentType("application/octet-stream; charset=utf-8");IOUtils.copy(is, os);} catch (Exception e) {e.printStackTrace();}finally{IOUtils.closeQuietly(is);IOUtils.closeQuietly(os);IOUtils.closeQuietly(zos);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。