<%String dm = request.getParameter("dm");String id = request.getParameter("id");" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > <em>jsp</em>页面内显示<em>word</em>文档原内容-

<em>jsp</em>页面内显示<em>word</em>文档原内容-

作者:乔山办公网日期:

返回目录:word文档


jsp代码如下:
<%@page contentType="applicationnd.ms-excel;charset=gb2312" %>
<%
String dm = request.getParameter("dm");
String id = request.getParameter("id");
%>
<jsp:useBean id="r" class="dx.MyDoc" scope="page"/>
<%
ServletOutputStream sout = response.getOutputStream();
java.io.InputStream in = null;
if(dm!=null)
in = r.fj(dm,"fj");
else
in = r.fj2(id);
byte b[] = new byte[0x7a120];// 创建byte数组用作缓冲
while (in.read(b) != -1)
{
sout.write(b); //输出字符流
}
sout.flush();
sout.close();
%>
MyDoc.fj(),流的形式读入word文件即可,ie可以直接打开,其它浏览器提示下载,下载后是word 文件。

1、利用jacob包将用户上传的word文件转换成htm格式,必须是“筛选过的网页”,不然显示不了图片;
2、将转换好的htm格式文档路径存入数据库,读的时候就在数据库里读。

jacob1.8包下载地址:http://sourceforge.net/projects/jacob-project/

可能会抛出no jacob in java.library.path异常,解决办法如下:
1、 把jacob.dll在 C:/Program Files/Java/jdk1.5.0_08/bin、C:/Program Files/Java/jdk1.5.0_08/jre/bin、 C:/WINDOWS/system32 目录下各.放一份
2、把jacob.jar放入 项目的lib包下

将word文档转换为htm格式的java代码如下:

import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
public class WordToHtml {
//将指定目录下面的指定doc文件转化为HTML并存储在savepaths目录下
public static void change(String filepaths, String savepaths) {
File f = new File(filepaths);
String filename = f.getName();
String filetype = filename.substring((filename.length() - 3), filename.length());// 取得文件类型
if (filetype.equals("doc")) {// 判断是否为doc文件
System.out.println("当前正在转换......");
// 打印当前目录路径
System.out.println(filepaths);
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
String docpath = filepaths;
String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
String inFile = docpath;
// 要转换的word文件
String tpFile = htmlpath;
// HTML文件
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new Variant(false),new Variant(true) },
new int[1]).toDispatch();// 打开word文件
/*
* new Variant(10)筛选过的网页
* new Variant(9) 单个文件网页
* new Variant(8) 另存为e799bee5baa6e58685e5aeb9331网页
* new Variant(7) 另存为txt格式
* new Variant(6) 另存为rtf格式
* new Variant(0) 另存为doc格式
*/
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {tpFile, new Variant(10) }, new int[1]);// 作为html格式保存到临时文件
Variant fl = new Variant(false);
Dispatch.call(doc, "Close", fl);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}

public static void main(String[] args) {
//转换目录下的所有doc文件
// String paths = new String("D://test//");
String savepaths = new String("D://test//");
// changeAll(paths, savepaths);
//
//转换指定doc文件
String filepaths = "D://test.doc";
change(filepaths, savepaths);
}
}
JAVA代码:
[java] view plaincopy
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TransformFiletoHtml
{
int WORD_HTML = 8;
int WORD_TXT = 7;
int EXCEL_HTML = 44;

/**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public void wordToHtml(String docfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动e69da5e887aa7a64337word
try
{
app.setProperty("Visible", new Variant(false));
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();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}

/**
* EXCEL转HTML
* @param xlsfile EXCEL文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public void excelToHtml(String xlsfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
try
{
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
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)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}

/**
* /删除指定文件夹
* @param folderPath 文件夹全路径
* @param htmlfile 转换后HTML存放路径
*/
public void delFolder(String folderPath)
{
try
{
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {e.printStackTrace();}
}

/**
* /删除指定文件夹下所有文件
* @param path 文件全路径
*/
public boolean delAllFile(String path)
{
boolean flag = false;
File file = new File(path);
if (!file.exists())
{
return flag;
}
if (!file.isDirectory())
{
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++)
{
if (path.endsWith(File.separator))
{
temp = new File(path + tempList[i]);
}
else
{
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile())
{
temp.delete();
}
if (temp.isDirectory())
{
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
}

调用JAVA代码:
[java] view plaincopy
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TransformFiletoHtml trans = new TransformFiletoHtml();
trans.wordToHtml("D:\\sinye.doc", "D:\\sinye.html");
}

}

1、利用jacob包将用户上传的word文件转换成htm格式,必须是“筛选过的网页”,不然显示不了图片;
2、将转换好的htm格式文档路径存入数据库,读的时候就在数据库里读。

jacob1.8包下载地址:http://sourceforge.net/projects/jacob-project/

可能会抛出no jacob in java.library.path异常,解决办法如下:
1、 把jacob.dll在 C:/Program Files/Java/jdk1.5.0_08/bin、C:/Program Files/Java/jdk1.5.0_08/jre/bin、 C:/WINDOWS/system32 目录下各.放一份
2、把jacob.jar放入 项目的lib包下

将word文档转换为htm格式的java代码如下:

import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
public class WordToHtml {
//将指定目录下面的指定doc文件转化为HTML并存储在savepaths目录下
public static void change(String filepaths, String savepaths) {
File f = new File(filepaths);
String filename = f.getName();
String filetype = filename.substring((filename.length() - 3), filename.length());// 取得文件类型
if (filetype.equals("doc")) {// 判断是否为doc文件
System.out.println("当前正在转换......");
// 打印当前目录路径
System.out.println(filepaths);
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
String docpath = filepaths;
String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
String inFile = docpath;
// 要转换的word文件
String tpFile = htmlpath;
// HTML文件
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));// 设置e799bee5baa6e78988e69d83366word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new Variant(false),new Variant(true) },
new int[1]).toDispatch();// 打开word文件
/*
* new Variant(10)筛选过的网页
* new Variant(9) 单个文件网页
* new Variant(8) 另存为网页
* new Variant(7) 另存为txt格式
* new Variant(6) 另存为rtf格式
* new Variant(0) 另存为doc格式
*/
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {tpFile, new Variant(10) }, new int[1]);// 作为html格式保存到临时文件
Variant fl = new Variant(false);
Dispatch.call(doc, "Close", fl);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}

public static void main(String[] args) {
//转换目录下的所有doc文件
// String paths = new String("D://test//");
String savepaths = new String("D://test//");
// changeAll(paths, savepaths);
//
//转换指定doc文件
String filepaths = "D://test.doc";
change(filepaths, savepaths);
}
}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网