Avatar billede fredand Forsker
25. april 2003 - 14:20 Der er 8 kommentarer og
1 løsning

Downloaded image is half?

Hello!

I try to download I file/image with a java app but the file/image is always only half. But I cant understand why?

Perhaps I could do this in some other way?

Best regards Fredrik

The code:

package wwwfileloader;

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

public class WWWFileLoader
{
    public static void main(String[] args)
    {
        byte[] fileBytes = readURLLikeBytes("http://www.google.com/logos/dna.gif");
        writeByteToFile(fileBytes, "wwwfileloader/filebytes.gif");
    }

    public static byte[] readURLLikeBytes(String adress)
    {
        byte[] fileBytes = null;

        try
        {

            URL url = new URL(adress);
            URLConnection connection = url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(is);

            byte b;
            System.out.println( bufferedInputStream.available() );
            fileBytes = new byte[ bufferedInputStream.available() ];
            bufferedInputStream.read(fileBytes, 0, bufferedInputStream.available());
            bufferedInputStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return fileBytes;
    }

    public static void writeByteToFile(byte[] fileBytes, String fileName)
    {
        try
        {
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            fileOutputStream.write(fileBytes);
            fileOutputStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


}
Avatar billede =maddog= Nybegynder
25. april 2003 - 14:29 #1
available() does not give you the entire length of the file, but just the available buffer length of the stream. My guess is that your file is exactly double the size of that.
You will have to use a while(bufferedInputStream.read(fileBytes, 0, bufferedInputStream.available())!=-1) loop to exhaust the resources of the stream.
Avatar billede =maddog= Nybegynder
25. april 2003 - 14:34 #2
idealy you should write the bytes to the output stream immidiately so your buffer stays in use, and record the amount of bytes you read.
int bytesRead=0;
while ((bytesRead=bis.read(buffer,0,buffer.length))!=-!) {
  fos.write(buffer,0,bytesRead);
}
bis.close();
fos.flush();
fos.close();
/* where bis is the input stream and fos is the output stream */
Avatar billede =maddog= Nybegynder
25. april 2003 - 14:35 #3
=-!)      --->      =-1)
Avatar billede arne_v Ekspert
25. april 2003 - 14:50 #4
I guess that available() just returns the number of bytes
available in the low-level socket buffer.

There are no guarantee that it will return the size
of what is being retrieved.

It is not even likely that it return the size of what
is being retrieved for large files.

I do not even think you can assume that multiple calls
to available() will return the same number (new data can
arrive in between).

A fixed size buffer and maddogs loop is what you want.
Avatar billede fredand Forsker
25. april 2003 - 14:57 #5
Hello!

I changed to this, I guess that is what you mean?

package wwwfileloader;

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

public class WWWFileLoader
{
    public static void main(String[] args)
    {
        saveFile("http://www.google.com/logos/dna.gif", "wwwfileloader/filebytes.gif");
    }

    public static void saveFile(String adress, String fileName)
    {
        byte[] fileBytes = new byte[1000];

        try
        {

            URL url = new URL(adress);
            URLConnection connection = url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(is);

            FileOutputStream fileOutputStream = new FileOutputStream(fileName);

            System.out.println( bufferedInputStream.available() );

            int bytesRead = 0;
            while ( (bytesRead = bufferedInputStream.read(fileBytes, 0, fileBytes.length) ) != -1 )
            {
                fileOutputStream.write(fileBytes, 0, bytesRead);
            }
            bufferedInputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Best regards

F
Avatar billede fredand Forsker
25. april 2003 - 14:57 #6
...And it works of course!!
Avatar billede fredand Forsker
25. april 2003 - 15:01 #7
Buy the way you must give an answer so I can give you the points!

F
Avatar billede =maddog= Nybegynder
25. april 2003 - 15:04 #8
ok
Avatar billede fredand Forsker
25. april 2003 - 15:10 #9
Thanks!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester