当前位置: 首页 > 工具软件 > fineuploader > 使用案例 >

fineUploader 多文件删除获取qquuid

凤扬
2023-12-01

作为开发小白,找了好久没获取到qquuid。结果一回头发现一句话既可解决:

String qquuid = request.getParameter("qquuid");

//手动分割上代码=======================================================================

1.fineUploader的模板

<link href="${ctx}/resource/js/lib/fineUploder/fine-uploader-new.css" rel="stylesheet">
<link href="${ctx}/resource/js/lib/fineUploder/fine-uploader-gallery.css" rel="stylesheet">
<script src="${ctx}/resource/js/lib/fineUploder/jquery.fine-uploader.js"></script>

<script type="text/template" id="qq-template">
    <div class="qq-uploader-selector qq-uploader" qq-drop-area-text="Drop files here" style="padding:5px;">
        <div class="buttons">
            <div class="qq-upload-button-selector qq-upload-button">
                <div>上传</div>
            </div>
        </div>
        <ul class="qq-upload-list-selector qq-upload-list" aria-live="polite" aria-relevant="additions removals">
            <li style="font-size:12px;color:blue;">
                <span class="qq-upload-file-selector qq-upload-file"></span>
                <button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete" style="font-size:8px;padding:1px;">删除</button>
            </li>
        </ul>
    </div>
</script>

2.页面jsp定义容器

<div id="fine-uploader-gallery"></div>

3.初始化

$('#fine-uploader-gallery').fineUploader({
    template: 'qq-template',
    autoUpload: true,  //是否自动上传,true就会自动上传
    multiple : true, //multiple选项:默认为true,用户可以一次选择多个文件并上传。
    request: {  //后台接受文件上传的URL路径。设置其中的endpoint属性。
        endpoint: '${ctx}/basInvestigation/fileUpload?busId=${eventId}&fileSource=2'
    },
    session: {
        endpoint: '${ctx}/basInvestigation/getFileInfo?busId=${eventId}'
    },
    deleteFile: {  //用于删除已上传的文件。可以删除的文件必须是在同一个页面中已经成功上传,或者由session选项初始化的文件列表。
        enabled: true,
        endpoint: '${ctx}/basInvestigation/deleteFile',  //后台用于删除文件的URL地址。
        // 返回值和上传时一样必须含有“success”属性
        method: 'POST',//必须设置为'post'
        forceConfirm: true,  //是否出现删除确认的对话框,默认值为false
        confirmMessage: '确定要删除吗? ',
        deletingFailedText: '删除失败!'
    },
    validation:
        {
            allowedExtensions: ['jpeg', 'jpg', 'pdf', 'png','docx','doc']
        }
})

4.上传的后台代码

 

/**
 * 上传资料
 * @param request
 * @param response
 */
@RequestMapping(value = "/fileUpload")
public void fileUpload(MultipartHttpServletRequest  request, HttpServletResponse response) {
    //获取用户信息
    SysUserSession sysUserSession = (SysUserSession) request.getSession().getAttribute(SYSUSERSESSION);
    String id = request.getParameter("busId");
    String filename = "";
    //是否上传成功
    boolean uploadState = false;
    String qquuid = request.getParameter("qquuid");
    Map<String, Object> resMap = new HashMap<String, Object>();
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    try{
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile mf = entity.getValue();
            // 文件存放路径
            StringBuilder fileUrl = new StringBuilder(sysUserSession.getAreaCode()+"/clearchaos/"+sysUserSession.getLoginAccount()+"/"+TimeHelper.getCurrentYear()+TimeHelper.getCurrentMonth());
            //原文件名
            String fileOldName  = mf.getOriginalFilename();
            //根据.分割原文件名
            String[] splitName = fileOldName.split("\\.");
            //重命名后的文件名 格式(原文件名+文件来源(wx、pc、app)+时间))
            String fileNewName =  TimeHelper.formatDateByFormat(new Date(),"yyyyMMddHHmmss")+"-pc-"+fileOldName;
            String fileContentType = splitName[splitName.length-1];
            fileUrl.append("/"+fileContentType);
            filename = filename.substring(filename.lastIndexOf('\\') + 1);
            // 获取item中的上传文件的输入流
            InputStream in = mf.getInputStream();
            uploadState = FtpUtil.uploadFile(fileUrl.toString(), fileNewName, in);
            BasEventFile basEventFile = new BasEventFile();
            if(uploadState) {
                basEventFile.setBusId(id);
                basEventFile.setFileNewName(fileNewName);
                basEventFile.setFileOldName(fileOldName);
                if(fileContentType.equals("docx")|| fileContentType.equals("doc")|| fileContentType.equals("pdf") ){
                    basEventFile.setFileType("3");
                }else{
                    basEventFile.setFileType("1");
                }
                basEventFile.setFileUrl(fileUrl.toString());
                basEventFile.setFileSource(qquuid);
                basEventFile = basEventFileService.save(basEventFile);
                resMap.put("fileId", basEventFile.getpkId());
                resMap.put("success", true);
            }else{
                resMap.put(RES_CODE, "0");
                resMap.put(RES_MSG, "上传失败!");
                resMap.put("success", false);
            }
        }
        String json = JsonUtil.writeMapJSON(resMap);
        response.getWriter().println(json);
    } catch (Exception e) {
        resMap.put(RES_CODE, "1");
        resMap.put(RES_MSG, "上传失败!");
        logger.error("错误异常",e);
    }
}

5.回显的后台代码

/**
 * 获取资料详细信息
 * @param busId
 * @return
 */
@RequestMapping(value = "getFileInfo")
public void getFileInfo(String busId,HttpServletResponse response)throws IOException {
    Map<String,Object> resMap = new HashMap<String,Object>();
    JSONArray array = new JSONArray();
    try {
        //根据id获取上传到ftp的文件
        List<Map<String, Object>> listMap= basInvestigationService.findFileByBusId(busId);
        if(!listMap.isEmpty()){
            for(int i=0;i<listMap.size();i++){
                ObjectMapper objectMapper = new ObjectMapper();
                net.sf.json.JSONObject obj = new net.sf.json.JSONObject();
                if(!listMap.get(i).isEmpty()) {
                    String name=(String) listMap.get(i).get("file_new_name");
                    String fileOldName=(String) listMap.get(i).get("file_old_name");
                    String url =(String) listMap.get(i).get("file_url");
                    String fileSourceId =(String) listMap.get(i).get("file_source");
                    String fileName=url+SEPORATE+name;
                    byte[] img = FtpUtil.downloadFtpFileByte(fileName);
                    String json = objectMapper.writeValueAsString(img);
                    json = json.replaceAll("\"", "");
                    obj.put("name", fileOldName);
                    obj.put("uuid",fileSourceId);
                    obj.put("blobName", name);
                    String srcUrl = "/hzz/upload/showImg?fileName="+fileName;
                    obj.put("thumbnailUrl", "data:image/png;base64,"+json);
                    array.add(obj);
                }
            }
        }
    } catch (Exception e) {
        logger.error("错误异常",e);
    }
    response.getWriter().print(array);
}

6.删除的后台代码

/***
 * 删除文件的方法
 * @param
 * @return
 */
@RequestMapping(value = "deleteFile", method = RequestMethod.POST)
public void deleteFile(HttpServletRequest request ,HttpServletResponse response)throws IOException  {
    boolean resMsg = true;
    Map<String,Object> resMap = new HashMap<String,Object>();
    try {
        String qquuid = request.getParameter("qquuid");
        BasEventFile basEventFile = basEventFileService.findByFileSource(qquuid);
        // 物理删除
        String name = basEventFile.getFileNewName();
        String filepath = basEventFile.getFileUrl()+basEventFile.getFileNewName();
        resMsg=FtpUtil.deleteFile(filepath,name);
        if(resMsg){
            // 删除文件表
            basEventFileService.delete(basEventFile);
            resMap.put("success",true);
        }else{
            resMap.put("success",false);
        }
    } catch (Exception e) {
        logger.error("错误异常",e);
        resMap.put("success",false);
    }
    String json = JsonUtil.writeMapJSON(resMap);
    response.getWriter().println(json);
}

如此可获取到qquuid因为是form data数据提交,上传时用MultipartHttpServletRequest 即可获取到qquuid.

 
MultipartHttpServletRequest
 类似资料: