Can any one help me with sample to code to place a file in a remote machine:)
Can any one help me with sample to code to place a file in a remote machine:)
What exactly do you mean by placing a file, do you want to send a file from one machine to another?
yep can u please send the sample code for it
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.
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
Sorry about that, will keep this in mind for future.
Sure ... no problem... and thanks for understanding... the forum is here to help people with their Java programming problems and questions...
CJSL
thanks dude :)
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...
Use a JFileChooser for file selection.
Erm, this thread is long dead; there's no need to reply to it again. I'm closing this thread.
kind regards,
Jos