Reputation: 61
I'm reading pdf file from remote server, splitting using pdfbox and able to save that splitted file in local system, but unable to save splitted file into the remote server. How can i do that using pdfbox. Below is the code for splitting and saving splitted file in local system
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();
below is the 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)
Upvotes: 0
Views: 844
Reputation: 10184
This is where the exception is coming in your code:
pd.save(new FileOutputStream(outputPath+"file1.pdf"));
You cannot write files this way over http
. Your 'outputpath' is not a file system. When you first posted this question before your recent edits, your 'outputpath' was a local file system. I am sure it would have worked had you tried with it. The easiest solution for your problem is create a network fileshare of the remote server where you want to upload the file.
To write or upload files over http, the first thing you need is a running server handling POST requests. If you are running Apache instead, you need to some servr-side scripting to accept a file. Then you can grab the bytes from the pdf output file in a byte array and write those bytes to the remote stream.
Upvotes: 1