假如你是读取远程主机上的文件,此文件不在服务器上,那么使用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();
无论以什么样的方式,请注意拷贝的地址不要写错了,或者有多余的空格之类。