October 19, 2014

Sending File from System to System in java

How to send a file from one system to another using java code

Description :The program will help you to send a file from one computer to another in network through a socket. the port number here used is 8025 (we eliminate all 1024 reserved ports, for example 21 are reserved port for ftp).  developer can declare the port number as static final. The server application will send a file specified by the user through socket where the client application receives it. please provide feedback after running the code, check the java source code.This is an example for java networking application.



CLIENT SERVER
SENDING IMAGE FROM CLIENT TO SERVER IN JAVA



FTPServer.java

/**
 *
 */
package pakistan;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author javabelazy
 *
 */
public class FTPServer {
  
    ServerSocket serverSocket = null;
    Socket socket = null;
    int data = -1;
    DataOutputStream dataOutputStream = null;
    OutputStream outputStreams = null;
    DataInputStream dataInputStream = new DataInputStream(System.in);
    String fileNameNew = null;
    String string = null;
    File file = null;
    FileInputStream fileInputStream = null;
  

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FTPServer ftpServer = new FTPServer();
        ftpServer.sendFile();

    }

    private void sendFile() {
        // TODO Auto-generated method stub
        string = new String();
        try{
            //d = new DataInputStream(System.in);
            System.out.println(" Enter the file name to be transfered ");
            fileNameNew=dataInputStream.readLine();
            file = new File(fileNameNew);
            fileInputStream = new FileInputStream(file);
            serverSocket= new ServerSocket(8025);
            socket=serverSocket.accept();
            outputStreams=socket.getOutputStream();
            dataOutputStream=new DataOutputStream(outputStreams);
            while((data=fileInputStream.read())!=-1){
                char c = (char)data;
                string=string+c;
            }
            dataOutputStream.writeUTF(string);
            System.out.println("The file "+fileNameNew +" file transfered to client machine");

        }
        catch (Exception e) {
            // TODO: handle exception
            System.out.println(" The server system got an exception : "+e.getMessage());
        }
        finally{
            try {
                socket.close();
                serverSocket.close();
                outputStreams.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println(" Unable to send file (file transfer got the following exception ) : "+e.getMessage());
                e.printStackTrace();
            }

          
        }
      
    }

}


FTPClient.java

/**
 *
 */
package china;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @author java belazy
 *
 */
public class FTPClient {
  
    Socket socket =  null;
    int len = 0;
    int count = 0;
    InputStream inputStream = null;
    DataInputStream dataInputStream = null;
    DataInputStream newDataInputStream = null;
    String fileName = null;
    String fileData = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FTPClient ftpClient = new FTPClient();
        ftpClient.receiveFile();

    }

    private void receiveFile() {
        // receiving file sent by host system
        while(true){
        try{
            socket=new Socket("", 8025);
            inputStream = socket.getInputStream();
            newDataInputStream=new DataInputStream(inputStream);
            System.out.println(" Enter the file name to be saved ");
            dataInputStream = new DataInputStream(System.in);
            fileName = dataInputStream.readLine();
            File file = new File(fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileData =newDataInputStream.readUTF();
            len= fileData.length();
            for(count=0;count<len;count++){
                char c =fileData.charAt(count);
                fileOutputStream.write(c);
            }
            System.out.println("File copied to system ");

                   
        }catch (Exception e) {
            // TODO: handle exception
            System.out.println(" The file tranfer client got an exception : "+e.getMessage());
        }finally{
            try {
                socket.close();
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println(" The file tranfer client got an exception : "+e.getMessage());
                e.printStackTrace();
            }
           
           
        }
       
        }
       
    }

}
 

file transfer protocol implementation in java

Facebook comments