码迷,mamicode.com
首页 > 编程语言 > 详细

Spring mvc 上传进度条实现

时间:2015-03-16 12:37:41      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

以下的仅仅是学习而已,记录以下笔记

1 springmvc 进度条,要实现ProgressListener接口,实现方法update(long readLength, long contextLength, int items)

代码如下:

 

public class mProgressListener implements ProgressListener {

String pCount=null;
int sa=0;
public String getpCount() {
return pCount;
}

/* 第一个参数代表当前已读了多少字节,第二个参数代表整个表单的长度也可以当做文件的总大小,第三个参数是代表已解析到第几个(这里对你不重要) */
@Override
public void update(long readLength, long contextLength, int items) {
double result = (readLength / (double)1048576) / (contextLength / (double)1048576);
if(contextLength<0){


}else{
long read=readLength/1048576;
long context=contextLength/1048576;
sa=(int)Math.round((100.0)*read/context);
}

//DecimalFormat df = new DecimalFormat("#.00");
//System.out.println(read+"/"+context+"="+sa+":contextLength="+contextLength);

NumberFormat nf = NumberFormat.getPercentInstance();
// System.out.println((readLength / (double)1048576) + "/" +( contextLength / (double)1048576)
// + "=" + nf.format(result));

pCount =sa+"";

}

}

2,重写类CommonsMultipartResolver

public class CommonsMultipartResolver extends
org.springframework.web.multipart.commons.CommonsMultipartResolver {

HttpServletRequest request;

@Override
protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {

ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
upload.setSizeMax(-1);
if (request != null) {
mProgressListener pListener = new mProgressListener();
upload.setProgressListener(pListener);
request.getSession().setAttribute("pListener", pListener);
}

return upload;
}

@Override
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest arg0)
throws MultipartException {
this.request = arg0;
return super.resolveMultipart(arg0);
}
}

3,spring-servlet.xml 修改为

<!-- 支持上传文件 -->
<bean id="multipartResolver" class="com.glass.test.CommonsMultipartResolver">
<property name="maxUploadSize" value="-1" />
</bean>

 

4,jsp界面运用ajax实现数据的互交,

用到AjaxFileUpload.js 实现文件的异步传输

<script type="text/javascript">

$(function(){

$("#submin").click(function(){

  $.ajaxFileUpload({
    url: ‘upLoadProgress.htm‘,
    type: ‘post‘,
    secureuri: false, //一般设置为false
    fileElementId: ‘file‘, // 上传文件的id、name属性名
    dataType: ‘text‘, //返回值类型,一般设置为json、application/json
    // elementIds: elementIds, //传递参数到服务器
    success: function(data, status){
    layer.alert(data);
    },
      error: function(data, status, e){
    alert(e);
    }
  });
// $("#pform").submit();

// $("$pCount").text("51465") ;

startProgress();
return false;


});
function startProgress(){

$.ajax({
type: "post",
dataType: "text",
url: "testProgress.htm",
data: "",
success: function (data) {//data是没有小数的整型,
var num= parseInt(data)*0.01;
$("#flownum").text(data+"%") ;//显示到界面
$("#pCount").attr("width",(1000*num)+"px");
if(num<1){
setTimeout(startProgress(),2000);
}
// layer.alert(data);
}

});}

});

</script>

4,Controller 接收上传

5,Progress的controller如下:


@RequestMapping(value ="testProgress")
public void progress(HttpServletResponse response,
HttpServletRequest request) {
mProgressListener pListener= (mProgressListener) request.getSession().getAttribute("pListener");
setMessage(pListener.getpCount());//自己写的方法
Ajax(response);//自己写的方法

}

 

Spring mvc 上传进度条实现

标签:

原文地址:http://www.cnblogs.com/guoke-jsp/p/4341449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!