saving an image via an url


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: saving an image via an url

  1. #1
    Join Date
    May 2004
    Posts
    2

    saving an image via an url

    Hello. I am a newbie to Java, so please bear with me.

    I am working on an image app. What I have achieved is to load an image, affect it (brightness, scale etc) and then save it to the local HD as a Jpg... no problems and I am impressed with the way Java deals with this.

    Now I am trying to save the image via an URL, or upload it to a server via the http proxy. I have managed to communicate over http, but am having problems sending an image.

    I have seen posts here where an image is loaded over an url, but written via one. Code follows.

    this code is saving to the HD no problem:
    Code:
                            FileOutputStream fo = new FileOutputStream("myImg.jpg");
    	  BufferedOutputStream bo = new BufferedOutputStream(fo);
    	  ImageIO.write(destination, "jpeg", bo);
                           //destination is a buffered image
    	  bo.close();
    This is the code I have tried to write the image, but I am sure I am missing something as I am not creating a file, just trying to output to the stream... which I am sure is wrong:
    Code:
    	   URL url = new URL( "http://127.0.0.1/testing/javaUpload/myImg.jpg" );
    	   HttpURLConnection urlcon  =   (HttpURLConnection) url.openConnection(  );
    
    	   urlcon.setRequestMethod("POST");
    	   urlcon.setRequestProperty("Content-type", "image/jpeg");
    	   urlcon.setDoOutput(true);
    	   urlcon.setDoInput(true);
    
      	   ImageIO.write(destination, "jpeg", urlcon.getOutputStream());
                            //destination is a buffered image.
    thanks for taking the time to read this post, hope someone can point me in the right direction.

    cheers
    Martin

  2. #2
    Join Date
    May 2004
    Posts
    26
    At the first look, I would say that you need to flush the BufferedOutputStream.

    BufferedOutputStream.flush();

    Without this, the data stays within the buffer till the stream fills out. Perhaps, in your case, this is what the problem is.

    If this doesn't work, I will look at this in more detail.

  3. #3
    Join Date
    May 2004
    Posts
    2

    image via url

    Hello, thanks for taking the time to read my post.

    The first code section works fine, with the bufferedOutputStream, this is saving the image to the HD not a problem here.

    The problem I am having is with the second code section where I am trying to save a file to a server.

    Any pointers would be appreciated... cheers again for reading.

    cheers
    Martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center