简体   繁体   English

使用pdfbox将拆分的pdf文档保存到远程apache服务器

[英]save splitted pdf document to remote apache server using pdfbox

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. 我正在从远程服务器读取pdf文件,使用pdfbox进行拆分,并且能够将拆分后的文件保存在本地系统中,但是无法将拆分后的文件保存到远程服务器中。 How can i do that using pdfbox. 我如何使用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 下面是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)

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 . 您不能以这种方式通过http写入文件。 Your 'outputpath' is not a file system. 您的“ outputpath”不是文件系统。 When you first posted this question before your recent edits, your 'outputpath' was a local file system. 当您在最近编辑之前首次发布此问题时,您的“ outputpath”是本地文件系统。 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. 要通过http写入或上传文件,您需要做的第一件事是运行服务器以处理POST请求。 If you are running Apache instead, you need to some servr-side scripting to accept a file. 如果您正在运行Apache,则需要一些服务器端脚本来接受文件。 Then you can grab the bytes from the pdf output file in a byte array and write those bytes to the remote stream. 然后,您可以从pdf输出文件中的字节数组中获取字节,并将这些字节写入远程流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM