Java Tutorial: How To Create a Program To Send And Receive Files On NetBeans

in #utopian-io6 years ago (edited)

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

  • then write the name of the project.
    image.png

  • then select create new class.
    image.png
  • 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.
  • classjava.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.
    image.png

  • then enter the location and name of the file you want to send.
    image.png
  • then run the second file the file Receive.java as before. - in this section write down localhost
    image.png

  • then enter the storage location and the new name of the file that was sent.
    image.png

  • then we look at the folder before, if the file has been received.
    image.png

    okay the file has been accepted

OKE TUTORIAL HAS DONE

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

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.

  • it would be better if you add more detail explanation of the code .Tutorials are what talking how to do instead of justing showing the effect .

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 61926.98
ETH 3060.91
USDT 1.00
SBD 3.79