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

来源:互联网 发布:猛犸大数据 编辑:程序博客网 时间:2024/04/28 23:34
// 本地文件夹 批量导入    @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