hymdeyx hymdeyx
在时光的道路上,某些人眼中的草终究会长成别人手中的宝......
关注数: 9 粉丝数: 85 发帖数: 1,609 关注贴吧数: 14
上传进度条,ServletFileUpload的setProgressListener不起作用 我用的ofbiz框架,ajax调用 上传时的upload.setProgressListener()执行不到,是漏掉了什么配置了吗? /** * upload 上传文件 * @param request * @param response * @return * @throws Exception */ public static String uploadProcess(HttpServletRequest request, HttpServletResponse response) { Delegator delegator = (Delegator)request.getAttribute("delegator"); Map<String, Object> context = FastMap.newInstance(); //String storeId = request.getParameter("sid"); String imageFileNameFormat = EntityUtilProperties.getPropertyValue("global", "image.filename.format", delegator); FlexibleStringExpander filenameExpander = FlexibleStringExpander.getInstance(imageFileNameFormat); String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("global", "image.server.path", delegator), context); String imageUrlPrefix = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("global", "image.url.prefix",delegator), context); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date nowDate = new Date(System.currentTimeMillis()); String fileNo = sdf.format(nowDate); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String fileName = sdf2.format(nowDate); String fileLocation = filenameExpander.expandString(UtilMisc.toMap("no",fileNo)); String filePathPrefix = fileLocation; String savePath = imageServerPath+"/"+filePathPrefix; final HttpSession hs = request.getSession(); ModelAndView mv = new ModelAndView(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(!isMultipart){ request.setAttribute("result", "ERROR"); return "success"; } // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); //为factory设置是否将上传文件已临时文件的形式保存在磁盘的临界值 //(以字节为单位的int值) ((DiskFileItemFactory) factory).setSizeThreshold(2048 * 1024); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(1024*1024*100); // 设置上传的单个文件的最大字节数为100M upload.setSizeMax(1024*1024*1024); //设置整个表单的最大字节数为1G //监听器 upload.setProgressListener( new ProgressListener(){ public void update(long pBytesRead, long pContentLength, int pItems) { ProcessInfo pri = new ProcessInfo(); pri.itemNum = pItems; pri.readSize = pBytesRead; pri.totalSize = pContentLength; pri.show = pBytesRead+"/"+pContentLength+" byte"; pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength)*100); hs.setAttribute("proInfo", pri); } }); List items = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Parse the request // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); System.out.println("this is common feild!"+name+"="+value); } else { System.out.println("this is file feild!"); String fieldName = item.getFieldName(); String clientFileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); //File uploadedFile = new File("D:/"+fileName1); //String savePath1 = "/global/images/upload/2017-03-01"; // File uploadedFile = new File(savePath); /* if (!uploadedFile.exists()) { uploadedFile.mkdirs(); }*/ try { //item.write(uploadedFile); item.write(new File(imageServerPath+"/"+clientFileName)); String extName = clientFileName.substring(clientFileName.length()-4,clientFileName.length()); fileName = fileName+extName; //重命名 File file = new File(imageServerPath + "/" + clientFileName); File file2 = new File(imageServerPath + "/" + fileName); file.renameTo(file2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } String saveUrl = imageUrlPrefix + "/" +fileName;//clientFileName; request.setAttribute("result", "OK"); request.setAttribute("uri", saveUrl); return "success"; } /** * process 获取进度 * @param request * @param response * @return * @throws Exception */ /* @RequestMapping(value = "/process.json", method = RequestMethod.GET) @ResponseBody */ public static String process(HttpServletRequest request, HttpServletResponse response) throws Exception { //ProcessInfo processInfo = (ProcessInfo) request.getSession().getAttribute("proInfo"); //request.setAttribute("processInfo", processInfo); return "success"; } }
1 下一页