Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

I wants to save images through java program

Thread view: 
gupta.divyendu@gmail.com - 22 Mar 2007 13:08 GMT
I want a java program so I could give a URL of the image to it and
that image is saved in a particular folder.
The code I am using is this

import java.net.*;
import java.io.*;

class new
{
 public static void main(String[] args)
 {
    String line = null;
       StringBuffer buffer = new StringBuffer();
addressofImage="http://www.toy4education.com/files/1763110/uploaded/
roller.jpg"
       try
           {
               if (addressOfImage != null)
                 if(addressOfImage.length() > 0)
                 {
                   //create URL Object
                   urlobj = new URL(addressOfImage);
                   //open network connection
                   BufferedReader inStream = new BufferedReader(
                       new InputStreamReader(
                           new DataInputStream(
                               urlobj.openStream())));

                   while ((line = inStream.readLine() ) != null)
                   {
                       buffer.append(line + "\n");
                       //buffer.append(line);
                   }

                   inStream.close();

                   //process to convert buffer to byte array
                   String str = buffer.toString(); //create a new
string
str
                   byte[] bt = new byte[str.length()];//create byte
array
object
                   System.out.println("buffer.length: " +
buffer.length());
                   System.out.println("Str.length: " + str.length());

                   str.getBytes(0,buffer.length(),bt,0);//make str
into
byte now
                   //str.getBytes();//make str into byte now
                   //System.out.println("str: " + str);

                   //create file object to write to
                   //URLPath = urlobj.getHost() + urlobj.getFile();
                   String f ;
                   f = urlobj.getFile();
                   System.out.println(f);
                   int pos =  f.lastIndexOf("/");
                   String tempString = "C:\\temp\\" + f.substring(pos
+1);

                   File outputFile = new File(tempString);
                   FileOutputStream fos = new
FileOutputStream(outputFile);
                   //fos.write(bt);
                   fos.write((buffer.toString()).getBytes());
                   fos.close();

                 }

           }  // end try
           catch(IOException e)
           {
               System.out.println("A network error occured:" +
e.getMessage());

    }
}
But image is being saved but does not get displayed correctly. It is
very blurred.
Also it could not be opened in PS.
It gives error "problem parsing the jpeg data........."
Please help
Ingo R. Homann - 22 Mar 2007 13:21 GMT
Hi,

Converting binary data to a String is not a good idea. Reading it in a
byte[], and writing that arry, works.

Ciao,
Ingo
Chris Uppal - 22 Mar 2007 13:46 GMT
>   BufferedReader inStream = new BufferedReader(
>            new InputStreamReader(
>                new DataInputStream(
>                      urlobj.openStream())));

I very much doubt whether you should be using a DataInputStream for this and
you /definitely/ should not be using an InputStreamReader.  The data is binary,
and you should read it and operator it as binary -- try to do anything else and
(unless you know /exactly/ what you are doing) you'll get into a terrible mess,
and will write incorrect data to the output file.  Also, your buffering is in
the wrong place, it should be immediately around the urlobj.openStream() call.

Which means that you should be using an InputStream and an OutputStream, plus
buffering for both of those (a BufferedInputStream and a BufferedOutputStream).
The data you read in and write out should be either single bytes (which makes
the code simple, and will work reasonably efficiently since you are using
buffering), or you may prefer to use the form of the read() method which takes
a byte[] array (which will be marginally more efficient, but is more work to
get exactly right).  There should be no uses of Strings or 'char's in your IO
code anywhere (for this task); the only Strings which should occur in your
example would be used for the filenames.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.