How to get a pdf into the java code from a url? -
i have java code parses pdf. loads pdf locally. want source pdf url, "https://www.abc.com/xyz.pdf" instead of "c:\xyz.pdf", if change strings, throws error.
url url = new url("https://www.abc.com/xyz.pdf"); inputstream in = url.openstream(); fileoutputstream fos = new fileoutputstream(new file(temp.pdf)); int length = -1; byte[] buffer = new byte[1024];// buffer portion of data // connection while ((length = in.read(buffer)) > -1) { fos.write(buffer, 0, length); } fos.close(); in.close();
also, java.net.unknownhostexception when try above code @ line 2. link works fine in browser.
in java if want read directly url, can use this:
url oracle = new url("http://www.oracle.com/"); bufferedreader in = new bufferedreader( new inputstreamreader(oracle.openstream())); string inputline; while ((inputline = in.readline()) != null) system.out.println(inputline); in.close();
basically have use url.openstream method
Comments
Post a Comment