栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现将共享文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现将共享文件

  1. 依赖注入
        
        
        
            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 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 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);
    }
 

3.开启SMB服务

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号