使用flexpaper實現線上預覽功能時 ,遇到的flash快取問題的解放方案
本專案使用的是本地目錄對映為伺服器相對路徑,故預覽頁面時讀取的flash與專案不再同一碟符中,若更改flash檔案後,預覽還是隻顯示之前的舊檔案,一番搜尋後找到了解決方案,flexpaper 的官方demo中,有一個和java相關的demo,其中給出了一些方法,整理後使用如下:
原本flexpaper呼叫時:
$('#documentViewer').FlexPaperViewer( { config : { SWFFile : '<%=request.getContextPath()+"/"+swfFilePath%>',//伺服器中對映為本地目錄 Scale : 1, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : true, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', RenderingOrder : 'flash', StartAtPage : '', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, WMode : 'window', localeChain: 'en_US' }} );
但一直出現快取,解決方案:使用另一頁面用檔案流形式輸出flash byte陣列,在頁面上直接顯示,SWFFile 的值設為該頁面,檢視原始碼後發現,嵌入該屬性時需整個URL編碼,不然會無法顯示。
function showSwf(swf){ //console.log('show.jsp?swf='+encodeURI(swf)+'&temp='+Math.random()); $('#documentViewer').FlexPaperViewer( { config : { SWFFile : encodeURIComponent('show.jsp?swf='+swf+'&temp='+Math.random()), Scale : 1, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : false, FitWidthOnLoad : true, FullScreenAsMaxWindow : false, ProgressiveLoading : true, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', RenderingOrder : 'flash', StartAtPage : '', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, WMode : 'window', localeChain: 'en_US' }} ); }
顯示頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*" %> <%@ page import="com.wonders.attach.util.*" %> <%! public byte[] file_get_contents(String file) { byte[] con = {0}; if(file == null || file == "") return con; try { File f = new File(file); if(!f.isFile() || !f.canRead()) return con; FileInputStream fstream = new FileInputStream(file); con = new byte[(int) f.length()]; fstream.read(con); fstream.close(); } catch (Exception e) { e.printStackTrace(); } return con; } %> <% String path = FileUpProperties.getValueByKey("file_path"); String swf = (String)request.getParameter("swf"); if(swf != null && swf.length()>0){ swf = swf.replace("swf/",""); } //System.out.println(swf); response.reset();//如果在weblogic底下同樣要加上此句 BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/x-shockwave-flash"); response.setHeader("Accept-Ranges", "bytes"); byte[] content = file_get_contents(path+swf); response.setContentLength(content.length); outs.write(content); outs.flush(); outs.close(); // out.clear(); //out = pageContext.pushBody(); %>
即可正常顯示,解決快取問題。
附上demo中,各檔案格式輸出方式:
<%@ page import="lib.*,java.io.*" %>
<%
BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
common conf = new common();
String doc = request.getParameter("doc");
String pdfdoc = doc;
String pages = request.getParameter("page");
String format = request.getParameter("format");
String resolution = request.getParameter("resolution");
String callback = request.getParameter("callback");
if(doc == null)return;
if(!doc.endsWith(".pdf")) {pdfdoc = doc + ".pdf";}
if(pages == null) {pages = "";}
if(format == null) {format="swf";}
String swfdoc = pdfdoc + ".swf";
if("true".equals(conf.getConfig("splitmode", ""))) { swfdoc = pdfdoc + "_" + pages + ".swf";}
if(callback == null) {callback = "";}
String jsondoc = pdfdoc + ".js";
if("true".equals(conf.getConfig("splitmode", ""))) { jsondoc = pdfdoc + "_" + pages + ".js"; }
String pngdoc = pdfdoc + "_" + pages + ".png";
String jpgcachedoc = pdfdoc + "_" + pages + "_res_" + resolution + ".jpg";
String messages = "";
String swfFilePath = conf.separate(conf.getConfig("path.swf", "")) + swfdoc;
String pdfFilePath = conf.separate(conf.getConfig("path.pdf", "")) + pdfdoc;
String pngFilePath = conf.separate(conf.getConfig("path.swf", "")) + pngdoc;
String jpgCachePath = conf.separate(conf.getConfig("path.swf", "")) + jpgcachedoc;
String jsonFilePath = conf.separate(conf.getConfig("path.swf", "")) + jsondoc;
boolean validatedConfig = true;
String error = "";
if(!conf.is_dir(conf.getConfig("path.swf", ""))){
error = "Error:Cannot find SWF output directory, please check your configuration file.";
validatedConfig = false;
}
if(!conf.is_dir(conf.getConfig("path.pdf", ""))){
error += "Error:Cannot find PDF output directory, please check your configuration file.";
validatedConfig = false;
}
if(!validatedConfig){
error += "Error:Cannot read directories set up in configuration file, please check your configuration.";
}else if(!conf.validParams(pdfFilePath, pdfdoc, pages)){
error += "Error:Incorrect file specified, please check your path.";
}else{
if("swf".equals(format) || "jpg".equals(format) || "png".equals(format) || "pdf".equals(format)){
if(!conf.file_exists(swfFilePath)){
pdf2swf pdfconv = new pdf2swf(request);
messages = pdfconv.convert(pdfdoc, pages);
}
if("png".equals(format)||"jpg".equals(format)){
if(conf.validParams(swfFilePath, swfdoc, pages)){
if(!conf.file_exists(pngFilePath)){
swfrender pngconv = new swfrender();
pngconv.renderPage(pdfdoc, swfdoc, pages);
}
if("true".equals(conf.getConfig("allowcache", ""))){
conf.setCacheHeaders(response);
}
if(conf.getConfig("allowcache") == null || "false".equals(conf.getConfig("allowcache", "")) || ("true".equals(conf.getConfig("allowcache", "")) && conf.endOrRespond(request, response))){
if(resolution != null){
response.setContentType("image/jpeg");
outs.write(conf.file_get_contents(conf.generateImage(pngFilePath, jpgCachePath, resolution, "jpg")));
}else if("jpg".equals(format)){
response.setContentType("image/jpeg");
outs.write(conf.file_get_contents(conf.generateImage(pngFilePath, jpgCachePath, null, "jpg")));
}else{
response.setContentType("image/png");
outs.write(conf.file_get_contents(pngFilePath));
}
}
}else{
if(messages.length() == 0 || "[OK]".equals(messages))
messages = "[Incorrect file specified, please check your path]";
}
} else if("pdf".equals(format)){
response.setContentType("application/pdf");
outs.write(conf.file_get_contents(pdfFilePath));
}
if(conf.file_exists(swfFilePath)){
if("swf".equals(format)){
if("true".equals(conf.getConfig("allowcache", ""))){
conf.setCacheHeaders(response);
}
if(conf.getConfig("allowcache") == null || "false".equals(conf.getConfig("allowcache", "")) || ("true".equals(conf.getConfig("allowcache", "")) && conf.endOrRespond(request,response))){
response.setContentType("application/x-shockwave-flash");
response.setHeader("Accept-Ranges", "bytes");
byte[] content = conf.file_get_contents(swfFilePath);
response.setContentLength(content.length);
outs.write(content);
}
}
}else{
if(messages.length() == 0)
messages = "[Cannot find SWF file. Please check your JSP configuration]";
}
}else if("json".equals(format) || "jsonp".equals(format)){
if(!conf.file_exists(jsonFilePath)){
pdf2json jsonconv = new pdf2json();
messages = jsonconv.convert(pdfdoc, jsondoc, pages);
}
if(conf.file_exists(jsonFilePath)){
if("true".equals(conf.getConfig("allowcache", ""))){
conf.setCacheHeaders(response);
}
if(conf.getConfig("allowcache") == null || "false".equals(conf.getConfig("allowcache", "")) || ("true".equals(conf.getConfig("allowcache", "")) && conf.endOrRespond(request,response))){
response.setContentType("text/javascript");
if("json".equals(format)){
outs.write(conf.file_get_contents(jsonFilePath));
}
if("jsonp".equals(format)){
outs.write((callback + "(").getBytes());
outs.write(conf.file_get_contents(jsonFilePath));
outs.write((");").getBytes());
}
}
}else{
if(messages.length() == 0)
messages = "[Cannot find JSON file. Please check your JSP configuration]";
}
}
if(messages.length() > 0 && !"[OK]".equals(messages) && !"[Converted]".equals(messages) && !"png".equals(format)){
outs.write(("Error:" + messages.substring(1,messages.length()-2)).getBytes());
}
}
if(error.length() > 0)
outs.write(error.getBytes());
outs.flush();
outs.close();
out.clear();
out = pageContext.pushBody();
%>
相關推薦
使用flexpaper實現線上預覽功能時 ,遇到的flash快取問題的解放方案
本專案使用的是本地目錄對映為伺服器相對路徑,故預覽頁面時讀取的flash與專案不再同一碟符中,若更改flash檔案後,預覽還是隻顯示之前的舊檔案,一番搜尋後找到了解決方案,flexpaper 的官方demo中,有一個和java相關的demo,其中給出了一些方法,整理後使用如
JAVA Web專案中用OpenOffice+Swftools+Flexpaper實現線上預覽,txt檔案出現亂碼!
在幼兒園管理系統中,實現線上預覽功能。當上傳word、ppt、excel、pdf的時候,不會出現亂碼;當上傳txt檔案的時候(編碼除UTF-8之外),會出現亂碼。當時有兩個方案。 方案一: 在上傳txt檔案的時候,判斷其編碼是否為UTF-8,如果不是,提示框:提示使用者上傳
centos6.5下安裝openoffice+jodconverter+swftool+flexpaper工具實現線上預覽文件功能
作用:linux下文件伺服器上傳文件轉換成pdf文件,再由swftool工具轉換成swf檔案實現線上預覽 環境:OS centos6.5 java環境 軟體包:Apac
Extjs上傳附件實戰開發,實現批量上傳及線上預覽功能(二)
SWFUpload的使用: SWFUpload採用czpae86的UploadPanel二次開發,在此鳴謝。 SWFUpload下載最新版本swfupload.swf.v2.5.0.beta3.2.zip,你會發現資料夾裡只有swfuploa
java 使用openoffice 轉換文件,成.pdf,實現線上預覽效果
1. 下載 openoffice 地址 https://pan.baidu.com/s/1dfpoG6zlawoW1pqpDvBL0A 密碼: v4ej 如果上面的地址無法訪問請訪問這個地址:下載地址如下:http://www.openof
.net mvc使用FlexPaper外掛實現線上預覽PDF,EXCEL,WORD的方法
FlexPaper外掛可以實現在瀏覽器中線上預覽pdf,word,excel等。 在網上看到很多關於這個外掛實現預覽的技術,但是很難做到word和excel線上預覽。 pdf很好實現。 首先下載相關的外掛資訊,這裡不多說了。 其中這個外掛主要需要配合As
圖片上傳時實現本地預覽功能的原理解析
前言 最近在專案上加一個圖片裁剪上傳的功能,用的是cropper外掛,注意到選擇本地圖片後就會有預覽效果,這裡整理一下這種預覽效果的實現原理; 實現原理 通過input的 type = file屬性和window的內建FileReader物件,利用
js+java 實現圖片線上預覽功能
本部落格主要描述如何用JavaScript+java實現圖片的預覽功能,其實要點還是需要服務端的流。 (一)功能描述 點選頁面的 【預覽】 檢視已經上傳的或者在伺服器中存在的圖片。 (二)實現原理 使用img標籤的src屬性來渲染資料,但是,sr
springboot搭建檔案預覽解決方案,支援目前主流格式office檔案,txt檔案,png,jpg等圖片以及壓縮檔案的線上預覽功能
前言 這些天在做一個檔案預覽的功能,由於之前沒有這方面的經驗,因此在市面上找了一些這方面的資料,發現目前市面上主流的檔案預覽服務是由幾家廠商提供的,做的比較好的有如永中軟體,officeweb365等,不過你們都懂得,是要收費的,所以即便做的再好,我也只能觀望觀望,然後也百
java實現線上預覽--poi實現word、excel、ppt轉html
分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow 也歡迎轉載我的文章,轉載請註明出處 https://blog.csdn.net/aabbyyz java實現線上預覽 - -之poi實現word、e
ionic3專案實現線上預覽PDF檔案
這裡參考了大牛提供的預覽外掛完成自己需要實現的功能,ng2-pdf-viewer,該外掛不支援ionic3的懶載入,廢話少說,直接擼程式碼。 第一步,安裝 ng2-pdf-viewer npm install ng2-pdf-viewer --save 第二步,在專案中新建頁面
Java實現線上預覽--openOffice實現
Java實現線上預覽–openOffice實現 簡介 之前有寫了poi實現線上預覽的文章,裡面也說到了使用openOffice也可以做到,這裡就詳細介紹一下。 我的實現邏輯有兩種: 一、利用jodconverter(基於OpenOffice服務)將檔案(.doc、.docx、.xls、.pp
前端實現線上預覽pdf、word、xls、ppt等檔案
1、前端實現pdf檔案線上預覽功能 方式一: 通過a標籤href屬性實現 pdf檔案理論上可以在瀏覽器直接開啟預覽但是需要開啟新頁面。在僅僅是預覽pdf檔案且UI要求不高的情況下可以直接通過a標籤href屬性實現預覽 <a href="文件地址"></
怎麼簡便地去掉html中難看的檔案上傳按鈕並實現圖片預覽功能?
問題描述 通常的檔案上傳按鈕是這樣的: 選擇了檔案過後是這樣的: 很顯然,這樣的按鈕並不好看。 解決方法 用一個label標籤來裝載樣式,其for屬性指向type=file的inp
Word轉html實現線上預覽
word轉html,可以同時支援doc和docx兩種格式,非常好用 開發工具:idea 專案管理工具:maven 不多說,直接擼程式碼 1、首先配置pom.xml檔案,具體配置如下 2、工具類的開發 /** * WORD轉HTML docx格式 * POI版本: 3.10-
Asp.net MVC 利用(aspose+pdfobject.js) 實現線上預覽word、excel、ppt、pdf檔案
線上預覽word、excel、ppt利用aspose動態生成html 主要程式碼 private bool OfficeDocumentToHtml(string sourceDoc, string saveDoc) { bool result = false;
Java實現線上預覽–openOffice實現
Java實現線上預覽–openOffice實現 簡介 之前有寫了poi實現線上預覽的文章,裡面也說到了使用openOffice也可以做到,這裡就詳細介紹一下。 我的實現邏輯有兩種: 一、利用jodconverter(基於OpenOffice服務)將檔案(
通過Aspose對Word,Excel檔案進行Pdf轉換,實現線上預覽
解決思路:1.利用AsposeCells,AsposeWords相關Jar包提供的轉換功能,將Excel及Word型別文件轉換為Pdf檔案,並存於當前專案目錄下2.通過瀏覽器的iframe標籤功能,直接訪問應用下的相關Pdf檔案,目前主流瀏覽器均支援直接在頁面上瀏覽Pdf檔案
java實現線上預覽--poi實現word excel ppt轉html
java實現線上
實現線上預覽PDF的幾種解決方案
因客戶需要實現PDF的預覽處理,在網上找了一些PDF線上預覽的解決方案,有的用PDFJS的線上預覽方式,有的使用PDFObject的嵌入式顯示,有的通過轉換JPG/PNG方式實現間接顯示的方式,開始是想通過簡單的方式,能夠使用JS外掛實現預覽最好,可是線上預覽總是有一些不足,如不同瀏覽器的相容問題,甚至不同的