|
16 | 16 |
|
17 | 17 | import java.io.IOException; |
18 | 18 | import java.io.OutputStream; |
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.Arrays; |
20 | 21 | import java.util.EnumSet; |
21 | 22 | import java.util.HashSet; |
@@ -202,36 +203,53 @@ public String copyFileTo(String sourceIP, String sourceFilePath, String destIP, |
202 | 203 | @Override |
203 | 204 | public String listFiles(String IP, String filePath, String user, String pass) throws IOException { |
204 | 205 | extractSharedPathFromPath(filePath.replace("\\", "/")); |
205 | | - return listFiles(IP, sharedFolder, sFilePath, user, pass, null); |
206 | | - } |
207 | | - |
208 | | - public String listFiles(String IP, String sharedFolder, String path, String user, String pass, String domain) { |
209 | 206 | String output=""; |
| 207 | + boolean isDirectory = false; |
210 | 208 | sharedFolder = parsePath(sharedFolder); |
211 | | - path = parsePath(path); |
| 209 | + String path = parsePath(sFilePath); |
212 | 210 | if (!path.trim().endsWith("/") && !"".equalsIgnoreCase(path.trim())) path+="/"; |
213 | | - boolean isDirectory = false; |
214 | | - SMBClient client = new SMBClient(); |
215 | | - try (Connection connection = client.connect(IP)) { |
216 | | - AuthenticationContext ac = new AuthenticationContext(user, pass.toCharArray(),domain); |
217 | | - Session session = connection.authenticate(ac); |
218 | | - // Connect to Share |
219 | | - try (DiskShare share = (DiskShare) session.connectShare(sharedFolder)) { |
220 | | - for (FileIdBothDirectoryInformation f : share.list(path, "*")) { |
221 | | - isDirectory = f.getFileAttributes()==FileAttributes.FILE_ATTRIBUTE_DIRECTORY.getValue(); |
222 | | - output+= f.getFileName() + (isDirectory? "/" : "") + "\n"; |
223 | | - } |
224 | | - } catch (SMBApiException e) { |
225 | | - output="Erro: Nao foi possivel localizar o diretorio "+sharedFolder+(!"".equalsIgnoreCase((sharedFolder+path).trim())? "/" : "")+path; |
226 | | - if (e.getMessage() != null) output+=" ("+e.getMessage()+")"; |
| 211 | + try { |
| 212 | + ArrayList<FileIdBothDirectoryInformation> list = listFilesAsList(IP, sharedFolder, sFilePath, user, pass, null); |
| 213 | + for (FileIdBothDirectoryInformation f : list) { |
| 214 | + isDirectory = f.getFileAttributes()==FileAttributes.FILE_ATTRIBUTE_DIRECTORY.getValue(); |
| 215 | + output+= f.getFileName() + (isDirectory? "/" : "") + "\n"; |
227 | 216 | } |
228 | 217 | } catch (IOException e) { |
229 | | - output="Erro: Nao foi possivel listar os arquivos do diretorio: "+sharedFolder; |
| 218 | + output="Erro: Nao foi possivel listar os arquivos do diretorio: "+sharedFolder; |
| 219 | + if (e.getMessage() != null) output+=" ("+e.getMessage()+")"; |
| 220 | + } catch (Exception e) { |
| 221 | + output="Erro: Nao foi possivel localizar o diretorio "+sharedFolder+(!"".equalsIgnoreCase((sharedFolder+path).trim())? "/" : "")+path; |
230 | 222 | if (e.getMessage() != null) output+=" ("+e.getMessage()+")"; |
231 | 223 | } |
232 | 224 | return output; |
233 | 225 | } |
234 | 226 |
|
| 227 | + @Override |
| 228 | + public ArrayList<FileIdBothDirectoryInformation> listFilesAsList(String IP, String filePath, String user, String pass) throws Exception { |
| 229 | + extractSharedPathFromPath(filePath.replace("\\", "/")); |
| 230 | + return listFilesAsList(IP, sharedFolder, sFilePath, user, pass, null); |
| 231 | + } |
| 232 | + |
| 233 | + public ArrayList<FileIdBothDirectoryInformation> listFilesAsList(String IP, String sharedFolder, String path, String user, String pass, String domain) throws Exception { |
| 234 | + ArrayList<FileIdBothDirectoryInformation> output = new ArrayList<>(); |
| 235 | + sharedFolder = parsePath(sharedFolder); |
| 236 | + path = parsePath(path); |
| 237 | + if (!path.trim().endsWith("/") && !"".equalsIgnoreCase(path.trim())) path+="/"; |
| 238 | + SMBClient client = new SMBClient(); |
| 239 | + Connection connection = client.connect(IP); |
| 240 | + AuthenticationContext ac = new AuthenticationContext(user, pass.toCharArray(),domain); |
| 241 | + Session session = connection.authenticate(ac); |
| 242 | + // Connect to Share |
| 243 | + DiskShare share = (DiskShare) session.connectShare(sharedFolder); |
| 244 | + for (FileIdBothDirectoryInformation f : share.list(path, "*")) { |
| 245 | + output.add(f); |
| 246 | + } |
| 247 | + if (share!=null) share.close(); |
| 248 | + if (session!=null) session.close(); |
| 249 | + if (connection!=null) connection.close(); |
| 250 | + return output; |
| 251 | + } |
| 252 | + |
235 | 253 | private String parsePath(String path) { |
236 | 254 | while (path.startsWith("\\") || path.startsWith("/")) { |
237 | 255 | path = path.substring(1); |
|
0 commit comments