public static void pullLogToResorce(File localFile) throws Exception{
SmbFile f;
//服务器上的log路径
String smbPath = "smb://" + Constant.LogServerIp;
UniAddress ua = UniAddress.getByName(Constant.LogServerIp);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(Constant.LogServerIp, "账号名", "密码"); //在java程序中访问windows有用户名和密码保护的共享目录
SmbSession.logon(ua, auth);//验证是否能够成功登录
try {
f = new SmbFile(smbPath, auth); //共享服务器
if(!f.exists()){
f.mkdirs();
}
//输入文件夹为压缩包
FileInputStream in = new FileInputStream(localFile);
//输出文件夹
SmbFile smbFileOut = new SmbFile(f + "/"+ localFile.getName() + ".zip",auth);
if(!smbFileOut.exists()){
smbFileOut.createNewFile();
}
SmbFileOutputStream out = new SmbFileOutputStream(smbFileOut); //输出的文件夹
long starttime = System.currentTimeMillis(); //开始时间
byte [] b = new byte[4096];
int n,tot = 0;
while((n = in.read(b)) > 0){
out.write(b,0,n);
tot += n;
}
long spendtime = System.currentTimeMillis() - starttime;
System.out.println("Log upload completed, " + tot + " bytes,in total" + "spend time" + spendtime);
in.close();
out.close();
} catch (Exception e){
e.printStackTrace();
throw new CommunicationException("Put File fail");
}
}
Linux环境下上传文件至服务器---JAVA
最新推荐文章于 2022-06-23 18:53:09 发布