假如你是讀取遠程主機上的文件,此文件不在服務器上,那麼使用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();
無論以什麼樣的方式,請注意拷貝的地址不要寫錯了,或者有多餘的空格之類。