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