<!--网络共享内文件传输--> <!--java连接共享文件夹--> <dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.14-kohsuke-1</version> </dependency>
2.代码接口
@GetMapping(value = "/getShareFile") @ApiOperation(value = "获取共享文件信息") public RestResult<List<String>> 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<String> 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<Object> getWebToLocalFile(@RequestParam("webPath") String webPath , @RequestParam("updateId") String updateId) { File file=new File(createPath); if (!file.exists()){ file.mkdirs(); } NdeUploadFile ndeUploadFile = ndeUploadFileService.ndeUploadById(updateId); //查询上传服务表最大批次 Integer i = ndeFileServerService.selectMaxByBatch(); String[] split = webPath.split(","); List<NdeFileServer> fileServersList = new ArrayList<>(); for (String sp : split ) { String fileName = sp.substring(sp.lastIndexOf("/") + 1); File webFile = new File(shareFilePath + fileName); NdeFileServer fileServer = new NdeFileServer(); try { //获取图片的宽高 BufferedImage sourceImg = ImageIO.read(new FileInputStream(webFile)); fileServer.setHeight(sourceImg.getHeight()); fileServer.setWidth(sourceImg.getWidth()); //保存到本地 String s = createPath + "/" + ndeUploadFile.getDirUrl() + "/" + fileName; File localFile = new File(s); //清理缓存 System.gc(); boolean b = webFile.renameTo(localFile); if (!b) { //renameTo移动失败,就复制文件,然后删除原文件 FileUtils.copyFile(webFile, localFile); webFile.delete(); } fileServer.setType(1); fileServer.setHttpUrl(httpPath + s.replaceAll(s.split("/")[0], "")); fileServer.setFilePath(s); //批次 if (StringUtils.isEmpty(i)) { fileServer.setBatch(1); } else { fileServer.setBatch(i + 1); } fileServer.setUploadFileId(updateId); fileServer.setFileName(fileName); fileServer.setId(StringUtils.createUUID()); fileServer.setCreateTime(new Date()); ndeFileServerService.insertSelective(fileServer); fileServersList.add(fileServer); } catch (IOException e) { e.printStackTrace(); } } return RestResultUtil.genSuccessResult(fileServersList); }