zoukankan      html  css  js  c++  java
  • jfinal 后台文件上传(结合上一篇(h5 图片回显))

    前端用了jquery.form.js插件异步提交表单

     $("#imgForm").ajaxSubmit();//户主头像

    /**
    *
    * @description 上传户主头像
    * @author 邵海雄
    * @date 2016年8月12日 上午9:39:26
    */
    public void holdHead(){
    //上传文件名(以当前系统时间为准)
    String uploadfileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
    UploadFile uploadFile = getFile("photoFilePic");//获得上传文件
    File source = uploadFile.getFile(); //源文件
    String fileName = uploadFile.getFileName(); //获得源文件文件名
    String extension = fileName.substring(fileName.lastIndexOf(".")); //得到源文件的后缀
    String uploadPath = PathKit.getWebRootPath()+"/upload/household"; //上传文件的路径
    //服务器上的图片地址,添加到数据库的图片地址
    String imgSrc="/upload/household/"+uploadfileName+extension;
    ViewModel viewModel = AntipovertyCommonService.upload(uploadPath,source, uploadfileName+extension);
    String householdId = getPara("householdId_img");
    if (StrKit.notBlank(householdId)) {
    //拿到户主ID更新图片地址
    boolean flag= PovertyService.updateHouseholdHeadById(householdId,imgSrc);
    }
    renderJson();
    }

    /**
    *
    * @description 文件上传
    * @author 邵海雄
    * @return
    * @date 2016年8月9日 下午 15:54:39
    */
    public static ViewModel upload(String uploadPath,File source,String uploadFileName) {
    ViewModel vm = new ViewModel();

    try {
    FileInputStream fis = new FileInputStream(source); //源文件输入

    File targetDir = new File(uploadPath);//上传文件parent
    if (!targetDir.exists()) {
    targetDir.mkdirs(); //如果目标路径不存在新建一个
    }

    File target = new File(targetDir,uploadFileName);//上传文件
    if(!target.exists())
    {
    target.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(target);//上传文件输出
    byte[] bts = new byte[300];
    while (fis.read(bts, 0, 300) != -1) { // 读取到数组
    fos.write(bts, 0, 300); //写入
    }
    fis.close();
    fos.close();
    source.delete();
    vm.setInfo(new Info("上传成功"));


    } catch (FileNotFoundException e) {
    vm.setErr(new Error("上传出现错误,请稍后再上传"));
    } catch (IOException e) {
    } vm.setErr(new Error("文件写入服务器出现错误,请稍后再上传"));

    return vm;
    }

  • 相关阅读:
    如何编写Robot Framework测试用例1---(基本格式篇)
    如何编写Robot Framework测试用例2---(测试用例语法1)
    使用RobotFramework的DataBaseLibrary(Java实现)
    Python fabric远程自动部署简介
    Python之路【第二十三篇】:Django 初探--Django的开发服务器及创建数据库(笔记)
    Python之路【第二十二篇】:Django之Model操作
    Python之路【第二十一篇】:Django之Form组件
    第十一篇:web之Django之Form组件
    第十篇:web之前端之django一些feature
    第九篇:web之前端之web上传文件的方式
  • 原文地址:https://www.cnblogs.com/shaohaixiong/p/5767319.html
Copyright © 2011-2022 走看看