/**
* 将文件上传到服务器共享文件夹
* @param remoteUrl smb://xxxx.xx.xx.xx/plmtomesfile/
* @param localFilePath C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\作业指导书_test.xlsx
* @param ip 服务器IP
* @param username 登陆用户
* @param password 密码
*/
public static void upload(String remoteUrl, String localFilePath,String ip,String username,String password) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
File localFile = new File(localFilePath);
inputStream = new FileInputStream(localFile);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(ip, username, password); //先登录验证
SmbFile smbFile = new SmbFile(remoteUrl+"/"+localFile.getName(),auth);
smbFile.connect();
outputStream = new SmbFileOutputStream(smbFile);
byte[] buffer = new byte[4096];
int len = 0; // 读取长度
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
outputStream.write(buffer, 0, len);
}
// 刷新缓冲的输出流
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}