89346

reading image from URL

Question:

If I execute the following Groovy code

URL url = new URL('http://glowstick.blisstunes.com/wp-content/plugins/rss-poster/cache/e1ebf_josh-wink.jpg') ImageIO.read(url)

I get an exception:

javax.imageio.IIOException: Can't get input stream from URL! at javax.imageio.ImageIO.read(ImageIO.java:1369)

But if I visit the URL in a browser the image displays. Is it because the HTTP request is being blocked because it doesn't look (from the headers) like it's coming from a browser?

Answer1:

Use this:

Image image = Toolkit.getDefaultToolkit().createImage(url);

Answer2:

Use the below code as reference. Do similar thing .

URL urlTemp ; urlTemp = new URL( ContentUrl); HttpURLConnection ycGetContent = null; ycGetContent = (HttpURLConnection) urlTemp.openConnection(); ycGetContent.setDoOutput(true); ycGetContent.setRequestProperty("Cookie", cooStr); ycGetContent.connect(); BufferedInputStream bins = new BufferedInputStream(ycGetContent.getInputStream()); FileOutputStream fout = new FileOutputStream(lastWord); int m = 0; byte[] bytesIn = new byte[1024]; while ((m = bins.read(bytesIn)) != -1) { fout.write(bytesIn, 0, m); } fout.close(); bins.close(); //System.out.println("File " +lastWord +" downloaded successfully ...\n\n "); LOG.info("File " +lastWord +" downloaded successfully");

Recommend

  • Image won't appear in JLabel
  • Change ImageIcon being displayed in JFrame
  • Setting a path when creating a new File in Java
  • create ZPL to print a PNG-image with JAVA
  • Does Control.Invoke pump messages?
  • Cross-Origin Request Blocked with CORS headers present
  • Not able to catch SIGINT signal while using select()
  • How to create a thread in a class?
  • NSTimer and updating UI
  • Laravel phpunit always 404
  • how to use asyncio with boost.python?
  • An unexpected error has been detected by HotSpot Virtual Machine
  • TensorFlow C++, runtime issue
  • php show all images in directory and sort by last modified
  • SQL getting multiple columns into one column
  • Unzip archive in .Net CORE 1.0
  • Is there a Windows socket API call / option to “block” a range of ports à la SO_EXCLUSIVEADDRUSE
  • Streaming screenshots over WebRTC as a video stream from iOS
  • drawing random circles, storing their coorindates in an array
  • How to log in to an HTTPS website with Jsoup?
  • Problems compiling files using JOGL
  • Swing - Get new component under mouseReleased
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Cross-Platform Protobuf Serialization
  • javascript inside java/jsp code
  • what is the difference between the asp.net mvc application and asp.net web application
  • How to format a variable of double type
  • Matrix multiplication with MKL
  • Android Studio and gradle
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • coudnt use logback because of log4j
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • JaxB to read class hierarchy
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?