PS: if you want to see my solution directly, you can jump to 2.5. I hope it can help you
1. Problem description
After connecting to the SFTP server, it is found that the required files cannot be obtained, and an exception occurs when SFTP calls the cd method
2. Solution process
2.1 due to a policy problem, the SFTP server cannot be connected to my local code. After countless attempts, I angrily built an SFTP server on my Alibaba cloud server for testing, as follows:
Path of test file: / data/sftp/mysftp/upload
2.2 during the test, I first studied how to obtain files after connecting to SFTP. The first few attempts were to obtain File objects with File. The code is as follows:
File file = new File(path+File.separator, uidName); System.out.println(file.getAbsolutePath()); FileInputStream input = null; if(file.exists()){ try { input = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } }
Finally, it is found that no matter how to switch the path, file The returns of exists() are all false, that is, the acquisition fails. Therefore, the SFTP method is used to obtain the file. The code is as follows:
String path = "/data/sftp/mysftp/upload"; directory = path; channelSft.cd(directory); // Required file name String uidName = "test.txt"; InputStream inputStream = channelSft.get(uidName);
This method is to use the CD to switch to the directory where the file is located, and then use sftp's get() method to obtain the input stream, and then process it.
2.3 however, during the test, I found that when I call the cd() method, the relative path can be accessed, but the absolute path cannot be accessed. If I directly use the absolute path, an error no such file will be reported. The exception is shown in the following figure:
Although I don't understand why the relative path is feasible and the absolute path is not, at least I should be able to obtain file resources. I excitedly went to the company's test environment and found that the relative path is still No Such File!!
2.4 what is the reason? I tried the ls() method again and found that ls is OK, but cd(xx) is not. At this time, I suspected that it was a permission problem. I also went to learn about the file permissions of linux. It turns out that it has nothing to do with permissions (because the parts accessible from the command line should also be available in the code). The summary of permissions is useless for the time being, so I'll post it later.
2.5 later, after listening to the analysis given to me by others, I think it is either the permission or the path. Since the permission is excluded, try whether the path is correct. We can use the pwd command to display the working directory of this file (the absolute path where it is currently located).
The command is as follows:
[root@iZbp1hwh629hd4xz80i1z0Z upload]# sftp mysftp@120.xx.xxx.xxx mysftp@120.xx.xxx.xxx's password: Connected to 120.xx.xxx.xxx. sftp> ls upload sftp> cd upload sftp> ls test.txt sftp> pwd Remote working directory: /upload sftp>
You can see that the path printed with pwd command after logging in the SFTP account is the path required for our cd operation! This result is different from the absolute path of the file. I tried it. That's the problem.
// use/upload You can visit it smoothly String directory = "/upload"; channelSft.cd(directory);
About Linux file permissions:
View user permissions:
groups user name: view the user group to which the current user belongs
cat /etc/group: view all user groups
PS: to cd enter a directory, the permission of the directory must have - x (executable) permission. If you want to ls export the files in this directory, the directory must also have - R (readable) permission.
View file permissions: ll action:
[root@iZbp1hwh629hd4xz80i1z0Z upload]# ll total 4 -rw-r--r-- 1 root root 12 Dec 31 22:10 test.txt
The returned results can be divided into four parts: '-','rw - ',' - R - ','r --',
The first character represents the file type (- represents ordinary file, d represents directory file, c: represents string device, if router and other devices, this is an ordinary file
The rest are divided into three groups with three units in each group
The first group (rw -): indicates the permission of the file owner. The owner of the test file here is root. The permission here is readable and writable
The second group (- r -): the permissions of the group to which the file belongs. The group to which the file in the above figure belongs is also the root group, which is readable here
The third group (r --): permissions of others (persons unrelated to this document), which is readable here