继上次的把附件内容显示在jsp页面,这次查资料发现可以把word文档直接嵌入到jsp页面。文档的内容格式不会
发生变化,很强大的一个功能
第一步 下载jacob
下载地址:http://www.chinait8.net/download/ide/4107.html
第二步 解压下载的文件,将其中dll文件放入tomcat bin目录和jdk的bin目录下,将jacob.jar 放入项目的lib库中。
这次学习还是通过servlet来实现的,仅供做个笔记日后有用
package com.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class QianRuServlet extends HttpServlet {
public QianRuServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=gbk");
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String filename = request.getParameter("filename");
filename = new String(filename.getBytes("iso8859-1"),"UTF-8");
String fullFileName = getServletContext().getRealPath("/upload/" + filename);
String perfix=filename.substring(0,filename.indexOf("."));
String savePath = getServletContext().getRealPath("/upload/" + perfix+".html");
/* String t=URLEncoder.encode(perfix, "GBK");
String urlStr = URLDecoder.decode(t, "GBK"); */
File file = new File(savePath);
//判断上传文件的保存目录是否存在
if (!file.exists() && !file.isDirectory()) {
transWord2Htm(fullFileName);
}
String urlStr=new String(perfix.getBytes("gbk"),"ISO8859-1");
//request.getRequestDispatcher("upload/"+perfix+".html").forward(request, response);
response.sendRedirect("upload/" + urlStr+".html");
}
public void init() throws ServletException {
// Put your code here
}
public boolean transWord2Htm(String docFile) {
String HtmlFile = docFile.substring(0, (docFile.length() - 5)) + ".html";
ActiveXComponent app = new ActiveXComponent("Word.Application");
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();
// 打开word文件
// 作为html格式保存到临时文件::参数 new Variant(8)其中8表示word转html;7表示word转txt;44表示Excel转html。。。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
HtmlFile, new Variant(8) }, new int[1]);
// 作为htm格式保存文件
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
app.invoke("Quit", new Variant[] {});
}
return true;
}
}
html页面会自动生成,然后通过调用就可以了。