jfinal 上传文件 进度条 问题

 最近看了一下jfinal

曾经用过 play 1

jfinal 对数据库的兼容性 相较 play 还是有一定的距离.

play是以model为准.不用操作数据库

jfinal是以数据库为准,基本不用操作model

jfinal的参数验证方式比较繁琐.play很优雅.jfinal controller方法不支持传参.,play这个比较灵活.不管是日期,对像还是常用数据类型都支持

 

jfinal render()  之前 可能需要setArg()   play render()参数名即可

 

jfinal 每个controller都需要映射路由. play有默认的路由规则.

 

当然..jfinal小巧,play相较jfinal 还是有些庞大.虽然已经很精简了.

 

jfinal的文件上传用的是cos组件.

cos组件里头没有提供像fileupload类似的 progresslistner

所以不方便拿到文件上传的进度

 

.一顿搜索.没见人解决过.

 

小人不才,看了一下cos上传代码..暴力解决 了一下.直接修改了cos的源码

 

修改如下:

修改 cos的 FilePart 类:

添加了一个本地静态变量与接口

public  interface Progress{
	  public void start();
	  public abstract void progress(long currentSize);
	  public void complate();
  }
  
public static ThreadLocal<Progress> progresss = new ThreadLocal<Progress>();

  

修改了 FilePart的 write(OutputStream out)

 

long write(OutputStream out) throws IOException {
    // decode macbinary if this was sent
    if (contentType.equals("application/x-macbinary")) {
      out = new MacBinaryDecoderOutputStream(out);
    }
    long size=0;
    int read;
    byte[] buf = new byte[8 * 1024];
    if(progresss.get()!=null)progresss.get().start();
    while((read = partInput.read(buf)) != -1) {
      out.write(buf, 0, read);
      size += read;
      if(progresss.get()!=null)
    	  progresss.get().progress(size);
    }
    if(progresss.get()!=null)progresss.get().complate();
    return size;
  }

 

 

controller中使用例子:

FilePart.progresss.set(new FilePart.Progress() {

			public void progress(long currentSize) {
				int totalsize = QuestionController.this.getRequest().getContentLength();
				double csize = (double) currentSize;
				double s = csize/totalsize*100;
				System.out.println(String .format("%.2f",s));
			}
			
			public void start() {
				
			}

			public void complate() {
				FilePart.progresss.remove();
			}
			
		});
		UploadFile file = getFile();
		file.getFile();

   打印结果:

0.16

0.33

0.49

0.66

0.82

0.99

1.15

1.32

1.48

等...........

单应用的情况下 将 进度值存入session,前台 ajax轮存获取完成量即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值