• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading data from url and then outputting as string?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jason Mill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest, that pointed me in the right driection. I'm in the process of converting from using strings.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic