?
Solved

Writing to a local drive from the server

Posted on 2003-02-20
8
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 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