jsp实现在线阅读pdf文件 现在有个需求,需要能在ie中直接浏览上传的pdf文件,不能复制等操作用Java在jsp页面如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一步:下载PDFBox.jar。解压后,把lib文件下的PDFBox.jar,PDFBox-0.7.2-log4j.jar放到你classpath路径下第二步:写个简单的读取pdf文件的程序。(PdfReader.java)import java.io.File;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.net.MalformedURLException;import java.net.URL;import org.pdfbox.pdmodel.PDDocument;import org.pdfbox.util.PDFTextStripper;public class PdfReader { public void readFdf(String file) throws Exception { // 是否排序 boolean sort = false; // pdf文件名 String pdfFile = file; // 输入文本文件名称 String textFile = null; // 编码方式 String encoding = "UTF-8"; // 开始提取页数 int startPage = 1; // 结束提取页数 int endPage = Integer.MAX_VALUE; // 文件输入流,生成文本文件 Writer output = null; // 内存中存储的PDF Document PDDocument document = null; try { try { // 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件 URL url = new URL(pdfFile); //注意参数已不是以前版本中的URL.而是File。 document = PDDocument.load(pdfFile); // 获取PDF的文件名 String fileName = url.getFile(); // 以原来PDF的名称来命名新产生的txt文件 if (fileName.length() > 4) { File outputFile = new File(fileName.substring(0, fileName .length() - 4) + ".txt"); textFile = outputFile.getName(); } } catch (MalformedURLException e) { // 如果作为URL装载得到异常则从文件系统装载 //注意参数已不是以前版本中的URL.而是File。 document = PDDocument.load(pdfFile); if (pdfFile.length() > 4) { textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt"; } } // 文件输入流,写入文件倒textFile output = new OutputStreamWriter(new FileOutputStream(textFile), encoding); // PDFTextStripper来提取文本 PDFTextStripper stripper = null; stripper = new PDFTextStripper(); // 设置是否排序 stripper.setSortByPosition(sort); // 设置起始页 stripper.setStartPage(startPage); // 设置结束页 stripper.setEndPage(endPage); // 调用PDFTextStripper的writeText提取并输出文本 stripper.writeText(document, output); } finally { if (output != null) { // 关闭输出流 output.close(); } if (document != null) { // 关闭PDF Document document.close(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PdfReader pdfReader = new PdfReader(); try { // 取得E盘下的SpringGuide.pdf的内容 pdfReader.readFdf("E:\\SpringGuide.pdf"); } catch (Exception e) { e.printStackTrace(); } }} 使用struts2标签的问题 windows历史记录功能在java程序中如何实现 急,事务 补偿 听说过的进 Servlet解密不了encodeURL 浏览器的怪异 大家看看这是什么错误,十万火急 Exception report jsf+ajax问题 求EasyUI socket axis做的webservice应用运行时是不是都很慢? JSP乱码问题 链接跳转问题
第二步:写个简单的读取pdf文件的程序。(PdfReader.java)import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 输入文本文件名称
String textFile = null;
// 编码方式
String encoding = "UTF-8";
// 开始提取页数
int startPage = 1;
// 结束提取页数
int endPage = Integer.MAX_VALUE;
// 文件输入流,生成文本文件
Writer output = null;
// 内存中存储的PDF Document
PDDocument document = null;
try {
try {
// 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
URL url = new URL(pdfFile);
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 获取PDF的文件名
String fileName = url.getFile();
// 以原来PDF的名称来命名新产生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作为URL装载得到异常则从文件系统装载
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件输入流,写入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper来提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 设置是否排序
stripper.setSortByPosition(sort);
// 设置起始页
stripper.setStartPage(startPage);
// 设置结束页
stripper.setEndPage(endPage);
// 调用PDFTextStripper的writeText提取并输出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 关闭输出流
output.close();
}
if (document != null) {
// 关闭PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盘下的SpringGuide.pdf的内容
pdfReader.readFdf("E:\\SpringGuide.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}