Writing to a local drive from the server

Hi
I am writing a web application and I need to write a new file to a local drive(on the client computer drive c: or D:) from server.
Please let me know how it can be done.
Thanks
cincin2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 Can you elaborate a bit? You need to write data to a local drive (like c: or d:) in a file?
0
CEHJCommented:
If you mean you want to implement a server-side 'push' to a drive on an arbitrary machine, that machine will have to have a listening Socket. If that's what you want, i've probably got a bit of code you can use.
0
cincin2003Author Commented:
Hi
I am writing a web application and I need to write a new file to a local drive(on the client computer drive c: or D:) from server.
Please let me know how it can be done.
Thanks
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJCommented:
What server-side technology have you got?
0
cincin2003Author Commented:
I am using websphere 3.5
0
CEHJCommented:
You can use these classes with a bit of minor tweaking. Create a new thread to start the BinaryServer on the client machine(s). Then make the servlet connect to port 2048 using the client's address and write the file. Let me know if it works for you:

class BinaryServer implements Runnable {
  ServerSocket ssock;
  Socket socket;
  int port = 2048;
  InputStream sockIn;
  FileOutputStream fileOut;

  public void run() {
    try {
      ssock = new ServerSocket(port);
      System.out.println("Waiting for client to connect...");
      for (;;) {
        new FileReceiver(ssock.accept());
        System.out.println("Client has connected!");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class FileReceiver implements Runnable {
  /**
   * A self-running class that handles
   * receiving a file transmission
   */
  Socket socket;
  InputStream in;
  OutputStream out;
  long bytesReceived;

  public FileReceiver(Socket socket) {
    this.socket = socket;
    new Thread(this).start();
  }

  String getSavePath() {
    return "" + (int)(Math.random() * 10000) + "_notepad.exe";
  }

  public void run() {
    /**
     * Get an input stream on the socket,
     * and write the file to an output
     * stream
     */
    try {
      in = socket.getInputStream();
      int buf;
      out = new FileOutputStream(getSavePath());
      System.out.println("FileReceiver receiving file:");
      while ((buf = in.read()) > -1) {
        doOutput(buf);
      }

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (in != null) {
        try { in.close(); } catch(IOException e) { e.printStackTrace(); }
      }
      if (out != null) {
        System.out.println("\nFile reception completed (unverified)");
        System.out.println("Listening for next file...");
        try { out.close(); socket.close(); } catch(IOException e) { e.printStackTrace(); }
      }
    }
  }

  public void doOutput(int buf) {
    /**
     * Loose couples the reception from
     * the loop in which it happens
     */
    try {
      out.write(buf);
      bytesReceived++;
      if (bytesReceived % 1024 == 0)
      System.out.print("*");
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cincin2003Author Commented:
thank you for now but I found my problam and I am good for now thank's
0
CEHJCommented:
Well you asked a question and i have gone to the trouble of providing a solution, so please accept the answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.