Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Writing to a local drive from the server

Posted on 2003-02-20
8
Medium Priority
?
221 Views
Last Modified: 2010-03-31
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
Comment
Question by:cincin2003
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7986562
 Can you elaborate a bit? You need to write data to a local drive (like c: or d:) in a file?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7987365
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
 

Author Comment

by:cincin2003
ID: 8015784
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 8015820
What server-side technology have you got?
0
 

Author Comment

by:cincin2003
ID: 8016001
I am using websphere 3.5
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 8016052
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
 

Author Comment

by:cincin2003
ID: 8016746
thank you for now but I found my problam and I am good for now thank's
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016787
Well you asked a question and i have gone to the trouble of providing a solution, so please accept the answer.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question