package book.ch20;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 创建HttpSession对象
HttpSession session = request.getSession();
if ("status".equals(request.getParameter("c"))) {// 如果请求中c的值为status
doStatus(session, response);// 调用doStatus方法
} else {// 否则,调用doFileUpload方法
doFileUpload(session, request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
private void doFileUpload(HttpSession session, HttpServletRequest request,
HttpServletResponse response) throws IOException {
try {
// 创建UploadListener对象
UploadListener listener = new UploadListener(request
.getContentLength());
listener.start();// 启动监听状态
// 将监听器对象的状态保存在Session中
session.setAttribute("FILE_UPLOAD_STATS", listener
.getFileUploadStats());
// 创建MonitoredDiskFileItemFactory对象
FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
// 通过该工厂对象创建ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 将转化请求保存到list对象中
List items = upload.parseRequest(request);
// 停止使用监听器
listener.done();
boolean hasError = false;
// 循环list中的对象
for (Iterator i = items.iterator(); i.hasNext();) {
FileItem fileItem = (FileItem) i.next();
if (!fileItem.isFormField()) {// 如果该FileItem不是表单域
processUploadedFile(fileItem);// 调用processUploadedFile方法,将数据保存到文件中
fileItem.delete();// 内存中删除该数据流
}
}
if (!hasError) {// 如果没有出现错误
sendCompleteResponse(response, null);// 调用sendCompleteResponse方法
} else {
sendCompleteResponse(response,
"Could not process uploaded file. Please see log for details.");
}
} catch (Exception e) {
sendCompleteResponse(response, e.getMessage());
}
}
public void processUploadedFile(FileItem item) {
// 获得上传文件的文件名
String fileName = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
// 创建File对象,将上传的文件保存到C:\\upload文件夹下
File file = new File("C:\\upload\\", fileName);
InputStream in;
try {
in = item.getInputStream();// 获得输入数据流文件
// 将该数据流写入到指定文件中
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[4096]; // To hold file contents
int bytes_read;
while ((bytes_read = in.read(buffer)) != -1) // Read until
// EOF
{
out.write(buffer, 0, bytes_read);
}
if (in != null)
try {
in.close();
} catch (IOException e) {
;
}
if (out != null)
try {
out.close();
} catch (IOException e) {
;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void doStatus(HttpSession session, HttpServletResponse response)
throws IOException {
// 设置该响应不在缓存中读取
response.addHeader("Expires", "0");
response.addHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.addHeader("Pragma", "no-cache");
// 获得保存在Session中的状态信息
UploadListener.FileUploadStats fileUploadStats = (UploadListener.FileUploadStats) session
.getAttribute("FILE_UPLOAD_STATS");
if (fileUploadStats != null) {
long bytesProcessed = fileUploadStats.getBytesRead();// 获得已经上传的数据大小
long sizeTotal = fileUploadStats.getTotalSize();// 获得上传文件的总大小
// 计算上传完成的百分比
long percentComplete = (long) Math
.floor(((double) bytesProcessed / (double) sizeTotal) * 100.0);
// 获得上传已用的时间
long timeInSeconds = fileUploadStats.getElapsedTimeInSeconds();
// 计算平均上传速率
double uploadRate = bytesProcessed / (timeInSeconds + 0.00001);
// 计算总共所需时间
double estimatedRuntime = sizeTotal / (uploadRate + 0.00001);
// 将上传状态返回给客户端
response.getWriter().println("<b>Upload Status:</b><br/>");
if (fileUploadStats.getBytesRead() != fileUploadStats
.getTotalSize()) {
response.getWriter().println(
"<div class=\"prog-border\"><div class=\"prog-bar\" style=\"width: "
+ percentComplete + "%;\"></div></div>");
response.getWriter().println(
"Uploaded: " + bytesProcessed + " out of " + sizeTotal
+ " bytes (" + percentComplete + "%) "
+ (long) Math.round(uploadRate / 1024)
+ " Kbs <br/>");
response.getWriter().println(
"Runtime: " + formatTime(timeInSeconds) + " out of "
+ formatTime(estimatedRuntime) + " "
+ formatTime(estimatedRuntime - timeInSeconds)
+ " remaining <br/>");
} else {
response.getWriter().println(
"Uploaded: " + bytesProcessed + " out of " + sizeTotal
+ " bytes<br/>");
response.getWriter().println("Complete.<br/>");
}
}
// 如果文件已经上传完毕
if (fileUploadStats != null
&& fileUploadStats.getBytesRead() == fileUploadStats
.getTotalSize()) {
response.getWriter().println("<b>Upload complete.</b>");
}
}
private void sendCompleteResponse(HttpServletResponse response,
String message) throws IOException {
if (message == null) {
response
.getOutputStream()
.print(
"<html><head><script type='text/ javascript'> function killUpdate() { window.parent.killUpdate(''); }</script></head><body onload='killUpdate()'></body></html>");
} else {
response
.getOutputStream()
.print(
"<html><head><script type='text/ javascript'>function killUpdate() { window.parent.killUpdate('"
+ message
+ "'); }</script></head><body onload='killUpdate()'></body></html>");
}
}
private String formatTime(double timeInSeconds) {
long seconds = (long) Math.floor(timeInSeconds);
long minutes = (long) Math.floor(timeInSeconds / 60.0);
long hours = (long) Math.floor(minutes / 60.0);
if (hours != 0) {
return hours + "hours " + (minutes % 60) + "minutes "
+ (seconds % 60) + "seconds";
} else if (minutes % 60 != 0) {
return (minutes % 60) + "minutes " + (seconds % 60) + "seconds";
} else {
return (seconds % 60) + " seconds";
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ajax登录
共352个文件
jpg:67个
js:66个
class:46个
需积分: 0 162 下载量 25 浏览量
2008-07-18
19:18:05
上传
评论
收藏 2.64MB RAR 举报
温馨提示
ajax登录代码 java<br> usefull
资源详情
资源评论
资源推荐
收起资源包目录
ajax登录 (352个子文件)
UploadServlet.class 8KB
Category.class 5KB
LayoutEO.class 5KB
treeviewRender.class 5KB
UserEO.class 4KB
LoadRSS.class 4KB
BasePortletClass.class 4KB
ContentEO.class 4KB
pageServlet.class 3KB
pictureServlet.class 3KB
LoginServlet.class 3KB
GetServlet.class 3KB
PortletEO.class 3KB
MessageEO.class 3KB
HomepageServlet.class 3KB
RssEO.class 3KB
DbManager.class 3KB
ChatEO.class 3KB
UserEO.class 3KB
SinaNews.class 2KB
XinhuaNews.class 2KB
YahooNews.class 2KB
SearchSuggest.class 2KB
RssServlet.class 2KB
Bookmark.class 2KB
AddFeedServlet.class 2KB
Util.class 2KB
SendServlet.class 2KB
ChatUtil.class 2KB
TreeviewServlet.class 2KB
RssUtil.class 2KB
AlbumEO.class 2KB
EncodingFilter.class 1KB
UploadListener$FileUploadStats.class 1KB
Util.class 1KB
MonitoredDiskFileItemFactory.class 1KB
UploadListener.class 1KB
MonitoredOutputStream.class 1KB
MonitoredDiskFileItem.class 1KB
Config.class 1KB
StudentEO.class 1KB
SliderUtil.class 957B
Welcome.class 913B
Calender.class 907B
TreeviewElement.class 269B
OutputStreamListener.class 235B
.classpath 2KB
all.css 58KB
cssplay.css 9KB
lightbox.css 2KB
calendar.css 2KB
new.css 2KB
styles.css 2KB
styles.css 1KB
lightbox_ie.css 1KB
lightbox.css 831B
tree.css 453B
Thumbs.db 296KB
Thumbs.db 13KB
Thumbs.db 4KB
loading.gif 2KB
portlethdr1.gif 833B
folder.gif 572B
last.gif 547B
first.gif 543B
prev.gif 529B
next.gif 527B
plus.gif 200B
minus.gif 197B
slider_bg.gif 170B
slider_grip.gif 101B
sheet.gif 75B
blank.gif 54B
asybase.gif 49B
clear.cache.gif 43B
clear.cache.gif 43B
clear.cache.gif 43B
clear.cache.gif 43B
clear.cache.gif 43B
D7C700B61DCDFD92A3DBCB2D2AAB8741.cache.html 32KB
E9A6819133D45D92CD0E53E4F63EDDE4.cache.html 31KB
BB58777D116FD6B080ED3AF5AF747C63.cache.html 31KB
AB98DB5094B88269A0C01A1A7A3DD6B1.cache.html 31KB
C1B05EDC9608E5C7BEF535EA357CFAC1.cache.html 30KB
2A7012BC9696C6C3A95CF78CF7417473.cache.html 30KB
D0966C6AF5F2D23FFE9CF17923951475.cache.html 30KB
9C07EA868C91E54D8885D9647DDF3D53.cache.html 30KB
ADB71FF8D05255808F5DD98D9635289E.cache.html 30KB
C86ABA36B2C711B5FF434B0B79831F33.cache.html 29KB
25B4CEDE5A75C9E997DC000778428411.cache.html 27KB
6DB7DCC3E807A5DCFF389375DBD97188.cache.html 27KB
321658A40E52D58C6B7F6A08D2718315.cache.html 26KB
45E1590A53A9F9FE5A396EFB80D32029.cache.html 26KB
827F9E3CEF421F6CCFD452235287676E.cache.html 26KB
96789F2821C05E9605235EB9CED352C8.cache.html 25KB
CF4E484369DBB30B27F6EF26862358BD.cache.html 25KB
D46E561912D86C2FA1A33B8E9475E341.cache.html 25KB
67062697D5DA5A5F077F5B6B89F5B7EE.cache.html 25KB
ABAC7D05F1155DDB899229EE2D5EF6C4.cache.html 24KB
FE42B373B69EBCDCCF0E518E0989A73F.cache.html 11KB
共 352 条
- 1
- 2
- 3
- 4
luoxiaohu
- 粉丝: 4
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0