实现在线预览

var url = "one/readontheweb.jsp?unid="+unid;

window.location = encodeURI(url);

功能:在线预览

String htmlUrl="";

try{

String unid = request.getParameter("unid");

AppFile appFile = new AppFileManager(JNDI).doFindBeanByKey(unid);

appFile = null == appFile ? new AppFile() : appFile;

htmlUrl= AHSZFileToHtmlTools.getUrlByFile(appFile);

}catch(Exception e){

e.printStackTrace();

}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>首页</title>

<style type="text/css">

.boxContent{height: 100%;width:auto;min-width:600px;_width:expression(documentElement.clientWidth >1000? documentElement.clientWidth:1000);}

#MainWrap{padding: 10px;text-align: center;}

</style>

<script type="text/javascript" src="${corejs}/jquery.js"></script>

<script type="text/javascript">

function reinitIframe(){  

var iframe = document.getElementById("mainFrm");  

        try{  

            var bHeight = iframe.contentWindow.document.body.scrollHeight;  

            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

            var height = Math.min(bHeight, dHeight);  

            iframe.height =  height;  

        }catch (ex){}  

    }  

    //window.setInterval("reinitIframe()", 500);

</script>

</head>

<body>

<div class="boxContent">

<div id="MainWrap" style="height:2100px;">

<iframe id="mainFrm" src="${path }<%=htmlUrl %>" 

scrolling="no" marginheight="0" marginwidth="0" frameborder="0" 

width="100%" height="100%" name="mainFrm" onload="this.height=100%" 

style="background-color:transparent">

</iframe>

</div>

</div>

</body>

</html>

<script type="text/javascript">

    /**$(function(){

    //reinitIframe();

    });*/

</script>

public class AHSZFileToHtmlTools {

public static final int WORD_HTML = 8;

public static final int EXCEL_HTML = 44;

public static String getUrlByFile(AppFile appFile) {

String fileExt = appFile.getFile_ext();

String defaultUrl = "other.jsp?unid=" + appFile.getFile_unid();

if ("doc".equalsIgnoreCase(fileExt) 

|| "docx".equalsIgnoreCase(fileExt)

|| "xls".equalsIgnoreCase(fileExt)

|| "xlsx".equalsIgnoreCase(fileExt)) {

StringBuffer htmlPath = new StringBuffer();

htmlPath.append(getWebAppAddress()).append("was/upload/generate/");

if (!new File(htmlPath.toString()).exists()) {

FileUtil.mkdirs(htmlPath.toString());

}

htmlPath.append(appFile.getFile_unid()).append(".html");

if (!new File(htmlPath.toString()).exists()) {

String srcPath = getWebAppAddress() + appFile.getFile_path();

srcPath = srcPath.replace("//", "/");

srcPath = srcPath.replaceAll(" ", "%20");

byte2File(BlobUtil.blobToBytes(appFile.getFile_data()), srcPath);

try {

if ("doc".equalsIgnoreCase(fileExt)

|| "docx".equalsIgnoreCase(fileExt)) {

wordToHtml(srcPath, htmlPath.toString());

} else {

excelToHtml(srcPath, htmlPath.toString());

}

//new AppFileManager(GlobalParameter.APP_WAS).doUpdate(appFile);

} catch (Exception e) {

e.printStackTrace();

return defaultUrl;

}

}

return "/was/upload/generate/" + appFile.getFile_unid() + ".html";

}

return defaultUrl;

}

/**

* 文档转换函数

* @param docfile

*            word文档的绝对路径加文件名(包含扩展名)

* @param htmlfile

*            转换后的html文件绝对路径和文件名(不含扩展名)

* @throws Exception

*/

public static void wordToHtml(String docfile, String htmlfile) throws Exception {

// 初始化com的线程

ComThread.InitSTA();

ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word

try {

app.setProperty("Visible", new Variant(false));

// 设置word不可见

Dispatch docs = app.getProperty("Documents").toDispatch();

Dispatch doc = Dispatch.invoke(

docs,

"Open",

Dispatch.Method,

new Object[] { docfile, new Variant(false),

new Variant(true) }, new int[1]).toDispatch();

// 打开word文件

Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {

htmlfile, new Variant(WORD_HTML) }, new int[1]);

// 作为html格式保存到临时文件

Variant f = new Variant(false);

Dispatch.call(doc, "Close", f);

} catch (Exception e) {

throw e;

} finally {

app.invoke("Quit", new Variant[] {});

// 释放在程序线程中引用的其它com,比如Adobe PDFDistiller

ComThread.Release();

}

}

/**

* EXCEL转HTML

* @param xlsfile

*            EXCEL文件全路径

* @param htmlfile

*            转换后HTML存放路径

* @throws Exception

*/

public static void excelToHtml(String excelfile, String htmlfile) throws Exception {

// 初始化com的线程

ComThread.InitSTA();

// 启动excel

ActiveXComponent app = new ActiveXComponent("Excel.Application");

try {

// 设置excel不可见

app.setProperty("Visible", new Variant(false));

Dispatch excels = app.getProperty("Workbooks").toDispatch();

// 打开excel文件

Dispatch excel = Dispatch.invoke(

excels,

"Open",

Dispatch.Method,

new Object[] { excelfile, new Variant(false),

new Variant(true) }, new int[1]).toDispatch();

// 作为html格式保存到临时文件

Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {

htmlfile, new Variant(EXCEL_HTML) }, new int[1]);

Variant f = new Variant(false);

Dispatch.call(excel, "Close", f);

} catch (Exception e) {

throw e;

} finally {

app.invoke("Quit", new Variant[] {});

// 释放在程序线程中引用的其它com,比如Adobe PDFDistiller

ComThread.Release();

}

}

/**

* 获取系统的应用路径(路径在应用下)

* @return

*/

private static String getWebAppAddress() {

Class theClass = AHSZFileToHtmlTools.class;

java.net.URL u = theClass.getResource("");

// str会得到这个函数所在类的路径

String str = u.toString();

// 截去一些前面6个无用的字符

str = str.substring(6, str.length());

// 将%20换成空格(如果文件夹的名称带有空格的话,会在取得的字符串上变成%20)

str = str.replaceAll("%20", " ");

// 查找“WEB-INF”在该字符串的位置

int num = str.indexOf("WEB-INF");

// 截取即可

str = str.substring(0, num);

if (System.getProperty("os.name").toUpperCase().indexOf("LINUX") >= 0) {

str = "/" + str;

}

return str;

}

public static void byte2File(byte[] buf, String filePath) {

BufferedOutputStream bos = null;

FileOutputStream fos = null;

File file = null;

try {

File dir = new File(filePath);

if (!dir.exists() && dir.isDirectory()) {

dir.mkdirs();

}

file = new File(filePath);

fos = new FileOutputStream(file);

bos = new BufferedOutputStream(fos);

bos.write(buf);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bos != null) {

try {

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

猜你喜欢

转载自nidexuanzecjy-2014.iteye.com/blog/2292899