亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JCIFS SmbFile RenameTo 無法移動文件

JCIFS SmbFile RenameTo 無法移動文件

慕村225694 2021-12-18 15:14:46
我有使用 JCIFS SmbFile.renameTo() 方法的最奇怪的行為。當我執行下面的代碼時,它應該將網絡文件從 test1 移動到 test2,但它會在 test2 中創建一個名為 test.xml 的文件夾并拋出以下錯誤“當該文件已經存在時無法創建文件...”我可以想不通 為什么這種方法會這樣做? NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication (sDomain,                                        sUsername, sPassword); SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth); SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth); smbFromFile.renameTo(smbToFile);
查看完整描述

2 回答

?
GCT1015

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();


查看完整回答
反對 回復 2021-12-18
?
心有法竹

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;

}

很高興能幫助你 :)


查看完整回答
反對 回復 2021-12-18
  • 2 回答
  • 0 關注
  • 475 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號