I need a java code for downloading files from the internet ..For example I want to download doc,pdf files from the internet means i have to do it through my code ..So can anyone help me

Wow. I'd been wondering about that too...
Thank you abar_sow for asking the question and Ezzaral for answering it.:icon_smile:

Here is the code which i compiled ...

import java.io.*;
import java.net.*;
public class SampleFiledownload
{
public static void main(String args[])
{


java.io.BufferedInputStream in = new java.io.BufferedInputStream(new


java.net.URL("url path").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("file name");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
while(int count = in.read(data,0,1024) != null)
{
bout.write(data,0,count);
}
bout.close();
out.close();
in.close();
}
}

Its showing the following errors:

C:\Program Files\Java\jdk1.5.0\bin>javac SampleFiledownload.java
SampleFiledownload.java:12: '.class' expected
while(int count = in.read(data,0,1024) != null)
^
SampleFiledownload.java:16: ')' expected
bout.close();
^
2 errors

I tried my level best to correct the error.But i Cant....Anyone help..

Edited 3 Years Ago by happygeek: fixed formatting

You cannot declare a variable in a while condition, which you are attempting with the "int count" part. Change this to

int count;
while( (count = in.read(data,0,1024)) != -1)

instead. Notice the change from != null to != -1 also. The read method returns -1 at the end of the stream, not null.

Can anyone locate the error pls...
Code:

import java.io.*;
import java.net.*;
public class SampleFiledownload
{
public static void main(String args[])
{
int count;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new


java.net.URL("url path").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("file name");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
while(count = (in.read(data,0,1024) != null))
{
bout.write(data,0,count);
}
bout.close();
//out.close();
in.close();
}
}

Compilation:

C:\Program Files\Java\jdk1.5.0\bin>javac SampleFiledownload.java
SampleFiledownload.java:12: incomparable types: int and <nulltype>
while(count = (in.read(data,0,1024) != null))
^
SampleFiledownload.java:12: incompatible types
found   : int
required: boolean
while(count = (in.read(data,0,1024) != null))
^
2 errors

Edited 3 Years Ago by happygeek: fixed formatting

hi guys...my problem solved here is the code .Surely it ll be useful for u all...

import java.io.*;
import java.net.*;
public class SampleFile
{
public static void main(String args[]) throws IOException
{

java.io.BufferedInputStream in = new java.io.BufferedInputStream(new

java.net.URL("http://bdonline.sqe.com/documents/testplans.pdf").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("testplans.pdf");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
while(in.read(data,0,1024)>=0)
{
bout.write(data);
}
bout.close();
in.close();
}
}

I tried the code below with a pdf file which contains formulas and which is compiled from MikTex. It doesn't work in this case. A pdf file is saved, but; the only thing I can see is plus, minus, equal and similar signs; that is, no letters och digits. Do anyone of you know what could be wrong?

Thanks.

hi guys...my problem solved here is the code .Surely it ll be useful for u all...

import java.io.*;
import java.net.*;
public class SampleFile
{
public static void main(String args[]) throws IOException
{

java.io.BufferedInputStream in = new java.io.BufferedInputStream(new

java.net.URL("http://bdonline.sqe.com/documents/testplans.pdf").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("testplans.pdf");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
while(in.read(data,0,1024)>=0)
{
bout.write(data);
}
bout.close();
in.close();
}
}

This earlier posts are close, but I needed to change a few things. Sometimes you don't get a full 1024 bytes. Here is the updated version:

import java.io.*;
import java.net.*;
public class SampleFile
{
public static void main(String args[]) throws IOException
{

java.io.BufferedInputStream in = new java.io.BufferedInputStream(new 

java.net.URL("http://bdonline.sqe.com/documents/testplans.pdf").openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("testplans.pdf");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int x=0;
while((x=in.read(data,0,1024))>=0)
{
bout.write(data,0,x);
}
bout.close();
in.close();
}
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

I tried the code below with a pdf file which contains formulas and which is compiled from MikTex. It doesn't work in this case. A pdf file is saved, but; the only thing I can see is plus, minus, equal and similar signs; that is, no letters och digits. Do anyone of you know what could be wrong?

Thanks.

How to give login credentials for downloading pdf files

How to give login credentials for downloading pdf files

Please create new thread instead of bumping old one.

Thread closed.

This question has already been answered. Start a new discussion instead.