JFinal文件上传、下载、删除、修改文件名


1.文件上传:

ObjectVo objectVo=new ObjectVo();
        try {
            UploadFile file=getFile();
            String cou_number="201811";
            String session= String.valueOf(getSession());
            //大小限制
            System.out.println(file.getFile().length()+"   "+maxPostSize);
            if (file.getFile().length()>maxPostSize){
                objectVo.setCode(0);
                objectVo.setMsg("超出大小限制");
                renderJson(objectVo);
            }
            //默认路径+文件名
            String path=file.getUploadPath()+"/"+file.getFileName();
            //文件大小
            double data_size=file.getFile().length();
            
            String tea_upload=Db.getSql("teacher.teacherUploadData");

            String filename=file.getFileName();
            //文件名
            String name=filename.substring(0,filename.lastIndexOf("."));
             //文件类型
            String data_type=(filename.substring(file.getFileName().lastIndexOf("."))).replace(".","");
            Integer flag=Db.update(tea_upload,name,data_type,data_size,session,cou_number,path);
            objectVo.setCode(flag);
            objectVo.setMsg("文件上传成功!");
        }catch (Exception e){
            e.printStackTrace();
            objectVo.setMsg("文件上传失败!");
            objectVo.setCode(0);
        }
        renderJson(objectVo);

2.文件下载:

String name=文件名;
String type="docx";
try {
      String path = getSession().getServletContext().getRealPath("/static/modules/upload");
      path=path+"\\"+name+"."+type;
      System.out.println(path);
      File file=new File(path);
      renderFile(file);
}catch (Exception e){
      renderJson("文件下载失败!");
}

3. 修改文件名称

        String id="30";
        String oriname="t";
        String data_type="txt";
        String newname="t1";
        ObjectVo objectVo=new ObjectVo();
        try {
            String path = getSession().getServletContext().getRealPath("/static/modules/upload");
            String newpath=path+"\\"+oriname+"."+data_type;
            File file=new File(newpath);
            if (file.exists()){
                file.renameTo(new File(path+"\\"+newname+"."+data_type));
                objectVo.setCode(1);
                objectVo.setMsg("文件修改成功!");
            }else {
                objectVo.setCode(0);
                objectVo.setMsg("文件不存在!");
            }
        }catch (Exception e){
            objectVo.setCode(0);
            objectVo.setMsg("文件修改失败!");
        }
        renderJson(objectVo);

4.文件删除

        String id="1";
        String oriname="tz";
        String data_type="txt";
        ObjectVo objectVo=new ObjectVo();
        String path = getSession().getServletContext().getRealPath("/static/modules/upload");
        String newpath=path+"\\"+oriname+"."+data_type;
        try {
            File file=new File(newpath);
            if (file.exists()){
                file.delete();
                objectVo.setCode(1);
                objectVo.setMsg("文件删除成功!");
            }else {
                objectVo.setCode(0);
                objectVo.setMsg("文件删除失败,请检查文件是否存在!");
            }
        }catch (Exception e){
            objectVo.setMsg("文件删除失败!");
            objectVo.setCode(0);
        }
        renderJson(objectVo);

猜你喜欢

转载自blog.csdn.net/weixin_42551369/article/details/88896865