方法很简单,利用著名的Runtime.getRuntime()即可,如下:
/** */
/**
* @param filePath pdf文件路径
* @return
*/
public String getPdfContent(String filePath) ... {
String excute="pdftotext";
String[] cmd=new String[]...{excute, "-enc", "UTF-8", "-q", filePath,"-"};
Process p=null;
try ...{
p=Runtime.getRuntime().exec(cmd);
} catch (IOException e) ...{
e.printStackTrace();
}
BufferedInputStream bis=new BufferedInputStream(p.getInputStream());
InputStreamReader reader=null;
try ...{
reader=new InputStreamReader(bis,"UTF-8");
} catch (UnsupportedEncodingException e1) ...{
e1.printStackTrace();
}
StringBuffer sb=new StringBuffer();
try ...{
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
sb = new StringBuffer();
while (line != null) ...{
sb.append(line);
sb.append(" ");
line = br.readLine();
}
} catch (Exception e) ...{
e.printStackTrace();
}
return sb.toString();
}
* @param filePath pdf文件路径
* @return
*/
public String getPdfContent(String filePath) ... {
String excute="pdftotext";
String[] cmd=new String[]...{excute, "-enc", "UTF-8", "-q", filePath,"-"};
Process p=null;
try ...{
p=Runtime.getRuntime().exec(cmd);
} catch (IOException e) ...{
e.printStackTrace();
}
BufferedInputStream bis=new BufferedInputStream(p.getInputStream());
InputStreamReader reader=null;
try ...{
reader=new InputStreamReader(bis,"UTF-8");
} catch (UnsupportedEncodingException e1) ...{
e1.printStackTrace();
}
StringBuffer sb=new StringBuffer();
try ...{
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
sb = new StringBuffer();
while (line != null) ...{
sb.append(line);
sb.append(" ");
line = br.readLine();
}
} catch (Exception e) ...{
e.printStackTrace();
}
return sb.toString();
}