Java上传文件进度条的实现方法(附demo源码下载)
作者:李超@hicc 时间:2023-06-06 11:06:16
本文实例讲述了Java上传文件进度条的实现方法。分享给大家供大家参考,具体如下:
东西很简单,主要用到commons-fileupload,其中有一个progressListener的接口,该接口可以实现实时更新已上传文件的大小,有了这个还说什么呢?
这里给出代码:
package lc.progress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import lc.progress.vo.fileUploadStatus;
import org.apache.commons.fileupload.ProgressListener;
public class myProgressListener implements ProgressListener {
private HttpSession session;
public myProgressListener(HttpServletRequest req) {
session=req.getSession();
fileUploadStatus status = new fileUploadStatus();
session.setAttribute("status", status);
}
/* pBytesRead 到目前为止读取文件的比特数
* pContentLength 文件总大小
* pItems 目前正在读取第几个文件
* 只要在session中实时保存文件上传的状态(这里我用fileUploadStatus类来封装)
*/
public void update(long pBytesRead, long pContentLength, int pItems) {
// TODO Auto-generated method stub
fileUploadStatus status = (fileUploadStatus) session.getAttribute("status");
status.setPBytesRead(pBytesRead);
status.setPContentLength(pContentLength);
status.setPItems(pItems);
}
}
然后在上传得servlet或action中加入这样一段代码,就可以把自定义的progressListener添加进去
myProgressListener getBarListener = new myProgressListener(req);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setProgressListener(getBarListener);
最后就是通过js来不断的访问另一个servlet来实时返回上传状态就可以了,限于篇幅我就不再贴代码了,有兴趣的读者可以自己下载来看。
完整实例代码点击此处本站下载。
希望本文所述对大家Java程序设计有所帮助。
标签:Java,上传文件,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java的MyBatis框架中实现多表连接查询和查询结果分页
2022-10-29 08:49:21
![](https://img.aspxhome.com/file/2023/7/72397_0s.png)
使用Android造了个滚轮控件轮子示例
2023-04-29 07:09:17
![](https://img.aspxhome.com/file/2023/6/106866_0s.gif)
C#算法之实现阿姆斯特朗数
2022-08-22 17:57:24
![](https://img.aspxhome.com/file/2023/1/122681_0s.jpg)
FastJSON字段智能匹配踩坑的解决
2022-07-29 19:32:26
![](https://img.aspxhome.com/file/2023/0/64540_0s.png)
Spring Boot中操作使用Redis实现详解
2023-11-24 02:39:29
![](https://img.aspxhome.com/file/2023/9/85399_0s.png)
Android存储字符串数据到txt文件
2021-11-05 21:55:16
详解C# 匿名对象(匿名类型)、var、动态类型 dynamic
2022-03-26 18:43:02
![](https://img.aspxhome.com/file/2023/8/72458_0s.png)
Android按钮按下的时候改变颜色实现方法
2021-09-24 20:15:39
![](https://img.aspxhome.com/file/2023/1/137991_0s.jpg)
C#飞行棋小程序设计分析
2023-06-05 05:27:24
![](https://img.aspxhome.com/file/2023/6/96396_0s.png)
Springboot如何根据实体类生成数据库表
2023-11-20 13:54:39
![](https://img.aspxhome.com/file/2023/7/59677_0s.jpg)
Android实现本地图片选择及预览缩放效果
2023-09-27 14:07:28
![](https://img.aspxhome.com/file/2023/3/93313_0s.gif)
简单解析java方法在调用在内存中的执行过程
2022-04-12 15:46:26
![](https://img.aspxhome.com/file/2023/6/59756_0s.png)
SpringBoot整合RocketMQ的详细过程
2023-07-10 05:07:00
![](https://img.aspxhome.com/file/2023/6/60256_0s.jpg)
java实现面板之间切换功能
2021-12-03 15:06:39
![](https://img.aspxhome.com/file/2023/5/72655_0s.jpg)
关于C#理解装箱与拆箱
2023-06-18 21:07:50
Android创建简单发送和接收短信应用
2022-01-04 18:31:08
![](https://img.aspxhome.com/file/2023/0/106820_0s.png)
快速解决Android适配底部返回键等虚拟键盘的问题
2021-10-25 14:50:23
Android实现本地Service方法控制音乐播放
2021-12-16 07:22:31
![](https://img.aspxhome.com/file/2023/0/129140_0s.jpg)
Android中将View的内容保存为图像的简单实例
2023-11-01 12:45:41
SpringBoot自定义加载yml实现方式,附源码解读
2022-01-22 22:39:40
![](https://img.aspxhome.com/file/2023/5/86025_0s.jpg)