【发布时间】:2015-01-29 10:32:21
【问题描述】:
以下是我通过验证和读取文件中的数据来访问共享文件夹中的文件的代码。(使用 JCIF)
public void findFiles() throws Exception{
String url = rs.getString("addPolicyBatchFolder_login_url"); //username, url, password are specified in the property file
String username = rs.getString("addPolicyBatchFolder_login_userName");
String password = rs.getString("addPolicyBatchFolder_login_password");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
SmbFile dir = null;
dir = new SmbFile(url, auth);
SmbFilenameFilter filter = new SmbFilenameFilter() {
@Override
public boolean accept(SmbFile dir, String name) throws SmbException {
return name.startsWith("starting string of file name");//picking files which has this string on the file name
}
};
for (SmbFile f : dir.listFiles(filter))
{
addPolicyBatch(f.getCanonicalPath()); //passing file path to another method
}
}
使用此代码,我成功地进行了身份验证,并且能够列出文件。我尝试打印规范路径(我也尝试使用f.path())并且我能够打印完整路径。
Following 是下一个方法。
public void addPolicyBatch(String filename) throws Exception{
File csvFile = new File(filename);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(csvFile)); //FileNotFound exception
while((line = br.readLine()) != null){
//more code
在上述方法中,当涉及到bufferReader时,显示FleNotFoundException。
如果我打印规范路径,以下是输出。
smb://sharePath/file.csv 正确路径
但是在第二种方法中(我得到异常的地方),异常如下。
java.io.FileNotFoundException: smb:\sharePath\file.csv (The filename, directory name, or volume label syntax is incorrect)
如你所见,smb: 之后只有一个\。
我不确定为什么它没有传递第一种方法中打印的确切文件路径。
【问题讨论】:
-
好吧,异常已经指出您的路径不正确。尝试使用不同的路径变体,例如。 G。带双反斜杠
-
我尝试了几种方法。
smb:///,smb://。即便如此,如果我能够列出具有正确路径的文件,那么在下一个方法中会出现什么问题? -
尝试其他可能性。我不能说哪个是正确的,因为我现在无法测试它,但是由于异常向您显示带有反斜杠的路径,那么我也会尝试使用这些。