具体请下载源代码:
(1)写文件上传进度监听器类:
package com.my.other;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
public class UploadProgressListener implements ProgressListener {
private long megaBytes = -1;
HttpServletRequest request = null;
UploadInfo progressInfo = null;
public UploadProgressListener(HttpServletRequest request) {
this.request = request;
progressInfo = (UploadInfo)request.getSession().getAttribute("uploadInfo");
if (progressInfo == null) {
progressInfo = new UploadInfo();
request.getSession().setAttribute("uploadInfo", progressInfo);
}
}
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 100000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
if (pContentLength == -1) {
progressInfo.setStatus("done");
} else {
progressInfo.setFileIndex(pItems);
progressInfo.setTotalSize(pContentLength);
progressInfo.setBytesRead(pBytesRead);
progressInfo.setStatus("progress");
}
}
}
(2)在action中代码如下:
package com.my.other;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("utf-8");
UploadProgressListener pl = new UploadProgressListener(request);
long start_time = System.currentTimeMillis();
DiskFileUpload fu = new DiskFileUpload();
fu.setProgressListener(pl);
List items = fu.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
FileItem fi = (FileItem)items.get(i);
if (fi.isFormField()) {
} else {
if ("".equals(fi.getName().trim())) {
continue;
}
//取文件内容
String filename = fi.getName();
int pos = filename.lastIndexOf("\\");
filename = filename.substring(pos + 1);
fi.write(new File(this.getServlet().getServletContext().getRealPath("//")
+ "//files//" + filename));
}
}
long end_time = System.currentTimeMillis();
System.out.println("Used time:" + (end_time - start_time ) );
return mapping.findForward("success");
}
}
(3)dwr.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting
2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="UploadMonitor" creator="new">
<param name="class">com.my.other.UploadMonitor</param>
</create>
<convert match="com.my.other.UploadInfo" converter="bean"></convert>
</allow>
</dwr>
(4)dwr调用的服务类:
package com.my.other;
import org.directwebremoting.WebContextFactory;
import javax.servlet.http.HttpServletRequest;
public class UploadMonitor
{
public UploadInfo getUploadInfo()
{
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
if (req.getSession().getAttribute("uploadInfo") != null)
return (UploadInfo) req.getSession().getAttribute("uploadInfo");
else {
return new UploadInfo();
}
}
}
(5)进度信息对象
package com.my.other;
public class UploadInfo
{
private long totalSize = 0;
private long bytesRead = 0;
private long elapsedTime = 0;
private String status = "done";
private int fileIndex = 0;
public UploadInfo()
{
}
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
public long getTotalSize()
{
return totalSize;
}
public void setTotalSize(long totalSize)
{
this.totalSize = totalSize;
}
public long getBytesRead()
{
return bytesRead;
}
public void setBytesRead(long bytesRead)
{
this.bytesRead = bytesRead;
}
public long getElapsedTime()
{
return elapsedTime;
}
public void setElapsedTime(long elapsedTime)
{
this.elapsedTime = elapsedTime;
}
public boolean isInProgress()
{
return "progress".equals(status) || "start".equals(status);
}
public int getFileIndex()
{
return fileIndex;
}
public void setFileIndex(int fileIndex)
{
this.fileIndex = fileIndex;
}
}
(6)页面文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='/StrutsFileUpload/dwr/interface/UploadMonitor.js'></script>
<script type='text/javascript' src='/StrutsFileUpload/dwr/engine.js'></script>
<script type='text/javascript' src='/StrutsFileUpload/dwr/util.js'></script>
<script type='text/javascript' src='/StrutsFileUpload/resources/upload.js'> </script>
<style type="text/css">
#progressBar { padding-top: 5px; }
#progressBarBox { width: 350px; height: 20px; border: 1px inset; background: #eee;}
#progressBarBoxContent { width: 0; height: 20px; border-right: 1px solid #444; background: #9ACB34; }
</style>
</head>
<body>
<h1>文件上传演示</h1><br>
<form action="upload.do" method="post" enctype="multipart/form-data" οnsubmit="startProgress()">
学生编码:<input type="text" name="code" value="001"><br>
学生姓名:<input type="text" name="name" value="zhangsan"><br>
附件1:<input type="file" name="file1" id="file1"><br>
附件2:<input type="file" name="file2" id="file2"><br>
<div id="progressBar" style="display: none">
<div id="theMeter">
<div id="progressBarText"></div>
<div id="progressBarBox">
<div id="progressBarBoxContent"></div>
</div>
</div>
</div>
<input type="submit" value="提交" id="uploadbutton">
<input type="reset">
</form>
</body>
</html>