利用cmd 命令 net use 來進行。
String remotepath="\\\\IP\\YPUpload";
String localpath="D:\\Lotus\\tmppic";
String userName="AXX";
String passWord="mXX";
///建立目錄
(new File(localpath)).mkdirs();
public void cmdnetcopyfile(Database db,Database dbattach,Vector djy,String login_user){
boolean Flag = false;
Process proc =null;
Runtime run =Runtime.getRuntime();
StringBuffer sb = new StringBuffer();
try
{
//Process proc=Runtime.getRuntime().exec("cmd.exe /c net use "+remotepath+" Zsd@2018 /user:Administrator ");
//Runtime.getRuntime().exec("cmd.exe /c net use "+remotepath+" Zsd@2018 /user:Administrator ");
//Runtime.getRuntime().exec("cmd.exe /c copy "+remotepath+"\\r002.jpg "+ localpath);
File rmt=new File(remotepath);
String cmdstr = "net use " + remotepath + " " + passWord + " /user:" + userName;
try{
proc=run.exec("cmd.exe /c "+cmdstr);
proc.waitFor();
}catch(Exception e){
e.printStackTrace();
try{
proc.getErrorStream().close();
proc.getInputStream().close();
proc.getOutputStream().close();
}catch(Exception ee){}
}
// BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()), 4096);
// String line = null;
// int i = 0;
// while ((line = br.readLine()) != null) {
// if (0 != i)
// sb.append("\r\n");
// i++;
// sb.append(line);
// }
// System.out.println(sb.toString());
File[] files=rmt.listFiles();
for(File f:files){
if(f.isDirectory()){
}else{
System.out.println("File:----"+f.getName()+"==="+f.getAbsolutePath());
File localfile=new File(localpath+File.separator+f.getName());
copyFile(f,localfile);
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
// proc.clone();
// proc.Dispose();
}
}
public void copyFile(File source,File dest) throws IOException{
InputStream input=null;
OutputStream output=null;
try{
input=new FileInputStream(source);
output=new FileOutputStream(dest);
byte[] buf=new byte[1024];
int bytesRead;
while((bytesRead=input.read(buf))!=-1){
output.write(buf,0,bytesRead);
}
}finally{
input.close();
output.close();
}
}