Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: File transfer from Client to Server side

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File transfer from Client to Server side

    Hello , I am new to this forum. Please guide me with the following problem. I have Server and Client java files. Client sends a file to Server, Server receives it and saves in the present working directory of Server java file. How to change the target directory in Server from current working directory? Here is my code :

    Server.java

    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 = 3332;  
        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);  
     
            System.out.println("File transfer success");  
     
            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.java

    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.ObjectInputStream;  
    import java.io.ObjectOutputStream;  
    import java.net.Socket;  
    import java.util.Arrays;  
    import java.lang.*;  
    import java.util.Scanner;  
     
     
    public class Client {  
        public static void main(String[] args) throws Exception {  
            String fileName = null;  
     
           try {  
                fileName = args[0];  
            } catch (Exception e) {  
            System.out.println("Enter the name of the file :");  
            Scanner scanner = new Scanner(System.in);  
            String file_name = scanner.nextLine();  
     
            File file = new File(file_name);  
            Socket socket = new Socket("localhost", 3332);  
            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();  
            System.exit(0);      
    }  
     
    }  
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: File transfer from Client to Server side

    Also posted at File transfer from Client to Server side - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Server Client does not send file
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 12:38 PM
  2. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM
  3. Validate in server side..
    By Ganezan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2009, 06:36 AM
  4. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM
  5. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM