我正在从远程服务器读取pdf文件,使用pdfbox拆分并能够将该拆分文件保存在本地系统中,但无法将拆分文件保存到远程服务器中。我怎么能用pdfbox做到这一点。 以下是在本地系统中拆分和保存拆分文件的代码
String urlPath = "http://localhost/input/"+pdfName;
String outputPath = "http://localhost/output/";
URL url = new URL(urlPath);
InputStream is = url.openStream();
document = new PDDocument();
try {
document = PDDocument.load(is);
} catch (IOException e2) {
e2.printStackTrace();
}
List<PDDocument> listOfSplitPages = null;
Splitter splitter = new Splitter();
splitter.setStartPage(splitStartPage); // split start page
splitter.setEndPage(splitPageNumber); // split start page
splitter.setSplitAtPage(splitPageNumber);
try {
listOfSplitPages = splitter.split(document); // splitting the document
} catch (Exception e1) {
e1.printStackTrace();
}
Iterator<PDDocument> iterator = listOfSplitPages.listIterator();
while(iterator.hasNext()){
PDDocument pdfDocument = iterator.next();
try{
pd.save(new FileOutputStream(outputPath+"file1.pdf"));
} catch (Exception e){
e.printStackTrace();
System.out.println("Something went wrong with page \n Here is the error message" + e);
}
}
document.close();
下面的是Stacktrace
java.io.FileNotFoundException: http:\localhost\Invoices\output\file1.pdf (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1118)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1106)
at Invoice.SplitPdfServlet.saveFileToRelavantFolder(SplitPdfServlet.java:174)
at Invoice.SplitPdfServlet.splitPdfUsingPageNumber(SplitPdfServlet.java:127)
at Invoice.SplitPdfServlet.doPost(SplitPdfServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
这是您的代码中出现异常的地方:
pd.save(new FileOutputStream(outputPath+"file1.pdf"));
您不能通过http
以这种方式写文件。您的'outputpath'不是文件系统。当您在最近编辑之前首次发布此问题时,您的“outputpath”是本地文件系统。我相信如果你试过它会有用。解决您问题的最简单方法是创建要上传文件的远程服务器的网络文件共享。
要通过http编写或上传文件,首先需要的是处理POST请求的正在运行的服务器。如果您正在运行Apache,则需要一些servr端脚本来接受文件。然后,您可以从字节数组中的pdf输出文件中获取字节,并将这些字节写入远程流。