Jaeh01
Social Presence Architect
LEVEL 1
100 XP
This is a tutorial in which we will going to create a program that can download an image file or any files in a website using Java.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of imgFromURL.java.
2. Import the following packages:
3. Initialize your variable in your Main, variable p for byte data type which will save memory in a large array since this data type is 4 times smaller than int data type and variable url for the URL class that will open the world wide web in a specific website as instantiated.
We access an image file as we have a png extension file from the sourcecodester site.
Now create a variable named urlConnect for the URLConnection class that we will open the connection using the openConnection method of the variable url above. And with the urlConnect variable, have its connect method to perform connectivity to the site. But take note that we will need the internet on this to download the file.
3. Create now a variable di for our DataInputStream class to get the the inputted url from the URLConnection class.
Now, to get the file we will use the FileOutputStream and have it instantiated with the name of the file to be saved. Have this code below:
4. To download the file, have this code below using the FileOutputStream, DataInputStream variable and the byte variable, then close the di and fo variable for streaming.
Output:
Here's the full code of this tutorial:
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of imgFromURL.java.
2. Import the following packages:
- import
java.io.*
;
//used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
- import
java.net.*
;
//used to access the URL and URL connection class to access websites
3. Initialize your variable in your Main, variable p for byte data type which will save memory in a large array since this data type is 4 times smaller than int data type and variable url for the URL class that will open the world wide web in a specific website as instantiated.
- byte
[
]
b =
new
byte
[
1
]
;
- URL
url =
new
URL
(
"http://www.sourcecodester.com/sites/default/files/print_0.png"
)
;
We access an image file as we have a png extension file from the sourcecodester site.
Now create a variable named urlConnect for the URLConnection class that we will open the connection using the openConnection method of the variable url above. And with the urlConnect variable, have its connect method to perform connectivity to the site. But take note that we will need the internet on this to download the file.
- URLConnection
urlConnect =
url.openConnection
(
)
;
- urlConnect.connect
(
)
;
3. Create now a variable di for our DataInputStream class to get the the inputted url from the URLConnection class.
- DataInputStream
di =
new
DataInputStream
(
urlConnect.getInputStream
(
)
)
;
Now, to get the file we will use the FileOutputStream and have it instantiated with the name of the file to be saved. Have this code below:
- FileOutputStream
fo =
new
FileOutputStream
(
"print_0.png"
)
;
4. To download the file, have this code below using the FileOutputStream, DataInputStream variable and the byte variable, then close the di and fo variable for streaming.
- while
(
-
1
!=
di.read
(
b, 0
, 1
)
)
- fo.write
(
b, 0
, 1
)
;
- di.close
(
)
;
- fo.close
(
)
;
Output:

Here's the full code of this tutorial:
- import
java.io.*
;
//used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
- import
java.net.*
;
//used to access the URL and URL connection class to access websites
- public
class
imgFromURL {
- public
static
void
main(
String
args[
]
)
throws
Exception
{
- byte
[
]
b =
new
byte
[
1
]
;
//data type to save memory space of the file
- URL
url =
new
URL
(
"http://www.sourcecodester.com/sites/default/files/print_0.png"
)
;
// url of the site that we will going to download the image file
- URLConnection
urlConnect =
url.openConnection
(
)
;
- urlConnect.connect
(
)
;
// get the connection to the site
- DataInputStream
di =
new
DataInputStream
(
urlConnect.getInputStream
(
)
)
;
//have the input to the connection
- FileOutputStream
fo =
new
FileOutputStream
(
"print_0.png"
)
;
//download the file and have it named print_0.png
- while
(
-
1
!=
di.read
(
b, 0
, 1
)
)
//download the image file
- fo.write
(
b, 0
, 1
)
;
- di.close
(
)
;
// close DataInputStream
- fo.close
(
)
;
// close FileOutputStream
- }
- }
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
You must upgrade your account or reply in the thread to view the hidden content.