23,layui结合SpringMVC上传文件以及携带额外的参数上传文件



<%--隐藏打开的index--%>





2.上传文件的JS

layui.use(['upload','layer'],function () {
    var upload = layui.upload,layer = layui.layer;
    //普通图片上传
     var uploadInst = upload.render({
        elem: '#pic' //绑定元素
        , size: 500 //限制文件大小,单位 KB
        , accept: 'file' //普通文件
        , url: layui.setter.apiRoot + api.upload //上传接口
        , before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参
})
    this.data={savename :$("#temporarypic").html()};
            layer.load(); //上传loading
        }
        , done: function (res, index, upload) {
            layer.closeAll('loading'); //关闭loading
         if (res.code == "SUCCESS") {
                $('#picurl').val(res.data);
                $('#temporarypic').attr('src', urltemporary + res.data);
            } else {
                layer.alert('上传失败');
            }
        }
       , error: function (index, upload) {
            layer.closeAll('loading'); //关闭loading
        }
    });
        


controller

   @RequestMapping(value = "upload", produces = "application/json;charset=utf-8")
    public ResponseModel upload(MultipartFile file,String savename) throws Exception {
        AccountModel accountModel = currentAccount();
      ResponseModel responseModel = new ResponseModel();
        String filename = carouselService.upload(file, temporaryfilePath, formalfilePath,accountModel.getCompanycode());
        return responseModel.setData(filename);//返回名称
  }

service

  public String upload(MultipartFile file, String temporaryfilePath, String formalfilePath, String companycode ) throws Exception {
        String fileName = UploadFile.getupload(file, temporaryfilePath, formalfilePath);
    return fileName;
    }

util

 public static String getupload(org.springframework.web.multipart.MultipartFile file, String filePath, String formalfilePath) throws Exception {
//a.jpg--->.jpg
      String fileName = UUID.randomUUID().toString() + ertName;//uuid创建文件
        try {
            File dir = new File(filePath);
            File dirformal = new File(formalfilePath);
            if (!dir.exists()) {
//创建文件夹
                if (!dir.mkdirs()) {
                    return "";
                }
            }
      if (!dirformal.exists()) {
//创建文件夹
                if (!dirformal.mkdirs()) {
                    return "";
                }
            }

  FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(filePath + fileName)));//把文件写入磁盘
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;//返回名称
    }


附带文件移动的方法:

   /**
     * 文件 移动
     *
     * @param tempFile:移动对象
     * @param descFile:移动之后的对象
     * @return 文件是否移动成功
     */
    public static boolean renameTo(String tempFile, String descFile) {
        Boolean isCopyFile = true;
        try {

//文件路径model(比如:apk的路径,ipa的路径)
            if (!tempFile.equalsIgnoreCase("") && !descFile.equalsIgnoreCase("")) {
                File dest = new File(descFile);
                File file = new File(tempFile);
//判断移动的两个路径是否是一样的
              if (dest.getPath().equals(file.getPath())) {
                    return true;
                }
//如果这个文件已存在的话就删除(desc)copy的位置,并且显示
                if (dest.exists()) {
                    return true;
// dest.delete();
               }
//copy文件  file 复制到dest
                return file.renameTo(dest);
            }
        } catch (Exception ex) {
            isCopyFile = false;
            ex.printStackTrace();
        }
        return isCopyFile;
    }

你可能感兴趣的:(23,layui结合SpringMVC上传文件以及携带额外的参数上传文件)