class="java">/** * samba共享文件读取 * * @param smbPath smbPath文件路径 * @param projectCode 项目编号 * @return */ public static ResponseMessage getSambaFile(String smbPath, String projectCode) { ResponseMessage responseEntity = new ResponseMessage(); responseEntity.setFlag(true); try { BioCloudProperties bioCloudProperties = ApplicationContextProvider.getContext().getBean(BioCloudProperties.class); String samplePdfFilePath = bioCloudProperties.getSampleInformationConfig().getSamplePdfFilePath(); String[] sambaConfig = bioCloudProperties.getCustomizationConfig().getSambaConfig(); UniAddress address = UniAddress.getByName(sambaConfig[0]); //sambaConfig[0] 分享者的ip, sambaConfig[1] 认证的用户名, sambaConfig[2] 认证的密码 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(sambaConfig[0], sambaConfig[1], sambaConfig[2]); SmbSession.logon(address, auth); SmbFile sf = new SmbFile("smb://" + sambaConfig[0] + smbPath, auth); long size = 2 * 1024 * 1024 * 1024; InputStream inputStream = sf.getInputStream(); byte[] bytes = new byte[1024]; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); String filePath = String.format("%s/%s", samplePdfFilePath, format.format(new Date())); Files.createDirectories(Paths.get(filePath)); filePath = filePath + File.separator + projectCode + smbPath.substring(smbPath.lastIndexOf(".")); LOG.info("上传到临时目录的文件路径:{}", filePath); FileOutputStream outputStream = new FileOutputStream(filePath); while (inputStream.read(bytes) != -1) { size = size - 1024; if (size < 0) { responseEntity.setFlag(false); break; } outputStream.write(bytes); } responseEntity.setMessage(filePath); outputStream.close(); inputStream.close(); } catch (SmbException e) { LOG.info("smbException:{}", e.getMessage()); responseEntity.setFlag(false); } catch (MalformedURLException e) { LOG.info("MalformedURLException:{}", e.getMessage()); responseEntity.setFlag(false); } catch (UnknownHostException e) { LOG.info("UnknownHostException:{}", e.getMessage()); responseEntity.setFlag(false); } catch (IOException e) { LOG.info("IOException:{}", e.getMessage()); responseEntity.setFlag(false); } return responseEntity; }
?