- 依赖注入
org.samba.jcifs
jcifs
1.3.14-kohsuke-1
2.代码接口
@GetMapping(value = "/getShareFile")
@ApiOperation(value = "获取共享文件信息")
public RestResult> getShareFile(@RequestParam(value = "netWorkPath") String netWorkPath) {
//String a = "smb:" + "//administrator:123@192.168.0.111/defect-M/";
File shareFile=new File(shareFilePath);
if (!shareFile.exists()){
shareFile.mkdirs();
}
String a = "smb:" + netWorkPath;
SmbFile file = null;
try {
file = new SmbFile(a);
file.connect();
List list = new linkedList<>();
if (file.exists()) {
if (file.isDirectory()) {
SmbFile[] files = file.listFiles();
for (SmbFile f : files
) {
if (f.getPath().contains("Thumbs.db") || f.isDirectory()) {
continue;
}
smbGet(f.getPath(), shareFilePath);
list.add(httpPath + shareFilePath.replaceAll(shareFilePath.split("/")[0], "") + f.getName());
}
}
}
return RestResultUtil.genSuccessResult(list);
} catch (Exception e) {
log.info("访问共享路径错误");
e.printStackTrace();
}
return RestResultUtil.failed();
}
@ApiOperation(value = "从共享目录下载文件到本地")
private void smbGet(String remoteUrl, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile == null) {
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@GetMapping(value = "getWebToLocalFile")
@ApiOperation("下载共享文件到本地(保存数据库,方便以后展示)")
public RestResult
3.开启SMB服务



