如何在不下载的情况下从网址阅读pdf?

时间:2012-11-12 08:43:46

标签: java swing url pdf

我试图从网址上读取pdf而不下载,然后我试图压扁它。

这是代码:

import java.io.FileOutputStream;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class FormFillAndMakeItFlattenPDF {
   public static void main(String[] args) {
      try {
         PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
         PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Flattened.pdf"));
         AcroFields form2 = stamp2.getAcroFields();
         stamp2.setFormFlattening(true);
         stamp2.close();
      }
      catch (Exception de) {
         de.printStackTrace();
      }
   }
}

但是,这会引发错误/异常。这是堆栈跟踪:

    java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:352)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:214)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:201)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:530)
    at java.net.Socket.connect(Socket.java:480)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:406)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:541)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:245)
    at sun.net.www.http.HttpClient.New(HttpClient.java:318)
    at sun.net.www.http.HttpClient.New(HttpClient.java:335)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:832)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:773)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:698)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1021)
    at java.net.URL.openStream(URL.java:1009)
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.PRTokeniser.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at FormFillAndMakeItFlattenPDF.main(FormFillAndMakeItFlattenPDF.java:18)

有谁能告诉我,我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

如果因代理问题而发生问题,请在应用程序启动时调用此方法。

System.setProperty("http.proxyHost", Config.PROXY_HOST);
System.setProperty("http.proxyPort", Config.PROXY_PORT);

将Config.X更改为系统值。

注意:对于https,您需要使用https.proxyHost和https.proxyPort添加类似的行

答案 1 :(得分:1)

您的确切代码适合我。我在Windows上使用带有Java 7的iText-4.2.0(*)。

这是我运行的代码,测试是绿色的,&#34; Flattened.pdf&#34;是否正确创建:

public class PdfTest {
    @Test
    public void testFlatten() throws Exception {
        PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
        PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Users\\david\\Flattened.pdf"));
        AcroFields form2 = stamp2.getAcroFields();
        stamp2.setFormFlattening(true);
        stamp2.close();
    }
}

(*)https://github.com/ymasory/iText-4.2.0