2 回答

TA貢獻1827條經驗 獲得超4個贊
copyTo(SmbFile)和之間有一個有趣的區別renameTo(SmbFile)- 只有其中一個說此文件和目標文件不需要在同一主機上。由于renameTo(SmbFile)它沒有說,我只能假設,你應該使用copyTo,然后delete()將原來的。
SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth);
SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth);
// smbFromFile.renameTo(smbToFile);
smbFromFile.copyTo(smbToFile);
smbFromFile.delete();

TA貢獻1866條經驗 獲得超5個贊
有兩種可能的場景:
1.)文件需要移動到同一臺服務器上(即輸入文件夾和輸出文件夾的認證細節是相同的)。
使用 renameTo() 方法。
public boolean moveFile(SmbFile file) {
log.info("{"Started Archiving or Moving the file");
String targetFilePath = this.archiveDir + file.getName(); //Path where we need to move that file.
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", userId, userPassword);
log.info("targetFilePath: {} , currentFile : {}",targetFilePath, file);
SmbFile targetFile = new SmbFile(targetFilePath, auth);
//authenticate the SmbFile
try {
file.renameTo(targetFile); //User renameTo method for same server
log.info("Archived File : {} to: {}", file.getName(),
targetFile.getName());
return true;
} catch (SmbException e) {
log.error("Unable to Archive File: {}", file.getName());
return false;
}
} catch (MalformedURLException e) {
log.error("Connection failed to Server Drive: {}", targetFilePath);
}
return false;
}
2.)文件需要在不同的服務器上移動(即輸入文件夾和輸出文件夾的身份驗證詳細信息 不相同)。
使用 copyTo() 方法。
在這里,我建議,您可以首先驗證存在文件的第一臺服務器,并檢查文件是否存在,如果存在,則將其添加到列表中:
public List<SmbFile> xmlFiles = new ArrayList<>(); //Here we will add all the files which are existing.
public boolean isFileExists() throws MalformedURLException, SmbException {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
userID, userPassword); //authenticating input folder.
SmbFile smbFile = new SmbFile(inputFolder, auth);
SmbFile[] smbFiles = smbFile.listFiles();
boolean isFilePresent = false;
if (smbFiles.length > 0) {
for (SmbFile file : smbFiles) {
if (file.getName().toLowerCase(Locale.ENGLISH)
.contains(AppConstant.FILE_NAME.toLowerCase(Locale.ENGLISH))) {
xmlFiles.add(file);
isFilePresent = true;
}
}
}
if (isPlanFilePresent) {
log.info("Number of files present on Server: {}",smbFiles.length);
return true;
}
return false;
}
這將為您提供列表中的文件。繼續將其復制到另一臺服務器。請注意,您只需要在此處對輸出文件夾進行身份驗證。
public boolean moveFile(SmbFile file) {
log.info("Started Moving or Archiving the file");
String toFilePath = this.outputFolder + file.getName(); //path where you need to copy the file from input folder.
try {
NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication("", outputFolderUserId, outputFolderPassword); //authenticating output folder
log.info("targetFilePath: {} and currentFile : {}", toFilePath, file);
SmbFile targetFile = new SmbFile(toFilePath, auth1);
try {
file.copyTo(targetFile);
file.delete(); //delete the file which we copied at our desired server
log.info("Archived File : {} to: {}", file.getName(), targetFile.getName());
return true;
} catch (SmbException e) {
log.error("Unable to Archive File: {}", file.getName());
return false;
}
} catch (MalformedURLException e) {
log.error("Connection failed to Server Drive: {}", toFilePath);
}
return false;
}
很高興能幫助你 :)
添加回答
舉報