What Will I Learn?
- You will learn how to create java program to send big file on NetBeans
- You will learn the workings of the application in sending and receiving a data
- You will learn how to connect two programs in one java package
Requirements
- you must understand the basics of java programming
- you should be able to operate classes in java according to its function
- NetBeans
Difficulty
- Intermediate
Tutorial Contents
okay i will give tutorial how to make program to send and receive big file with java using NetBeans. here we will create both programs in one package. i will create two program to send and recivied file here.
- first open NetBeans.
- then create a new project.
- then write the name of the project.
- then select create new class.
- then type the program script below.
package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Send {
public static void main(String[] args)throws IOException {
String namalokasifile= JOptionPane.showInputDialog("masukkan lokasi file"+"(e.g E:\\nama.format):");
int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
ServerSocket sock=new ServerSocket (Port);
JOptionPane.showMessageDialog(null,"server started");
JOptionPane.showMessageDialog(null,"waiting client");
Socket soc =sock.accept();
BufferedOutputStream bos = new BufferedOutputStream (soc.getOutputStream());
FileInputStream fis=new FileInputStream(namalokasifile);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer;
buffer = new byte [70022386];
while((n= bis.read(buffer))>-1)
{
bos.write(buffer,0,n);
System.out.println("bytes="+n);
bos.flush();}}}
- okay, I'll explain a bit about the script above.
- this is the package name of these two programs we will create in one package
package Send;
- this is the declaration of the classes we use in the program.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
- this is the declaration of the class name send
public class Send
writing class names must be capitalized, and should not use spaces. - this
Throws IOException
Is A Method That Reads String Data Input - this is a variable declaration process port
int Port =Integer.parseInt(JOptionPane.showInputDialog
with JOptionPane input method. - class
java.net.ServerSocket
used by Server to connections. - for data delivery process
sock.accept()
also called socket programing. - Class
BufferedInputStream
andBufferedOutputStream
can be used for reading and writing binary data from a file. - ini
byte[] buffer;
function To read a number of data bytes. - ini
System.out.println
serves to print the writing to the screen. - okay then we will create a second file that serves as the recipient of the file that is sent, we call it with Recivied.
- type the program under the same package as the previous program.
package file;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
public class ReciveBig {
public static void main(String[] args) throws IOException {
int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP= JOptionPane.showInputDialog("Input Your IP Server : ");
String save= JOptionPane.showInputDialog("masukkan tempat lokasi penyimpanan (e.g E:\\nama_file.Format_file) : ");
Socket sock=new Socket(IP,Port);
File file=new File(save);
BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int n;
byte[] buffer = new byte[70022386];
JOptionPane.showMessageDialog(null,"Connected");
while ((n = bis.read(buffer)) > -1) {
System.out.println("bytes="+n);
fos.write(buffer, 0, n);
// if(n<(1024)){
// fos.close();
// bis.close();
// break;
}
fos.flush();
}
JOptionPane.showMessageDialog(null,"File Received");
}}
- the second program is same as the first program, only different in some part, so I do not need to explain it again.
- we just run both programs.
- right click on the program Send.java then select run file.
- then enter the location and name of the file you want to send.
- then run the second file the file Receive.java as before. - in this section write down localhost
- then enter the storage location and the new name of the file that was sent.
- then we look at the folder before, if the file has been received.
okay the file has been accepted
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
This is a great post! I always wanted to get into jave. I'm going to actually try this out. Followed.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thank you so much sir
Hey @lapulga I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x