Carregar PDf da net

2 respostas
albjava

eu tenho que ler e imprimir arquivo no formato .pdf
ja vi um topico anterior(http://www.guj.com.br/posts/list/35395.java) que é perfeito pra isso.
so que ele não serve para endereço que esteja na net.
como faço pra baixar o arquivo e depois indexi-lo??

o codigo

package URL;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Date;

import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;

public class PdfParser {

    /***
     * Extract the content from the given PDF file. As a side effect most other fields are set too.
     *
     * @see org.zilverline.extractors.AbstractExtractor#getContent(java.io.File)
     */
    private String enderecoRecurso;

    public void setEnderecoRecurso(String enderecoRecurso) {
        this.enderecoRecurso = enderecoRecurso; //caminho do arquivo
    }

    public String getConteudo() {

        File f = new File(this.enderecoRecurso);
        FileInputStream is = null;
        try {
            is = new FileInputStream(f);
        } catch (IOException e) {
            System.out.println("ERRO: " + e.getMessage());
            return null;
        }

        PDDocument pdfDocument = null;
        try {
System.out.println("#1");
            PDFParser parser = new PDFParser(is);
System.out.println("#2");
            parser.parse();
System.out.println("#3");
            pdfDocument = parser.getPDDocument();
System.out.println("#4");
            PDFTextStripper stripper = new PDFTextStripper();
System.out.println("#5");
            return stripper.getText(pdfDocument);
//System.out.println("#6");
        } catch (IOException e) {
            return "ERRO: Não pode abrir o stream" + e;
        } catch (Throwable e) {
// catch this, since we need to close the resources
            return "ERRO: Erro enquanto baixava o pdf" + e;
        } finally {
            if (pdfDocument != null) {
                try {
                    pdfDocument.close();
                } catch (IOException e) {
                    return "ERRO: Não pode fechar o pdf." + e;
                }
            }
        }
    }
}

abraçs

2 Respostas

albjava
package URL;

import java.net.MalformedURLException;
import java.io.IOException;

public class Page {

    public static void main(String args[]) throws MalformedURLException,
            IOException {
        String url = "http://www.prodemge.gov.br/revistafonte/volume2/pdf/Economia_Java.pdf";
        
        Lendo lendo = new Lendo(url);
        String str = lendo.conectaURL();
        
        PdfParser pdf = new PdfParser();
        pdf.setEnderecoRecurso(str);
        pdf.getConteudo();

    }
}
albjava

galera…muita ajuda… :frowning: :frowning: :frowning:

Criado 3 de agosto de 2006
Ultima resposta 3 de ago. de 2006
Respostas 2
Participantes 1