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..

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

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

That code is not correct, I do not think it was actually ever finished. Try to use this

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.