Results 1 to 13 of 13
  1. #1
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Downloaded images aren't viewable

    So I have this code which downloads the source of an image and then writes it into a file.

    It works fine, except for the fact that the created files aren't viewable. Using windows viewer, it gives me an "Image can't be constructed" error.

    Code:
    Java Code:
    try {
    	URL URL = new URL(ImageLink);
    	HttpURLConnection huc = (HttpURLConnection)URL.openConnection();            
    	huc.setRequestMethod("GET");
    	huc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; JVM)");                        
    	huc.setRequestProperty("Pragma", "no-cache");
    	huc.connect();
    	BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));
    	String line = null;
    	String source = "";
    	
    	while ((line = reader.readLine()) != null) {
              source += line; //Store the entire source of the image in a variable.
    	}
    	try {
    		Pattern p = Pattern.compile("/([\\d]*.[\\w]{2,4})$"); //Extract filename from the image URL
    		Matcher m = p.matcher(ImageLink);
    		m.find();
    		String filepath = "E:\\wamp\\www\\images\\" + m.group(1);
    		File file = new File(filepath); //Create a file at E:/wamp/www/images
    		BufferedWriter out = new BufferedWriter(new FileWriter(file));
    		out.write(source); //Write the downloaded source to the file.
    		out.close();
    	} catch (IOException e) {
    		System.err.println("General Exception " + e);
    		e.printStackTrace();
    	} 
    }
    catch(IOException ioe) {
    	ioe.printStackTrace();
    }
    catch(Exception e) {
    	System.err.println("General Exception " + e);
    		e.printStackTrace();
    	}
    }
    Can anyone tell me what I'm doing wrong?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Most images are binary data. Treating binary bytes as characters with lineends etc will not restore the image's bytes to their original values.
    Try reading the image's bytes as bytes vs as String

  3. #3
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Ah right, thanks.

    How would I go about doing that? just change the variable type of source, or something else?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Change the class that reads from the InputStream.

  5. #5
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    I've done a bit of googling, and most sites I find use something similar to this for reading files as bytes. These methods however only seem to work for files which are on the same computer as the program.

    How would I do this reading from a file on the internet? (so basically just downloading an image from a webpage)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    huc.getInputStream()
    this call returns an InputStream. You can read the bytes from that, and write them to a file.

  7. #7
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    I'm sorry, I just don't get how to read from the inputstream into something other than a string.

    Could you give an example?

    I'm sorry if I'm asking stupid things here, I'm just having a very hard time wrapping my mind around this byte concept.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Look at the API doc for the InputStream class.

    Search this forum (or google) for 'getInputStream('
    or for '.read('
    Last edited by Norm; 07-04-2010 at 10:54 PM.

  9. #9
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    But how does that work? Seeing how getInputStream returns a character based stream rather than a byte based stream.

    Also, out.write(source); doesn't seem to like byte[] very much.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    First off, as said, you want to use the InputStream directly.
    Second, you want to use a FileOutputStream, which deals with byte[]'s.

    So, for reading, you'll generally read into a byte[] of some size...8k is one I often see (so 8192 in size). Note the number of bytes read on each read, because you may not read a full 8192 bytes each time, and you'll want to tell the output stream how much you're writing.

    After each read, write the byte[] out.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    doesn't seem to like
    Please post full content of any error messages here.

  12. #12
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    The method write(int) in the type BufferedWriter is not applicable for the arguments (byte[])

    I guess I have to use something as FileOutputStream

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Readers and Writers are for characters.
    Input and Output Streams are for bytes.

Similar Threads

  1. Replies: 0
    Last Post: 05-07-2010, 07:27 AM
  2. Implementing a downloaded JAR file?
    By johntveg in forum New To Java
    Replies: 2
    Last Post: 06-27-2009, 05:51 AM
  3. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 10:19 AM
  4. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 10:54 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •