1

I have to consume as rest api API(/getconstomerList) from my java code that returns a pdf file. I need to read the PDF file and convert it to String.

I tried doing this with tika

BodyContentHandler handler = new BodyContentHandler(10 * 1024 * 1024);
Metadata metadata = new Metadata();
URL url = new URL("http://" + hostName + uri);
BufferedInputStream inputstream = new BufferedInputStream(url.openStream());
Parser parser = new AutoDetectParser();
parser.parse(inputstream, handler, metadata, pcontext);
return handler.toString();

But this code keep returning me empty response, I am not sure what is wrong or if there is any other way to do this. Please help.

Lucie
  • 127
  • 1
  • 2
  • 12
  • 1
    Does the code work if you take out the download part and feed the same PDF from a local file into it? There may be redirects and such involved, better download the PDF into memory (or temporary file) first before handing it to Tika. – Thilo Feb 22 '19 at 06:28
  • 1
    Yes the code works with local pdf, I tested it with the same that's being provided with the rest response. I am new with handling file as rest response, can try saving it locally first. – Lucie Feb 22 '19 at 06:34

1 Answers1

1

For HTTP calls, I would recommend Retrofit library. You can easily setup/configure by providing an interface containing API methods. One of those methods would return the Call<ResponseBody> from which get the ResponseBody and using it you can get the InputStream. Using it, you can convert the stream to file and do what you would like to do with it. Below is a sample code.

String downloadFileAndReadAsString(ResponseBody responseBody, Path filePath) {
    try (InputStream inputStream = responseBody.byteStream()) {
        Path downloadedFilePath = Files.copy(inputStream, filePath, 
StandardCopyOption.REPLACE_EXISTING);
return new String(Files.readAllBytes(downloadedFilePath));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Hope it helps.

the_tech_maddy
  • 575
  • 2
  • 6
  • 21