Struts2实现文件上传时显示进度条功能
作者:surmounting 发布时间:2021-10-13 05:22:22
最近在做一个资源共享的项目中,采用了Struts2.1.8+Spring2.5.6+hibernate3.32的框架整合方式进行开发。在文件上传这块,因为需要实现文件上传时显示进度条的功能,所以尝试了一下。怕以后忘记,先贴出来分享下。
要在上传文件时能显示进度条,首先需要实时的获知web服务端接收了多少字节,以及文件总大小,这里我们在页面上使用AJAX技术每一秒向服务器发送一次请求来获得需要的实时上传信息。但是当我们使用struts2后怎么在服务端获得实时的上传大小呢?这里需要用到commons-fileupload中的progressListener接口,实现这个接口,然后再实现一个自己的解析器,并在解析器中添加自己实现的那个progressListener;然后再替换struts2自带的解析器(struts2自带的解析器类没有添加progressListener),然后就可以了。下面看看主要的代码(技术有限,如有不对之处,望不吝点解):
* :
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
public class ResourceProgressListener implements ProgressListener {
private HttpSession session;
public ResourceProgressListener(HttpServletRequest request) {
session = request.getSession();
ResourceFileUploadStatus newUploadStatus = new ResourceFileUploadStatus();
session.setAttribute("currentUploadStatus", newUploadStatus);
}
public void update(long readedBytes, long totalBytes, int currentItem) {
ResourceFileUploadStatus status = (ResourceFileUploadStatus) session.getAttribute("currentUploadStatus");
status.setReadedBytes(readedBytes);
status.setTotalBytes(totalBytes);
status.setCurrentItem(currentItem);
}
}
上传状态类:
public class ResourceFileUploadStatus {
private long readedBytes = 0L;
private long totalBytes = 0L;
private int currentItem = 0;
public long getReadedBytes() {
return readedBytes;
}
public void setReadedBytes(long bytes) {
readedBytes = bytes;
}
public long getTotalBytes() {
return totalBytes;
}
public void setTotalBytes(long bytes) {
totalBytes = bytes;
}
public int getCurrentItem() {
return currentItem;
}
public void setCurrentItem(int item) {
currentItem = item;
}
}
实现自己的解析器类:方法比较简单,找到struts2实现的解析器类,把代码拷贝过来然后添加上 * 即可。这个类代码较多就不整个文件拷了,主要是在parse方法里添加。Parse方法代码如下:红色标注部分即是需要自己添加的progressListener.
public void parse(HttpServletRequest servletRequest, String saveDir)
throws IOException {
System.out.println("执行自定义MultiPartRequest");
DiskFileItemFactory fac = new DiskFileItemFactory();
// Make sure that the data is written to file
fac.setSizeThreshold(0);
if (saveDir != null) {
fac.setRepository(new File(saveDir));
}
// Parse the request
try {
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setSizeMax(maxSize);
ResourceProgressListener progressListener = new ResourceProgressListener(servletRequest);//新建一个 *
upload.setProgressListener(progressListener);//添加自己的 *
List items = upload.parseRequest(createRequestContext(servletRequest));
for (Object item1 : items) {
FileItem item = (FileItem) item1;
if (LOG.isDebugEnabled()) LOG.debug("Found item " + item.getFieldName());
if (item.isFormField()) {
LOG.debug("Item is a normal form field");
List<String> values;
if (params.get(item.getFieldName()) != null) {
values = params.get(item.getFieldName());
} else {
values = new ArrayList<String>();
}
String charset = servletRequest.getCharacterEncoding();
if (charset != null) {
values.add(item.getString(charset));
} else {
values.add(item.getString());
}
params.put(item.getFieldName(), values);
} else {
LOG.debug("Item is a file upload");
// Skip file uploads that don't have a file name - meaning that no file was selected.
if (item.getName() == null || item.getName().trim().length() < 1) {
LOG.debug("No file has been uploaded for the field: " + item.getFieldName());
continue;
}
List<FileItem> values;
if (files.get(item.getFieldName()) != null) {
values = files.get(item.getFieldName());
} else {
values = new ArrayList<FileItem>();
}
values.add(item);
files.put(item.getFieldName(), values);
}
}
} catch (FileUploadException e) {
LOG.warn("Unable to parse request", e);
errors.add(e.getMessage());
}
}
上面的类建立完成后,还需要做一项工作:在struts.xml中添加如下内容:
<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="requestParser"
class="com.zeige.ResourceMultiPartRequest" scope="default" optional="true" />
<constant name="struts.multipart.handler" value="requestParser" />
下面就可以正常使用了,建立两个action,一个用来接收上传文件,以及对接收的文件作相应处理,处理完成后,在return SUCCESS之前去除session中currentUploadStatus属性,一个用来为页面读取实时上传进度服务,这个类中只要将session中的currentUploadStatus对象拿出来按照相应格式返回给客户端即可。
猜你喜欢
- Maven热部署,顾名思义就是可以不影响项目在服务器中的运行情况,可以实现项目代码的更新,减少启动,编译时间,达到快速开发的目的,也不需要手
- 一、项目概述之前有不少粉丝私信我说,能不能用Android原生的语言开发一款在手机上运行的游戏呢?说实话,使用java语言直接开发游戏这个需
- 前言在一个名为种花家的小镇上,生活着一群热爱编程的人。他们致力于构建出高效、可维护的软件系统,而 Spring Boot 框架成为了他们的不
- ES是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index)有点类似,都是基于分词和分
- 线程安全解决方案synchronized,ReentrantLock,Atomic 使用场景描述在实际开发过程中如果服务量,请求频繁,就会经
- 就我们所知道的,java中有子类和父类,子类由于继承父类而形成,那么父类还有没有父类呢?答案是有了,父类的父类就是object类,一切父类都
- — 遇到问题今天在IDEA里面运行项目的时候报了一个错,如下图所示:— 找到问题根源其实控制台给出的错误信息提示说的很明显:类加载器加载文件
- 重写 equals()方法 和 hashCode()方法最近看了学习了集合的简单的知识,碰到了讲解 Set 的部分,感觉很好奇,这里对于 S
- 环境操作系统windows10JDKjdk1.8.0_192IDEEclipse IDE for Enterprise Java Devel
- 1、用ASCII码判断在 ASCII码表中,英文的范围是0-127,而汉字则是大于127,具体代码如下:string text = &quo
- jpa EntityManager复杂查询概念EntityManager:EntityManager是JPA中用于增删改查的接口,它的作用相
- 注释介绍@Cacheable@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存@Cacheable 作用
- 页面提交请求参数有两种,一种是form格式提交,一种json格式提交通常情况下我们使用的都是form格式提交的数据,数据格式:k=v&
- 这篇文章主要介绍了springboot日期转换器实现实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- Springboot启动不检查JPA的数据源配置1.问题有时我们使用spring boot ,在依赖中配置了spring data jpa的
- Jetty和tomcat的比较Tomcat和Jetty都是一种Servlet引擎,他们都支持标准的servlet规范和JavaEE的规范。架
- 以最终客户的角度来看,JAR文件就是一种封装,他们不需要知道jar文件中有多少个.class文件,每个文件中的功能与作用,同样可以得到他们希
- Map接口简介Map接口是一种双列集合,它的每个元素都包含一个键对象Key和值对象Value,键和值对象之间存在一种对应关系,称为映射。从M
- 背景最近好几个项目在运行过程中客户都提出文件上传大小的限制能否设置的大一些,用户经常需要上传好几个G的资料文件,如图纸,视频等,并且需要在上
- 通用配置#下面介绍的整合JDBC和整合MyBatis都需要添加的实体类和配置数据库表#CREATE TABLE `user` ( `id`