jainesh
jainesh

Reputation: 71

File Copy from local to remote system using java IO

I need to copy a file from my local system to remote system, for this I'm using the following code:

    public class Autohost {

    public static void main(String[] args) throws IOException {
        InputStream in = new FileInputStream(new File(
                "C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war"));

        File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");
        f.createNewFile();
        OutputStream out = new FileOutputStream(f);
        // 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();


    }
}

But I'm getting the following error:

        Exception in thread "main" java.io.IOException: The system cannot find the path specified
        at java.io.WinNTFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(Unknown Source)
        at com.autohost2.java.Autohost.main(Autohost.java:18)

Upvotes: 2

Views: 9252

Answers (1)

Ben N
Ben N

Reputation: 2923

The filename on this line

File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");

is not a valid UNC path. You need two backslashes (four, in code) to signal a remote path. Fixed version:

File f = new File("\\\\10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");

Also make sure that security settings on the remote machine are configured to allow your account the appropriate access.

Upvotes: 3

Related Questions