有关上传进度条,ServletFileUpload的setProgressListener不起作用,是需要什么配置吗?

hymdeyx 2017-03-01 07:47:31
我用的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";
}
}
...全文
164 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧