Results 1 to 11 of 11
  1. #1
    aradhya is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default how to place a file in remote machine using simple java class

    Can any one help me with sample to code to place a file in a remote machine:)

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    What exactly do you mean by placing a file, do you want to send a file from one machine to another?

  3. #3
    aradhya is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default yep

    yep can u please send the sample code for it

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    //FileSender.java
    import java.net.*;
    import java.io.*;
    
    public class FileSender{
    	public static void main(String[] arg){
    		try{
    			FileInputStream fis=new FileInputStream("FileSender.java");
    			/*creating a socket to send data to 192.168.1.123
    			  i.e. IP address of the machine where the file is to be sent
    			  and 1000 is the port number on which server listens  requests*/
    			Socket socket=new Socket("192.168.1.123",1000);
    			OutputStream os=socket.getOutputStream();
    			int ch=0;
    			System.out.println("Sending file FileSender.java to 192.168.1.123");
    			while(true){
    
    				ch=fis.read();
    				if(ch==-1)
    				  break;
    				os.write(ch);
    			}
    			fis.close();
    			os.close();
    			System.out.println("Sending process completed");
    		}
    
    		catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    
    //FileReceiver.java
    ------------------
    import java.net.*;
    import java.io.*;
    
    public class FileReceiver{
    	public static void main(String[] arg){
    		try{
    			//we are creating the socket on port 1000
    			ServerSocket server=new ServerSocket(1000);
    			System.out.println("Server running...");
    			Socket socket=server.accept();
    			InputStream is=socket.getInputStream();
    			FileOutputStream fos=new FileOutputStream("d:\\a1.txt");
    			int ch=0;
    			System.out.println("Storing received contents to d:\\a1.txt");
    			while(true){
    				ch=is.read();
    				if(ch==-1)
    				  break;
    				fos.write(ch);
    			}
    			is.close();
    			fos.close();
    			System.out.println("Receive process completed");
    		}
    
    		catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }

    Compile both the codes , first execute FileReceiver then execute FileSender.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default teach the OPs...

    dswastik... it's propobably not a good idea to send complete solutions to the OPs... there's no learning process envolved ... to easy for the OP to just cut & paste & forget. It better to provide snippets, pseudo code, hints, links, etc.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Sorry about that, will keep this in mind for future.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Sure ... no problem... and thanks for understanding... the forum is here to help people with their Java programming problems and questions...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    aradhya is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    thanks dude :)

  9. #9
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    What if you do not know the name of the file before hand? for example i may want to fill in that file name with whatever a drop down menu provides...

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Use a JFileChooser for file selection.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to place a file in remote machine using simple java class

    Erm, this thread is long dead; there's no need to reply to it again. I'm closing this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. excecuting a jar file by calling a java class
    By Lavanya.vitria in forum Advanced Java
    Replies: 1
    Last Post: 12-13-2008, 12:11 PM
  2. remote file access problem
    By kib_tse in forum New To Java
    Replies: 2
    Last Post: 08-14-2008, 06:04 PM
  3. How to read a .EML file present on Remote Desktop?
    By anil@prokarma in forum New To Java
    Replies: 0
    Last Post: 06-12-2008, 02:05 PM
  4. Applet and remote file
    By Preethi in forum Java Applets
    Replies: 0
    Last Post: 06-03-2008, 05:54 AM
  5. how to load a file in remote machine
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •