[英]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)
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. 不是有效的UNC路径。 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. 还要确保将远程计算机上的安全设置配置为允许您的帐户进行适当的访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.