java 将某一文件夹下的所有文件 复制到指定文件下

// 本地文件夹 批量导入
    @RequestMapping(value = "/lotFile")
    public String lotFile(@RequestParam(value = "filePP") String filePP,
             RedirectAttributesModelMap  model , HttpSession httpSession, HttpServletRequest request) {
        // 允许复制的文件类型,现在允许复制htm html
         total=0;
        int no=0;
        
        List<YellowpagesitemDO> list = new ArrayList<YellowpagesitemDO>();
        String[] filterFile = { ".html", ".htm" };
        String parentId = (String) httpSession.getAttribute(SESSION_LAW02_PARENTID);
        String destStr = request.getSession().getServletContext().getRealPath("") +File.separator +"views"+File.separator+"law02"+File.separator+parentId;
        File src = new File(filePP);
        File des = new File(destStr);
        
        try {
            copyFolder(src,des,filterFile,model,parentId  ,list,httpSession);
        
        } catch (Exception e) {
            YellowpagesitemDO yy = new YellowpagesitemDO();
            yy.setName("源文件路径错误或源文件不存在");
            yy.setValue("导入失败!");
            list.add(yy);
            
        }
        model.addAttribute("id", parentId);
        model.addFlashAttribute("total", total);
        model.addFlashAttribute("no",no );
        model.addFlashAttribute("ok", total);
        model.addFlashAttribute("mess", list);
        return "redirect:/law02/show";
        
    }

    
    public void copyFolder(File srcFolder, File destFolder, String[] filterFile,ModelMap model,String parentId ,List<YellowpagesitemDO> list, HttpSession httpSession) throws Exception {
        File[] files = srcFolder.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                String pathname = destFolder + File.separator + file.getName();
                System.out.println(pathname+"复制文件呢--全名!!!");
                for (String suff : filterFile) {
                    if (pathname.endsWith(suff)) {
                        File dest = new File(pathname);
                        File destPar = dest.getParentFile();
                        destPar.mkdirs();
                        if (!dest.exists()) {
                            dest.createNewFile();
                        }
                        copyFile(file, dest,model,list,httpSession);
                    }
                }
            } else if(file.isDirectory()){
                copyFolder(file,  destFolder,  filterFile, model, parentId ,list,httpSession);
            }
            else {
                YellowpagesitemDO yellowpagesitemDO  = new YellowpagesitemDO();
                yellowpagesitemDO.setName("路径错误,源文件不存在");
                list.add(yellowpagesitemDO);
            }
        }
    }

    /***
     * * copy file * * @param src * @param dest * @param status * @throws
     * IOException
     * @param httpSession
     */
    private void copyFile(File src, File dest,ModelMap model ,List<YellowpagesitemDO> list, HttpSession httpSession) throws Exception {
        FileInputStream input = null;
        FileOutputStream outstrem = null;
        try {
            input = new FileInputStream(src);
            outstrem = new FileOutputStream(dest);
            outstrem.getChannel().transferFrom(input.getChannel(), 0, input.available());
            String content = dest.getPath();
            LawDO llDo = law01Service.getlaw(content);// 查一查 有无该文件记录,有则不插入记录到数据库
            if (llDo != null) {

            } else {
                LawDO lawDO = new LawDO();
                String parentId = (String) httpSession.getAttribute(SESSION_LAW02_PARENTID);
                lawDO.setParentId(parentId);
                lawDO.setTitle(dest.getName());
                OperatorDO opDo = (OperatorDO) httpSession.getAttribute(Constant.SESSION_USER_KEY);// 获取当前用户
                lawDO.setUpdUser(opDo.getName());
                System.out.println(content+"content++++++++");
                lawDO.setContent(content);
                lawDO.setDelFlag(0);
                law01Service.insertLaw(lawDO);
            }
            
            
            total=total+1;
            System.out.println(total+"*******total");
            YellowpagesitemDO yellowpagesitemDO =new  YellowpagesitemDO();
            yellowpagesitemDO.setName(src.getName());
            yellowpagesitemDO.setValue("导入成功!");
            list.add(yellowpagesitemDO);
            System.out.println(yellowpagesitemDO.getName()+"XXXXXX");
        } catch (Exception e) {
            throw e;
        } finally {
            outstrem.flush();
            outstrem.close();
            input.close();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值