954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to download a file

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

abar_sow
Light Poster
36 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This is accomplished with sockets via http. See if this helps:
http://www2.sys-con.com/itsg/virtualcd/Java/archives/0204/dibella/index.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

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

abar_sow
Light Poster
36 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

abar_sow
Light Poster
36 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

abar_sow
Light Poster
36 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

malin890423
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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();
}
}
bs123
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

dcholan
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
How to give login credentials for downloading pdf files


Please create new thread instead of bumping old one.

Thread closed.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You