This discussion is archived
10 Replies Latest reply: Mar 6, 2012 3:56 PM by EJP RSS

Copy file to a remote host

921967 Newbie
Currently Being Moderated
Hi,

Iam trying to copy a local file from my windows machine to a remote windows server by executing the copy command through the Runtime execution by using the Process p=Runtime.getRuntime().exec() and I have achieved it successfully, but it is running successfully only when I give the host name of the remote server and it is failing when I give the IP Address by displaying the following error:
Logon failure

So, here Iam in a confusion on how to pass credentials for that remote host so that I can execute the copy command with the IP address.

Can anyone help me on this?
  • 1. Re: Copy file to a remote host
    EJP Guru
    Currently Being Moderated
    Can anyone help me on this?
    It's hard to see how they could. You are asking what parameters to supply to an external program, which you haven't even named, and the question has basically nothing to do with Java anyway.

    Try the documentation for the program you are running.
  • 2. Re: Copy file to a remote host
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    Can anyone help me on this?
    You are asking what parameters to supply to an external program, which you haven't even named,
    (the command prompt copy command).

    Executing command prompt things is easy to test - try it in a command prompt first. Any command that works there will also work through Runtime.exec(), if you follow the wisdom that is within this article:

    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html


    But I have a question: why invoke a command prompt thing when you can just as easily use Java's IO APIs to do it in a cross-platform clean way?
  • 3. Re: Copy file to a remote host
    EJP Guru
    Currently Being Moderated
    Actually I don't agree with that for once. Copying files to a server is a system-dependent operation that may as well be completely externalized.

    Personally I view any application that needs to copy files anywhere with grave suspicion.
  • 4. Re: Copy file to a remote host
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    Personally I view any application that needs to copy files anywhere with grave suspicion.
    Well the automation software I build as part of our ETL processes do a lot of that work, although that tends to be SFTP stuff :)

    But for local apps - yeah it would be a little suspect.
  • 5. Re: Copy file to a remote host
    EJP Guru
    Currently Being Moderated
    What's ETL?
  • 6. Re: Copy file to a remote host
    gimbal2 Guru
    Currently Being Moderated
    Whoops, that might be a language thing. checks.

    Nope, its not. "Extraction, Transformation and Load".

    http://en.wikipedia.org/wiki/Extract,_transform,_load

    In other words, taking data from an external source (often file feeds), staging that data, storing the data in an operational data store and then syncing all mutations to a data warehouse. After that the data may be spread to one or more datamarts and to data exports in a controlled manner. Boring as hell, but once you have such a thing up and running at least you can answer any kind of answer about data at any time, especially because the DWH in between keeps track of the entire history of a record.
  • 7. Re: Copy file to a remote host
    EJP Guru
    Currently Being Moderated
    Not really an application, eh?
  • 8. Re: Copy file to a remote host
    gimbal2 Guru
    Currently Being Moderated
    ETL isn't, but the epic stuff I create to make it happen is ;)

    How did we end up here? Oh yeah, I derailed the thread.
  • 9. Re: Copy file to a remote host
    921967 Newbie
    Currently Being Moderated
    Hi Iam facing problem when I mention the IP Address of the host because it is showing "Access is denied", so my question is how to pass the username and password of the host in order to copy the file.
    InputStream in = new FileInputStream(new File("C:\\test.txt"));
    OutputStream out = new FileOutputStream(new File("\\\\x.x.x.x\\temp\\test.txt"));

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    in.close();
    out.close();
  • 10. Re: Copy file to a remote host
    EJP Guru
    Currently Being Moderated
    my question is how to pass the username and password of the host
    There is no way to do that in Java.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points