This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am opening a connection to a URL and I want to send the data read onto a browser. In this case I am reading a pdf file. It looks like the null characters in the pdf file are not being sent or being changed somehow and so subsequently the pdf file is empty. It works great for html/text files. Im using DataInputStream with read to get the url data and DataOuputStream.writeBytes to output the data. I have tried many other combinations. (BufferedInputStream, BufferedReader, OutputStreamWriter) Am I missing something - how do I stop the file being corrupted, Can this be done? Any help greatly appreciated.
A PDF file doesn't contain a String, and trying to treat it as one is going to cause problems when Java converts to/from the internal Unicode representation of character data. While this will work OK for textual data like HTML, it will totally destroy a PDF file. Don't wrap the InputStream you get from the URLConnection with anything -- just call read(byte) on it directly, and write the data using FileOutputStream.write(byte). This will result in an exact copy of what you read from the URL being put into your file, and your problem should go away. The classic implementation would look something like this:
I don't know exactly what you're trying to do, but why not just send the URL itself to the browser and let the browser handle it? You can do it easily with the BrowserLauncher class from http://browserlauncher.sourceforge.net Brian [ October 16, 2003: Message edited by: Brian Pipa ]