我需要一些幫助與我的代碼..我有一個工作的Java代碼將PDF轉換爲文本文件和一個JSP頁面上傳它。 事情是我想要鏈接它們,即當我上傳PDF文件時,應該將上傳的文件作爲輸入並生成相應的轉換文本文件。在JSP頁面上載PDF文件並將其轉換爲文本文件
FileConverter.java
package fileconverter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.IOException;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
public class FileConverter {
public static void main(String[] args) {
selectPDFFiles();
}
//allow pdf files selection for converting
public static void selectPDFFiles(){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Please wait...");
for(int i=0;i<Files.length;i++){
convertPDFToText(Files[i].toString(),"Text-File"+i+".txt");
}
System.out.println("Conversion complete");
}
}
public static void convertPDFToText(String src,String desc){
try{
FileWriter fw=new FileWriter(desc);
try (
BufferedWriter bw = new BufferedWriter(fw)) {
PdfReader pr=new PdfReader(src);
int pNum=pr.getNumberOfPages();
for(int page=1;page<=pNum;page++){
String text=PdfTextExtractor.getTextFromPage(pr, page);
bw.write(text);
bw.newLine();
}
bw.flush();
}
}
catch(IOException e){}
}
}
upload.jsp
<%--
Document : upload
Created on : May 16, 2014, 1:42:47 PM
Author : Kelvin
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><title>Upload and convert</title>
<body>
<div id="main">
<table id="uploadtbl" width="550" align="center" border="1">
<tr>
<th align="center">Upload and convert pdf to text file</th>
</tr>
<tr>
<td>
<input type="file" name="upload">
</td>
</tr>
<tr>
<td><input type="submit" name="totext" value="Convert To Text" /></td>
</tr>
</table>
</div>
</body>
</html>
你的問題是什麼? – AlexR
謝謝AlexR,我這裏的問題是我不知道如何鏈接Java程序和JSP頁面,這樣當我點擊提交按鈕時,將上傳的PDF文件轉換爲文本文件。 – user2643628