Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能
最近在做一個(gè)資源共享的項(xiàng)目中,采用了Struts2.1.8+Spring2.5.6+hibernate3.32的框架整合方式進(jìn)行開(kāi)發(fā)。在文件上傳這塊,因?yàn)樾枰獙?shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條的功能,所以嘗試了一下。怕以后忘記,先貼出來(lái)分享下。
要在上傳文件時(shí)能顯示進(jìn)度條,首先需要實(shí)時(shí)的獲知web服務(wù)端接收了多少字節(jié),以及文件總大小,這里我們?cè)陧?yè)面上使用AJAX技術(shù)每一秒向服務(wù)器發(fā)送一次請(qǐng)求來(lái)獲得需要的實(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自帶的解析器類沒(méi)有添加progressListener),然后就可以了。下面看看主要的代碼(技術(shù)有限,如有不對(duì)之處,望不吝點(diǎn)解):
監(jiān)聽(tīng)器:
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)自己的解析器類:方法比較簡(jiǎn)單,找到struts2實(shí)現(xiàn)的解析器類,把代碼拷貝過(guò)來(lái)然后添加上監(jiān)聽(tīng)器即可。這個(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)聽(tīng)器
upload.setProgressListener(progressListener);//添加自己的監(jiān)聽(tīng)器
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è)用來(lái)接收上傳文件,以及對(duì)接收的文件作相應(yīng)處理,處理完成后,在return SUCCESS之前去除session中currentUploadStatus屬性,一個(gè)用來(lái)為頁(yè)面讀取實(shí)時(shí)上傳進(jìn)度服務(wù),這個(gè)類中只要將session中的currentUploadStatus對(duì)象拿出來(lái)按照相應(yīng)格式返回給客戶端即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Jackson配置全局時(shí)間日期格式
本文主要介紹了SpringBoot使用Jackson配置全局時(shí)間日期格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
詳解如何給Sprintboot應(yīng)用添加插件機(jī)制
這篇文章主要為大家介紹了如何給 Sprintboot 應(yīng)用添加插件機(jī)制,文中有詳細(xì)的解決方案及示例代碼,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08
java web FTPClient實(shí)現(xiàn)上傳文件到指定服務(wù)器
這篇文章主要為大家詳細(xì)介紹了java web FTPClient實(shí)現(xiàn)上傳文件到指定服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Java文件過(guò)濾器實(shí)現(xiàn)按條件篩選文件
本文主要介紹了Java文件過(guò)濾器實(shí)現(xiàn)按條件篩選文件,文件過(guò)濾器是在文件處理中起到重要作用的工具,它可以用來(lái)篩選文件并根據(jù)特定的條件進(jìn)行過(guò)濾,下面就來(lái)介紹一下2024-04-04
SpringMVC修改返回值類型后的消息轉(zhuǎn)換器處理方式
這篇文章主要介紹了SpringMVC修改返回值類型后的消息轉(zhuǎn)換器處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
spring boot下 500 404 錯(cuò)誤頁(yè)面處理的方法
本篇文章主要介紹了spring boot下 500 404 錯(cuò)誤頁(yè)面處理的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
java GUI編程之布局控制器(Layout)實(shí)例分析
這篇文章主要介紹了java GUI編程之布局控制器(Layout),結(jié)合實(shí)例形式分析了java GUI編程中布局控制器(Layout)具體功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01

