473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

transfer a file between server and client

Hi, i kind of new to java and i want to transfer a file between a server and a client.
I know i have to use InputStream and OutputStream, but i don't know how to do it properly.
So far i think i've managed to do the client part. i read the content of a file, "hi.txt", and i send it to the server.

The problem is in the server part, i do not know how to write the file in the server. Thanks in advance for your help

Here is my code for the client.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.net.*;
  3. import java.io.*;
  4. public class client
  5. {
  6.  
  7.     public static void main(String arg[]) throws IOException
  8.     {
  9.  
  10.         InetAddress addr = InetAddress.getByName(null);//because i'm in my pc
  11.         System.out.println("addr = " + addr);
  12.         Socket socket = new Socket(addr, server.port);
  13.  
  14.         String source = "c:/hi.txt";
  15.         byte[] buf = new byte[10];//1 kilobyte buffer
  16.         int lentgth;
  17.  
  18.  
  19.         InputStream in = new BufferedInputStream(new FileInputStream(source));
  20.         OutputStream out = new BufferedOutputStream(socket.getOutputStream());
  21.  
  22.  
  23.         try
  24.         {
  25.  
  26.  
  27.  
  28.  
  29.             while((lentgth = in.read(buf))>0)
  30.                 {
  31.  
  32.                     out.write(buf,0,lentgth);//byte flow sent to the server
  33.                 }
  34.  
  35.  
  36.  
  37.  
  38.         }
  39.         finally
  40.         {
  41.             out.flush();
  42.             out.close();
  43.             in.close();
  44.             System.out.println("closing socket...");
  45.             socket.close();
  46.         }
  47.     }
  48. }
  49.  
  50.  

Server code

Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.io.*;
  3. public class server
  4. {
  5.     public static final int port = 8080;
  6.     public static void main(String arg[]) throws IOException
  7.     {
  8.  
  9.         ServerSocket s = new ServerSocket(port);
  10.         System.out.println("Started:  " + s);
  11.         byte[] buffer = new byte[10];
  12.  
  13.         try
  14.         {
  15.             Socket socket = s.accept();
  16.             System.out.println("Connection accepted: "+ socket);
  17.             //copy file  code here
  18.  
  19.  
  20.  
  21.                 try
  22.                 {
  23.                 InputStream in = new BufferedInputStream(socket.getInputStream());
  24.  
  25.  
  26.                 while(true)// loop to copy the file
  27.                 {
  28.  
  29.  
  30.                 }
  31.  
  32.                 }
  33.                 finally
  34.                 {
  35.                     socket.close();    
  36.                     System.out.println("closing socket...");
  37.                 }    
  38.         }
  39.         finally
  40.         {
  41.             s.close();
  42.             System.out.println("closing Server socket...");
  43.         }
  44.     }
  45. }
  46.  
Apr 23 '09 #1
1 21368
chaarmann
785 Expert 512MB
You used a FileInputStream to read the dta from file on the client, so you can use a FileOutputStream to write the data as file on the server.

By the way,you have an endless "while(true)" loop in your code! Obviously you should get the data from the socket there and store it as a file.
Apr 24 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ian maclure | last post by:
I'm writing a client-server app. Client controls Server which in turn configures and controls a bunch of hardware. I want to be able to start the server from my client. Now in C/C++ one could...
1
by: JT | last post by:
i'm using a model, view, control architecture for a group of .asp pages. i can't decide whether to use Server.Transfer or Server.Execute to pass posted data from my control to my model. are...
3
by: MBW | last post by:
The following code is for a simple server/client asplication that allows the user to toggle between serve and or client modes and send/recieve a message however i am getting an Attribute error on...
4
by: ewolfman | last post by:
Hi, This may sound a little complicated, but here goes: I have 2 webforms (lets call them 'x' and 'y'). * - 'x' is a form which contains a single server side TextBox web control, and an...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
6
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
4
by: Mark Olbert | last post by:
I'm looking for help in getting the Transfer Sql Server Objects task to work. The goal is to transfer the tables, views, sprocs, udfs from a SqlServer 2000 database to a Sql Server 2005 database. ...
9
by: TC | last post by:
Like a lot of database developers, I often choose Jet for the back- end. I'm starting to worry about what will happen when Jet is deprecated. Ostensibly, Jet users like me must switch to SQL Server...
0
by: ankuragt | last post by:
(problem in writing a string into a file on client side.) hey i have written a code of server and client.what i want to do is to transfer the contents of file on server side to the client side (line...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.