使用dropzone实现文件拖拽上传功能

使用dropzone实现文件拖拽上传功能_第1张图片

使用dropzone实现文件拖拽上传功能_第2张图片

前端代码,关于dropzone的配置我写在注释里了



    
    <%@include file="/webmana/meta.jsp" %>
    
    
    
    


    
    
<%@include file="/webmana/footer.jsp" %>

注意:

autoProcessQueue为true时默认自动上传所有文件,而设为false时手动触发默认一次只传两个,

设置parallelUploads: 100,可修改手动触发一次上传的数量

 

java后台接收保存文件:

@RequestMapping(value = "uploadImg", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String uploadImg(
            @RequestParam(value = "file") MultipartFile[] files,  //这样接收文件
            String classId,
            HttpServletRequest request
    ) {
        try {
            Map params=new HashMap();
            String path="/resource/images/";
            int userId=((TSystemUser)request.getSession().getAttribute("USER")).getUserId();
            params.put("classId",classId);
            params.put("attachmentType","IMAGE");
            params.put("userId",userId);
            for (MultipartFile file : files) {    //循环保存文件
                Map name=uploadFile(file, request);
                params.put("attachmentUrl",path+name.get("saveName"));
                params.put("attachmentName",name.get("fileName"));
                attachmentService.saveFile(params);
               // attachmentService.saveImg(path);
            }
            // 返回前台
            return JSON.toJSONString("success");

        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("fail");
        }

    }

    public Map uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
        Map result=new HashMap();
        String fileName = file.getOriginalFilename();
        String basePath=request.getSession().getServletContext().getRealPath("/");
        String path=basePath+"resource/images/";            //设置文件保存路径
//        File tempFile = new File(path, new Date().getTime() + String.valueOf(fileName));
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        String saveName=String.valueOf((new Date()).getTime()).substring(8)+(int)((Math.random()*999+1))+'.'+fileType;

        File tempFile = new File(path, String.valueOf(saveName));
        if (!tempFile.getParentFile().exists()) {    //创建文件夹
            tempFile.getParentFile().mkdir();
        }
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        file.transferTo(tempFile);
        result.put("fileName",fileName);
        result.put("saveName",saveName);
        return result;
    }

 

 

 

 

 

你可能感兴趣的:(前端)