help with downloading file and saving it

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

help with downloading file and saving it

Postby KanedaSyndrome » Sun Dec 28, 2008 5:13 pm

Hey +- and snow and any others I haven't met..

I am having a problem, been trying to couple the knowledge from this post and this.

My simple first approach was to try and download that sunny.gif as well as you did, what I want to do then is to save to the drive.

The code I have been jumbling with is this:

Code: Select all
try
       {
          URL myURL = new URL("http://www.google.com/images/weather/sunny.gif");
          URLConnection conn = myURL.openConnection();
          conn.connect();
          InputStream is = conn.getInputStream();
          FileOutputStream fOut = openFileOutput("sunny.gif", MODE_WORLD_READABLE);
          OutputStreamWriter osw = new OutputStreamWriter(fOut);
          
          osw.write(is);
          osw.flush();
          osw.close();
       
       }
       catch(Exception e)
       {
       }


I don't really understand all the concepts to the bottom, but I get an error with the osw.write, the error in the tooltip is:

"The method write(int) in the type OutputStreamWriter is not applicable for the arguments (InputStream)"

Hope you can help me out, since my ultimate goal is to have my app download a file from a server and then do stopwatch timer on the time it takes to get the file, since I need this time to calculate on connectivity and such.
KanedaSyndrome
Developer
Developer
 
Posts: 41
Joined: Tue Nov 11, 2008 3:32 pm

Top

Postby MrSnowflake » Sun Dec 28, 2008 5:52 pm

Don't know if the rest of you code is correct, but you should do something like: osw.write(is.readByte(...)); in a for loop until is is fully read.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby KanedaSyndrome » Sun Dec 28, 2008 5:57 pm

would that count for all file types? This particular thing is a .gif

Perhaps, if you have time and aren't too bothered, would you give a code example of how you would go about doing this? Something simple as getting a file from the net (any extension really), and then write it to the drive.

The thing is that I am not so solid in java and am not sure what needs to be done, if I need to load bytes into an array at a time or what not.
KanedaSyndrome
Developer
Developer
 
Posts: 41
Joined: Tue Nov 11, 2008 3:32 pm

Postby KanedaSyndrome » Sun Dec 28, 2008 6:44 pm

would I have to first:

initialize the connection and open and load the url, which in this case is just a picture.

Then load it into a char[] array or something to hold the data?

And first then being able to write it to a file?


Am quite the noob in this particular area. Hope you can help.
KanedaSyndrome
Developer
Developer
 
Posts: 41
Joined: Tue Nov 11, 2008 3:32 pm

Postby KanedaSyndrome » Mon Dec 29, 2008 4:17 pm

Tried messing around some code, but am really a noob in this area:

Code: Select all
try
       {
          URL myURL = new URL("http://www.google.com/images/weather/sunny.gif");
          URLConnection conn = myURL.openConnection();
          conn.connect();
          InputStream is = conn.getInputStream();
          FileOutputStream fOut = openFileOutput("sunny.gif", MODE_WORLD_READABLE);
          OutputStreamWriter osw = new OutputStreamWriter(fOut);
          
          int ini = fOut; //make ini = byte size of file.. somehow.. 2 is just a placeholder
          
          for(int i = ini; i >= 0; i--)
          {
          osw.write(is.read()); //confused about this.. :(
          }
          osw.flush();
          osw.close();
       //osw.write(is.readByte(...)); in a for loop until is is fully read.
       }
       catch(Exception e)
       {
       }



As you can see, encased it in a for loop, but need to know for how long it needs to run, and that I bet is dependant on the amount of bytes of the incoming file, if I am understanding this correctly..

But you need to dumb it a bit down for me, hope you can and are willing to help me. :/
KanedaSyndrome
Developer
Developer
 
Posts: 41
Joined: Tue Nov 11, 2008 3:32 pm

Postby MrSnowflake » Mon Dec 29, 2008 4:28 pm

More like (pseudo code):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. byte[] buffer = new byte[1024];
  3.  
  4. int read = 0;
  5.  
  6. int lastRead = 0;
  7.  
  8. while (read > 0) {
  9.  
  10.   lastRead = is.read(read, buffer, 1024);
  11.  
  12.   read += lastRead;
  13.  
  14.   osw.write(buffer);
  15.  
  16. }
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests