有关于对于远程主机文件的拷贝

假如你是读取远程主机上的文件,此文件不在服务器上,那么使用java执行cmd通过共享文件夹的方式拷贝文件比较合适

代码如下:

Process process = Runtime.getRuntime().exec("net use \\192.168.10.243\\IPC$ '1qaz!QAZ'/user:'EWELL'");
			String path = "xcopy \\\\192.168.10.243\\pdf\\jpg.pdf "+dstPath;
			System.out.println("path:"+path);
			process = Runtime.getRuntime().exec(path);

而如果你是拷贝远程服务器上的文件的话,使用httpClient和httpUrlConnection比较合适

String strUrl = "http://192.168.10.243/pdfpdf/jpg.pdf";
			URL url = new URL(strUrl);
			HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
			String showCopyPath =dstPath+ strUrl.substring(strUrl.lastIndexOf("/"));
			urlCon.setConnectTimeout(5000);
			urlCon.setReadTimeout(5000);
			
			showPath = "../patientHologram/pdfShow"+ strUrl.substring(strUrl.lastIndexOf("/"));
		
			FileInputStream fis = (FileInputStream) urlCon.getInputStream();
			FileOutputStream fos = new FileOutputStream(showCopyPath);
			
			int length = 0;
			byte[] buffer = new byte[1024]; // 一字节缓冲
			while((length=fis.read(buffer)) != -1){
			    fos.write(buffer, 0, length);
			}
			fis.close();
			fos.close();*/
			/*urlCon.setConnectTimeout(5000);
			urlCon.setReadTimeout(5000);*/
			/*BufferedReader in = new BufferedReader(new InputStreamReader(fis));
			BufferedWriter bw = new BufferedWriter(new FileWriter(showCopyPath));
			String inputLine = "";
			while((inputLine = in.readLine() )!= null){
				bw.write(inputLine);
			}
			in.close();
			bw.close();

无论以什么样的方式,请注意拷贝的地址不要写错了,或者有多余的空格之类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值