Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how can i send a file(urgent)  RSS feed

 
Naveed Anjum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am executing a client server programme on a local PC and i want to send a file from server to client.For example i have a file on PC in directory c:\myfiles\naveed.txt i want to send this file from server to client.So plz tell me which command i will use.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common way to do this is via sockets. Sun has a good tutorial HERE that should get you started. A bit of advice:
Don't try to write a couple hundred lines of code and make them run. Tackle this in very small chunks.

*Connect first.
*Send a string from one machine to the other.
*Open a file and read it
*Open a file and write it
*Send a line at a time
*etc ...
If you keep these little pieces nicely separated from each other, you can easily post running code (or not quite running code) here for more tips. Hope that helps!
 
Naveed Anjum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all thankz for ur reply actually i donot want to read or write file i just want that client send a request for a file to server and server then send this file to client with out open this file and the file size is 1 MB.i have both programmes for client and server........
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very vague request. Sounds like you should just run a web server on the server side then use a web browser on the client side. Or, you could it via FTP. We need more details ot give you a good answer. Are you looking to run a java server you write on the server machine and a java client you write on the client machine?
Brian
 
aarhus arhus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am running these two client server programmes on local machine.Actually i want to send a request from client that send me(file name) file so server will send this file to client without open it.File is on local machine and the size of file is 1 MB.So plz update both code for this requirment.Thankz in advance
thats my client programme
------------------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.*;
public class Client2{
public static void main(String[] args){
String server="localhost";
int port=80;
try{
Socket socket=new Socket(server,port);
BufferedReader inputStream=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter outputStream=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
outputStream.println("I need a file named atif.txt");
System.out.println(inputStream.readLine());
socket.close();
}catch(ConnectException e){System.out.println(e);}
catch(UnknownHostException e){System.out.println(e);}
catch(IOException e){System.out.println(e);}
}
}
------------------------------------------------------------------
thats my server programme
--------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.*;
public class Server2 {
public static void main(String[] args) {
Socket client;
PrintWriter outputStream;
BufferedReader inputStream;
try
{
ServerSocket server = new ServerSocket(80);
System.out.println("Server listening on port 80");
client = server.accept();
inputStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputStream = new PrintWriter(client.getOutputStream(),true);
while(true)
{
String request = inputStream.readLine();
System.out.println("Client says:" + request);
String reply = "Ya i have this file";
outputStream.println(reply );
DataInputStream input=new DataInputStream(new FileInputStream("atif.txt"));
String s=input.toString();
outputStream.println(s);
client.close();
}
}catch(IOException e)
{
System.out.println(e);
}
}
}
----------------------------------------------------------------
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't send a file without reading it, regardless of what program you run, ftp, http, anything. You cna use binary or text format depends on your need.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!