1 package webViewer; 2 3 import java.io.*; 4 5 import junit.framework.Test; 6 7 import com.aspose.words.*; //引入espouse-word-14.11.0-jdk16.jar包 8 9 public class Word2Pdf { 10 private static boolean getLicense() { 11 boolean result = false; 12 try { 13 InputStream is = Test.class.getClassLoader().getResourceAsStream("wordlicense.xml"); // license.xml應放在..\WebRoot\WEB-INF\classes路徑下 14 com.aspose.words.License aposeLic = new com.aspose.words.License(); 15 aposeLic.setLicense(is); 16 result = true; 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 return result; 21 } 22 23 public static void word2pdf(String Address) { 24 25 if (!getLicense()) { // 驗證License 若不驗證則轉化出的PDP文檔會有水印產生 26 return; 27 } 28 try { 29 File file = new File("C:/inetpub/wwwroot/web/file/pdf1xxxx.pdf"); //新建一個空白pdf文檔 30 FileOutputStream os = new FileOutputStream(file); 31 Document doc = new Document(Address); //Address是將要被轉化的word文檔 32 doc.save(os, SaveFormat.PDF); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 33 os.close(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 }
運行代碼css
1 package webViewer; 2 public class Test { 3 public static void main(String[] args){ 4 Word2Pdf.word2pdf("http://172.16.8.80/file/4.docx"); 5 /*File f=new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 6 if(f.exists()){ 7 f.delete(); 8 } else{ 9 System.out.println("無文件"); 10 }*/ 11 } 12 }
運行以上代碼能夠正常生成想要的pdf文件html
可是將java函數放到jsp中java
以下web
1 <%@ page language="java" import="java.sql.*" import="java.util.*" import="java.io.*" pageEncoding="utf-8" %> 2 <%@ page import="webViewer.*" %> 3 <%@ page import="com.aspose.words.*" %> 4 <%@ page import="com.aspose.cells.*" %> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 <title>My JSP 'View.jsp' starting page</title> 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 21 <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> 22 23 </head> 24 <% 25 String pdfFileAddress=request.getParameter("pdfFileAddress"); 26 String suffix=request.getParameter("suffix"); 27 String pdfFileAddress1; 28 %> 29 <body> 30 <% 31 /* File f=new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 32 if(f.exists()){ 33 f.delete(); } 34 else{ */ 35 %> 36 <% 37 if(suffix.equals("doc")||suffix.equals("docx")){ 38 Word2Pdf.word2pdf("http://172.16.8.80/"+pdfFileAddress); 39 40 // Word2Pdf.word2pdf("http://172.16.8.80/file/4.docx"); 41 /* Word2Pdf.word2pdf("http://172.16.8.80/"+pdfFileAddress);*/ 42 /* try { 43 File file = new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 44 FileOutputStream os = new FileOutputStream(file); 45 Document doc = new Document("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/"+pdfFileAddress); 46 doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 47 os.close(); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } */ 51 pdfFileAddress1="file/pdf1xxxx.pdf"; 52 }else{ 53 if(suffix.equals("xls")||suffix.equals("xlsx")){ 54 Excel2Pdf.excel2pdf("http://172.16.8.80/"+pdfFileAddress); 55 pdfFileAddress1="file/pdf1yyyy.pdf"; 56 }/*else{ 57 if(suffix.equals("ppt")||suffix.equals("pptx")){ 58 Ppt2pdf.ppt2pdf("http://172.16.8.80:8080/generic/web/"+pdfFileAddress); 59 pdfFileAddress1="file/pdf1.pdf"; 60 }*/ else{ 61 pdfFileAddress1=pdfFileAddress; 62 } 63 } 64 /*}*/ 65 66 %> 67 <iframe src="http://172.16.8.80/viewer.html?file=<%=pdfFileAddress1%>" height="600px" width="680px"></iframe> 68 </body> 69 </html>
頁面會出現如下錯誤sql
HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError:
junit/framework/Testapache
type Exception reporttomcat
message javax.servlet.ServletException: java.lang.NoClassDefFoundError:
junit/framework/Testwebsocket
description The server encountered an internal error that prevented it from fulfilling
this request.app
exception
org.apache.jasper.JasperException: javax.servlet.ServletException:
java.lang.NoClassDefFoundError: junit/framework/Test
org.apache.jasper.servlet.JspServletWrapper.handleJspException
(JspServletWrapper.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)webapp
root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: junit/framework/Test
org.apache.jasper.runtime.PageContextImpl.doHandlePageException
(PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException
(PageContextImpl.java:845)
org.apache.jsp.View_jsp._jspService(View_jsp.java:179)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: junit/framework/Test
webViewer.Word2Pdf.getLicense(Word2Pdf.java:13)
webViewer.Word2Pdf.word2pdf(Word2Pdf.java:25)
org.apache.jsp.View_jsp._jspService(View_jsp.java:133)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.70
logs.
Apache Tomcat/7.0.70
經上網研究,解決方法是:tomcat的lib庫中缺乏junit.jar包
漲到Eclipse中junit.jar包 如圖:
將該jar包拷貝到tomcat的lib目錄中 以下圖
最後從新啓動tomcat便可正常生成pdf。