Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to transfer file between client to server in java socket?

  • Description:
    This project aims to create a simple Java application that enables users to transfer files over a socket connection. It provides a user-friendly interface for selecting a file and sending it to a server using socket communication.
    Steps:
Step 1
    Open the Project in IDE:
  • Launch your preferred Java IDE (e.g., Netbeans, Eclipse).
  • Open the project by selecting the project folder.
Step 2
    Create the Necessary project folder:
  • Create the project File_Tranfer.
  • Create the java class Client.java.
  • Create the java class Server.java.
  • Create the java class MyFile.java.
Step 3
    Create the Client side Code:
  • Import the necessary packages(Swing, awt, io, net.Socket).
  • Set the Frame, size of the frame and layout for client.
  • Set the Label, font of the label and Border for Title of the frame.
  • Create the panel for button and create the button for send one file.
  • Add buttons(send file and choose file) to the Panel.
Step 4
    Add ActionListener Property for ChooseFile:
  • Add JfileChooser property to choose one particular file.
  • Create a method actionPerformed to perform for choose one file.
Step 5
    Add ActionListener Property for SendFile:
  • Create a method actionPerformed to perform for send one file.
  • If the file is null to print choose the file.
  • else send the file to the server by using FileInputStream and DataOutputStream
  • Create the client side Socket by using localhost and port number(1234).
Step 6
    Add Everything to the Frame:
  • Add jlTitle to the frame.
  • Add jlFileName to the frame.
  • Add jpButton to the frame
  • Set the frame visible.
Step 7
    Create the Server side Code:
  • Import the necessary packages(Swing, awt, io, net.Socket, util).
  • Create the ArrayList for listing out the id, name, data, fileExtension.
  • Set the Frame, size of the frame and layout for server.
  • Set the panel and scrollPane vertically.
  • Set the label for jlTitle.
  • Set the Button for save the file.
  • Add all the property to the Frame.
  • Create the server socket by using port number(1234).
Step 8
    Create stream to receive data from the client through the socket:
  • DataInputStream method is used to read the input data.
  • If the file exists and do some operation for the byte data.
  • Add the new file to the array list which holds all our data.
  • Add ActionListener Property for SaveFile.
Step 9
    Add Everything to the Frame:
  • Add jlTitle to the frame.
  • Add jScrollPane to the frame.
  • Add jbSaveFile to the frame
  • Set the frame visible.
Step 10
    Create the MyFile Code:
  • Declaring the variables(id, name, data, fileExtension).
  • Creating the constructor for declaring object of the variables.
  • Set all the variables(setID, setName, setData, setFileExtension) as void.
  • get all the variables(getID, getName, getData, getFileExtension).
Step 11
    Run the Client Application and Server Application:
  • Run the main method in the Client Class.
  • Run the main method in the Server Class.
Sample Code
  • package com.mycompany.simple;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    public class Simple extends JFrame {
    private JList fileList;
    private DefaultListModel listModel;
    public Simple() {
    super("Client Side");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);
    listModel = new DefaultListModel<>();
    JScrollPane scrollPane = new JScrollPane(fileList);
    setLayout(new BorderLayout());
    username = rs.getString(1);
    add(scrollPane, BorderLayout.CENTER); loadLocalDrive();
    loadLocalDrive();
    setVisible(true);
    }
    private void loadLocalDrive() {
    File localDrive = new File("/home/user13/Downloads");
    if (localDrive.isDirectory()) {
    File[] files = localDrive.listFiles();
    if (files != null) {
    for (File file : files) {
    listModel.addElement(file.getName());
    }
    }
    }
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> new Simple());
    JFrame f = new JFrame();
    JPanel contentPanel = new JPanel(new BorderLayout());
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    contentPanel.add(scrollPane, BorderLayout.CENTER);
    JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton yourButton = new JButton("save");
    bottomPanel.add(yourButton);
    contentPanel.add(bottomPanel, BorderLayout.SOUTH);
    f.setContentPane(contentPanel);
    yourButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    textArea.append("Button Clicked!\n");
    }
    });
    f.setVisible(true);
    }
    }

Screen Shots
    Client.java:

  • Database connectivity using java for Oracle database

  • Microsoft ODBC for Oracle driver should be selected while setting DSN

  • Database Creation in Oracle 10g

  • Database connectivity

  • Oracle database in Java

  • Server.java:

  • Database connectivity using java for Oracle database

  • Microsoft ODBC for Oracle driver should be selected while setting DSN

  • Microsoft ODBC for Oracle driver should be selected while setting DSN