使用SAMBA 访问分享文件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用SAMBA 访问分享文件

使用SAMBA 访问分享文件

 2018/9/7 19:00:24  一边天  程序员俱乐部  我要评论(0)
  • 摘要:/***samba共享文件读取**@paramsmbPathsmbPath文件路径*@paramprojectCode项目编号*@return*/publicstaticResponseMessagegetSambaFile(StringsmbPath,StringprojectCode){ResponseMessageresponseEntity=newResponseMessage();responseEntity.setFlag(true);try
  • 标签:使用 文件 分享
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;
    }

?

上一篇: Java中重载(Overload)与重写(Override) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名