CrazyEngineers
  • By using java how to listout sharing files in LAN...?

    ramana_slv

    Member

    Updated: Oct 26, 2024
    Views: 1.1K
    I want to know the all the sharing files when systems are connected in LAN......by using java program. Any one give me suggestion how it is possible.....?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • ramdane

    MemberMay 12, 2011

    please, give java code program, which list out us result, all sharing files in lan
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberMay 13, 2011

    May be there are some classes and interfaces by which you can do your work in easy way.
    But if you come to basics you can do it using rmi and socket programming.Just open your file streams on the network(from all the computers) using io package and store it in another where you want to display the files on the lan.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMay 14, 2011

    We can use a Client - Server script... Try this... Not mine, got it in the net...

    Server Class (Server.java)
    package transfer;
    
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server extends Thread {
        public static final int PORT = 3333;
        public static final int BUFFER_SIZE = 100;
    
        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(PORT);
    
                while (true) {
                    Socket s = serverSocket.accept();
                    saveFile(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void saveFile(Socket socket) throws Exception {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            FileOutputStream fos = null;
            byte [] buffer = new byte[BUFFER_SIZE];
    
            // 1. Read file name.
            Object o = ois.readObject();
    
            if (o instanceof String) {
                fos = new FileOutputStream(o.toString());
            } else {
                throwException("Something is wrong");
            }
    
            // 2. Read file to the end.
            Integer bytesRead = 0;
    
            do {
                o = ois.readObject();
    
                if (!(o instanceof Integer)) {
                    throwException("Something is wrong");
                }
    
                bytesRead = (Integer)o;
    
                o = ois.readObject();
    
                if (!(o instanceof byte[])) {
                    throwException("Something is wrong");
                }
    
                buffer = (byte[])o;
    
                // 3. Write data to output file.
                fos.write(buffer, 0, bytesRead);
            } while (bytesRead == BUFFER_SIZE);
    
            fos.close();
    
            ois.close();
            oos.close();
        }
    
        public static void throwException(String message) throws Exception {
            throw new Exception(message);
        }
    
        public static void main(String[] args) {
            new Server().start();
        }
    }
    Client Class (Client.java)
    package transfer;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.util.Arrays;
    
    public class Client {
        public static void main(String[] args) throws Exception {
            String fileName = null;
    
            try {
                fileName = args[0];
            } catch (Exception e) {
                System.out.println("Pass file name as command line argument");
            }
    
            File file = new File(fileName);
            Socket socket = new Socket("localhost", Server.PORT);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    
            oos.writeObject(file.getName());
    
            FileInputStream fis = new FileInputStream(file);
            byte [] buffer = new byte[Server.BUFFER_SIZE];
            Integer bytesRead = 0;
    
            while ((bytesRead = fis.read(buffer)) > 0) {
                oos.writeObject(bytesRead);
                oos.writeObject(Arrays.copyOf(buffer, buffer.length));
            }
    
            oos.close();
            ois.close();
        }
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register