Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:12:24+00:00 2026-05-13T20:12:24+00:00

I am trying to save a JPEG image from a URL to a file

  • 0

I am trying to save a JPEG image from a URL to a file with Java.
URL: http://150.214.222.100//axis-cgi/mjpg/video.cgi?resolution=640×480&compression=1&duration=1&timeout=&dummy=garb

I tried the following:
1)

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);
    }

=> Exception:”Width (-1) and height (-1) cannot be <= 0″

2)

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))>0)
        out.write(buf,0,len);
        out.close();
        in.close();
    }

The file is somehow broken. When I open it with Kate, I can read:

–myboundary Content-Type: image/jpeg Content-Length: 38256 ….

There should not be any text in a binary file.

What could the problem be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:12:24+00:00Added an answer on May 13, 2026 at 8:12 pm

    For some reason, the http response body when requesting that image contains a mime part (mime parts are useful for putting multiple files into a single response). In this response, there is only one mime part, so it is mostly useless.

    There is code in the javax.mail package that you might be able to use to parse this properly if you want, but it’s not a very good api, imho.

    Alternatively, there are a bunch of ways you could hackishly fix this in code yourself. Since there’s only one mime part, you can just throw away data from the beginning of your input stream until you see two newline characters in a row (bytes equal to 10). That should work since mime headers are supposed to be 7-bit ascii, iirc, so there’s no character encoding to worry about.

    Here is some sample code:

    URLConnection conn = urlNorthView.openConnection();
    InputStream in = conn.getInputStream();
    String contentType = conn.getHeaderField("Content-Type");
    if (!"image/jpeg".equals(contentType)) {
        // hack: assuming it's mime if not a raw image
        int one = in.read();
        if (one == -1) {
            // stop??
        }
        int two = in.read();
        while (two != -1 && !(two == 10 && one == 10)) {
            one = two;
            two = in.read();
        }
    }
    // if it was mime, we've stripped off the mime headers
    // and should now get the image
    inputStream2Disk(in);
    

    Edit: crap, instead of two \n, you’ll see two \r\n, or the bytes 0x0d, 0x0a, 0x0d, 0x0a. Throwing away data until you see this pattern is left as an exercise to the reader 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to take an image, optimize it as a 65% jpeg and save
I have an asp.net app and I am trying to save a text file
I am trying to write a proxy which reads an image from one server
I am trying to programatically set the dpi metadata of an jpeg image in
I'm trying to embed a IPTC data onto a JPEG image using iptcembed() but
I am trying to save some images from Flash to Php by sending a
It's me your junior programmer extraodinaire I'm trying to display an image from my
I am trying to upload a file via a form and then save in
I'm trying to save received data into file: #data Array[byte] fl = open(file_name, 'wb')
I'm trying to save some values from a form in CakePHP into a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.