我正在尝试编写一个读取上传的pdf文件的servlet,然后从另一个servlet中读取它。我想解析该pdf文件并在解析的文本中搜索关键字。
首先我做的应用程序就像普通的java代码然后它工作正常。但是,当我使用与servlet相同的代码时,它显示了一个意外的错误类定义未找到错误。
以下是错误:
SEVERE: Servlet.service() for servlet [ex.sat.com.PDFTestServlet] in context with path [/ContentBasedFileRetrival] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.pdfbox.pdfparser.PDFParser
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at ex.sat.com.PDFTestServlet.pdftoText(PDFTestServlet.java:67)
at ex.sat.com.PDFTestServlet.doPost(PDFTestServlet.java:48)
我将pdf box jar文件添加到我的eclipse项目的库中。没有编译错误。
但是当我将项目作为java应用程序运行时,相同的代码工作正常。
这是我的servlet:
package ex.sat.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
@WebServlet("/PDFTestServlet")
public class PDFTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String SAVE_DIR = "uploadFiles";
/**
* @see HttpServlet#HttpServlet()
*/
public PDFTestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
ServletContext context = getServletContext();
PrintWriter writer = response.getWriter();
String appPath = request.getServletContext().getRealPath("");
String savePath = appPath + File.separator+"j2.pdf";
writer.println(savePath);
String pdf_text=pdftoText(savePath);
writer.println("SATYA");
writer.println(pdf_text);
}
static String pdftoText(String fileName) {
PDFParser parser;
String parsedText = null;;
PDFTextStripper pdfStripper = null;
PDDocument pdDoc = null;
COSDocument cosDoc = null;
File file = new File(fileName);
if (!file.isFile()) {
System.err.println("File " + fileName + " does not exist.");
return null;
}
try {
parser = new PDFParser(new FileInputStream(file));
} catch (IOException e) {
System.err.println("Unable to open PDF Parser. " + e.getMessage());
return null;
}
try {
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(5);
parsedText = pdfStripper.getText(pdDoc);
} catch (Exception e) {
System.err
.println("An exception occured in parsing the PDF Document."
+ e.getMessage());
} finally {
try {
if (cosDoc != null)
cosDoc.close();
if (pdDoc != null)
pdDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return parsedText;
}
}
答案 0 :(得分:1)
在我看来,servlet容器找不到您的库,您可以尝试将jar文件放在容器的libraries目录中,然后重新启动它。 (库通常不会部署应用程序)。
答案 1 :(得分:0)