2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
copyTo(SmbFile)和之間有一個(gè)有趣的區(qū)別renameTo(SmbFile)- 只有其中一個(gè)說(shuō)此文件和目標(biāo)文件不需要在同一主機(jī)上。由于renameTo(SmbFile)它沒(méi)有說(shuō),我只能假設(shè),你應(yīng)該使用copyTo,然后delete()將原來(lái)的。
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貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
有兩種可能的場(chǎng)景:
1.)文件需要移動(dòng)到同一臺(tái)服務(wù)器上(即輸入文件夾和輸出文件夾的認(rèn)證細(xì)節(jié)是相同的)。
使用 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.)文件需要在不同的服務(wù)器上移動(dòng)(即輸入文件夾和輸出文件夾的身份驗(yàn)證詳細(xì)信息 不相同)。
使用 copyTo() 方法。
在這里,我建議,您可以首先驗(yàn)證存在文件的第一臺(tái)服務(wù)器,并檢查文件是否存在,如果存在,則將其添加到列表中:
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;
}
這將為您提供列表中的文件。繼續(xù)將其復(fù)制到另一臺(tái)服務(wù)器。請(qǐng)注意,您只需要在此處對(duì)輸出文件夾進(jìn)行身份驗(yàn)證。
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;
}
很高興能幫助你 :)
添加回答
舉報(bào)