簡體   English   中英

FileSystemException - 嘗試使用 Java 從 Jetty 刪除文件時不允許操作

[英]FileSystemException- Operation not permitted when attempting to delete a file from Jetty using Java

嘗試通過碼頭刪除 debian 中的文件時出現 FileSystemException。 請注意,該文件的所有者是mysql ,因為我在此操作之前使用 mysql 進行了導出,並且該文件存在於 debian 的/tmp文件夾中。現在,當我嘗試使用 Java 刪除文件時,我得到一個FileSystemException和說不允許操作 這是我的代碼。

                String filePath = "tmp/test.csv";
                try {
                    Files.deleteIfExists(Paths.get(filePath));
                }  catch (IOException e) {
                    e.printStackTrace();
                }

這是堆棧跟蹤。

java.nio.file.FileSystemException: /tmp/test.csv: Operation not permitted
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:244)
    at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108)
    at java.nio.file.Files.deleteIfExists(Files.java:1165)

我假設這個錯誤是由於文件的所有者是 mysql。我也嘗試在刪除文件之前將文件的所有者更改為jetty ,但仍然出現相同的錯誤。

                Path path = Paths.get(filePath);
                UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
                UserPrincipal jetty = lookupService.lookupPrincipalByName("jetty");
                
                
                try {
                    Files.setOwner(path, jetty);
                }catch(FileSystemException fe) {
                    fe.printStackTrace();
                }

我也嘗試了另一種方法,但又以同樣的錯誤告終。

                Path path = Paths.get(filePath);
                FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
                
                UserPrincipal hostUid = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("jetty");
                try {
                    view.setOwner(hostUid);
                }catch(FileSystemException fe) {
                    fe.printStackTrace();
                }

有什么辦法可以刪除這個文件嗎? 任何幫助,將不勝感激。

謝謝

正如@JoakimErdfelt 在評論中提到的那樣,問題在於為/tmp文件夾設置了粘滯位,這阻止了另一個用戶(在本例中為碼頭)刪除文件。 如果我使用chmod -t /tmp刪除了粘性位,則可以刪除該文件。

可以在此處找到更多詳細信息為什么 Files.delete 拋出 FileSystemException

和這里的 Linux Sticky Bit Concept Explained with Examples

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM