Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I save a JPEG from a URL to a file?
    text
    copied!<p>I am trying to save a JPEG image from a URL to a file with Java. URL: <a href="http://150.214.222.100//axis-cgi/mjpg/video.cgi?resolution=640x480&amp;compression=1&amp;duration=1&amp;timeout=&amp;dummy=garb" rel="nofollow noreferrer">http://150.214.222.100//axis-cgi/mjpg/video.cgi?resolution=640x480&amp;compression=1&amp;duration=1&amp;timeout=&amp;dummy=garb</a></p> <p>I tried the following: 1) </p> <pre><code>Image image = fetchImage(urlNorthView); saveImage2Disk(image); public static Image fetchImage( URL u ) throws MalformedURLException, IOException { Toolkit tk = Toolkit.getDefaultToolkit(); return tk.createImage(u ); } private void saveImage2Disk(Image Image) throws IOException{ File outputFile = new File("urlNorthView"+Calendar.getInstance().getTimeInMillis()+".jpg"); BufferedImage bufferedImage = new BufferedImage(Image.getWidth(null),Image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(Image, null, null); ImageIO.write(bufferedImage, "jpg", outputFile); } </code></pre> <p>=> Exception:"Width (-1) and height (-1) cannot be &lt;= 0"</p> <p>2) </p> <pre><code>inputStream2Disk((InputStream) urlNorthView.getContent()); private void inputStream2Disk(InputStream in) throws Exception{ File outputFile = new File("urlNorthView"+Calendar.getInstance().getTimeInMillis()+".jpg"); OutputStream out=new FileOutputStream(outputFile); byte buf[]=new byte[1024]; int len; while((len=in.read(buf))&gt;0) out.write(buf,0,len); out.close(); in.close(); } </code></pre> <p>The file is somehow broken. When I open it with <a href="http://en.wikipedia.org/wiki/Kate_%28text_editor%29" rel="nofollow noreferrer">Kate</a>, I can read:</p> <blockquote> <p>--myboundary Content-Type: image/jpeg Content-Length: 38256 ....</p> </blockquote> <p>There should not be any text in a binary file.</p> <p>What could the problem be?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload