检测文件夹&文件是否存在

public void checkappfile(HttpServletRequest req, HttpServletResponse resp) throws IOException{ String fname="aa.apk,bb.ipa,cc.png"; String [] filename=fname.split(","); String url = req.getRealPath("/"); File root = new File(url); File[] files = root.listFiles(); boolean app = false; for(File file:files){ if(file.getName().equals("app")){ app=true; break; } } JSONObject jsonObject=new JSONObject();String code="00"; if(app){ String result = ""; boolean msg = false; try { File root1 = new File(url+"app"); File[] fileapp = root1.listFiles(); for(File file:fileapp){ for(int i=0;i<filename length="" i="" msg="filename[i].equals(file.getName());" if="" filename="" i="" equals="" file="" getname="" result="result+filename[i]+',';" result="result.substring(0,result.length()-1);" if="" result="" length="">35){ code="02";msg=true; }else{ code="01";msg=false; } } catch (Exception e) { e.printStackTrace(); } jsonObject.put("code",code); jsonObject.put("isSuccess",msg); jsonObject.put("msg",result); System.out.println(jsonObject.toString()); resp.getWriter().append(jsonObject.toString()); }else{ jsonObject.put("code","00"); jsonObject.put("isSuccess",app); jsonObject.put("msg","没有找到对应的文件夹"); System.out.println(jsonObject.toString()); resp.getWriter().append(jsonObject.toString()); } } 返回结果:{"code":"00","isSuccess":false,"msg":"没有找到对应的文件夹"}{"code":"01","isSuccess":false,"msg":"aa.apk"}{"code":"02","isSuccess":true,"msg":"aa.apk,bb.ipa,cc.png"}

你可能感兴趣的:(文件夹是否存在)