hi,
I am trying to write a server program that will handle multiple clients and receives a file from each client.
I need to save data from all the clients in a single file on the server.
I had written a program, but server each time rewrites the content of the file. Instead of rewriting i need the server to append to the file...
My server code is.....
Kindly help me with this.import java.io.*; import java.net.*; public class MultiServerThread_append implements Runnable { private Socket connection; private int ID; public static void main(String[] args) throws IOException{ try{ int count = 0; ServerSocket socket1 = new ServerSocket(8080); System.out.println("MultipleSocketServer Initialized"); while (true) { Socket connection = socket1.accept(); Runnable runnable = new MultiServerThread(connection, ++count); Thread thread = new Thread(runnable); thread.start(); } }catch(Exception e){} } MultiServerThread_append(Socket s, int i){ this.connection = s; this.ID = i; } public void run(){ try{ int filesize = 6022386; //filesize temporary int bytesRead; int current = 0; File clientInfo = new File("D:/filecopy/clientfile_copy.txt"); byte[] mybytearray = new byte[filesize]; InputStream is = connection.getInputStream(); FileWriter fstream = new FileWriter(clientInfo,true); BufferedWriter out = new BufferedWriter(fstream); bytesRead = is.read(mybytearray, 0 , mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); String str = new String(mybytearray); out.write(str, 0, current); out.flush(); out.close(); connection.close(); } catch(IOException e){ e.printStackTrace(); } } }
Thanks in advance....