Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能
最近在做一個(gè)資源共享的項(xiàng)目中,采用了Struts2.1.8+Spring2.5.6+hibernate3.32的框架整合方式進(jìn)行開發(fā)。在文件上傳這塊,因?yàn)樾枰獙?shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條的功能,所以嘗試了一下。怕以后忘記,先貼出來分享下。
要在上傳文件時(shí)能顯示進(jìn)度條,首先需要實(shí)時(shí)的獲知web服務(wù)端接收了多少字節(jié),以及文件總大小,這里我們在頁面上使用AJAX技術(shù)每一秒向服務(wù)器發(fā)送一次請求來獲得需要的實(shí)時(shí)上傳信息。但是當(dāng)我們使用struts2后怎么在服務(wù)端獲得實(shí)時(shí)的上傳大小呢?這里需要用到commons-fileupload中的progressListener接口,實(shí)現(xiàn)這個(gè)接口,然后再實(shí)現(xiàn)一個(gè)自己的解析器,并在解析器中添加自己實(shí)現(xiàn)的那個(gè)progressListener;然后再替換struts2自帶的解析器(struts2自帶的解析器類沒有添加progressListener),然后就可以了。下面看看主要的代碼(技術(shù)有限,如有不對之處,望不吝點(diǎn)解):
監(jiān)聽器:
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);
}
}
上傳狀態(tài)類:
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;
}
}
實(shí)現(xiàn)自己的解析器類:方法比較簡單,找到struts2實(shí)現(xiàn)的解析器類,把代碼拷貝過來然后添加上監(jiān)聽器即可。這個(gè)類代碼較多就不整個(gè)文件拷了,主要是在parse方法里添加。Parse方法代碼如下:紅色標(biāo)注部分即是需要自己添加的progressListener.
public void parse(HttpServletRequest servletRequest, String saveDir)
throws IOException {
System.out.println("執(zhí)行自定義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);//新建一個(gè)監(jiān)聽器
upload.setProgressListener(progressListener);//添加自己的監(jiān)聽器
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());
}
}
上面的類建立完成后,還需要做一項(xiàng)工作:在struts.xml中添加如下內(nèi)容:
<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" />
下面就可以正常使用了,建立兩個(gè)action,一個(gè)用來接收上傳文件,以及對接收的文件作相應(yīng)處理,處理完成后,在return SUCCESS之前去除session中currentUploadStatus屬性,一個(gè)用來為頁面讀取實(shí)時(shí)上傳進(jìn)度服務(wù),這個(gè)類中只要將session中的currentUploadStatus對象拿出來按照相應(yīng)格式返回給客戶端即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Stream的基本概念以及創(chuàng)建方法
這篇文章主要介紹了Java Stream的基本概念以及創(chuàng)建方法,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-08-08
Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例
本文詳細(xì)講解了Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
解決maven常見錯(cuò)誤:Dependency is duplicated in
這篇文章主要介紹了解決maven常見錯(cuò)誤:Dependency is duplicated in file(s):問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享
這篇文章主要介紹了Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享,涉及攔截器的簡單介紹,攔截器和過濾器的區(qū)以及攔截器實(shí)現(xiàn)代碼等相關(guān)內(nèi)容,這里分享給大家,供需要的朋友參考。2017-10-10
SpringBoot打War包上傳到阿里云的LINUX服務(wù)器的操作方法
這篇文章主要介紹了SpringBoot打War包上傳到阿里云的LINUX服務(wù)器,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Java虛擬機(jī)之對象創(chuàng)建過程與類加載機(jī)制及雙親委派模型
這篇文章主要給大家介紹了關(guān)于Java虛擬機(jī)之對象創(chuàng)建過程與類加載機(jī)制及雙親委派模型的相關(guān)資料,本文通過示例代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11
java中char類型轉(zhuǎn)換成int類型的2種方法
這篇文章主要給大家介紹了關(guān)于java中char類型轉(zhuǎn)換成int類型的2種方法,因?yàn)閖ava是一門強(qiáng)類型語言,所以在數(shù)據(jù)運(yùn)算中會(huì)存在類型轉(zhuǎn)換,需要的朋友可以參考下2023-07-07
使用Java接收和處理OpenTelemetry數(shù)據(jù)的完整指南
在現(xiàn)代分布式系統(tǒng)中,OpenTelemetry 成為了一種常見的標(biāo)準(zhǔn),用于跟蹤和監(jiān)控應(yīng)用程序的性能和行為,OTLP是 OpenTelemetry 社區(qū)定義的一種數(shù)據(jù)傳輸協(xié)議,文將介紹如何使用 Java 編寫代碼來接收和處理 OTLP 數(shù)據(jù),需要的朋友可以參考下2024-04-04

