ajax文件上传核心代码
来源:互联网 发布:虚拟淘宝店怎么开 编辑:程序博客网 时间:2024/06/05 09:19
ajax文件上传核心代码
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());
session.setAttribute("bytesRead","0");
// 创建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,session);// 调用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,HttpSession session) {
// 获得上传文件的文件名
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;
long bytesRead = Long.parseLong((String) session.getAttribute("bytesRead"));// 已读数据大小
while ((bytes_read = in.read(buffer)) != -1) // Read until EOF
{
out.write(buffer, 0, bytes_read);
bytesRead+=(long)bytes_read;
session.setAttribute("bytesRead",String.valueOf(bytesRead));
}
UploadListener.FileUploadStats fileUploadStats = (UploadListener.FileUploadStats) session
.getAttribute("FILE_UPLOAD_STATS");
long sizeTotal = fileUploadStats.getTotalSize();// 获得上传文件的总大小
session.setAttribute("bytesRead",String.valueOf(sizeTotal));
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 +fileUploadStats.getBytesRead();
//System.out.println(bytesProcessed);
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>上传状态:</b><br/>");
if (bytesProcessed != sizeTotal) {
response.getWriter().println(
"<div class=/"prog-border/"><div class=/"prog-bar/" style=/"width: "
+ percentComplete + "%;/"></div></div>");
response.getWriter().println(
"完成: " + bytesProcessed + " 总大小: " + sizeTotal
+ " bytes (" + percentComplete + "%) "
+ (long) Math.round(uploadRate / 1024)
+ " Kbs <br/>");
response.getWriter().println(
"用时: " + formatTime(timeInSeconds) + " 总时间: "
+ formatTime(estimatedRuntime) + " 剩余时间:"
+ formatTime(estimatedRuntime - timeInSeconds)
+ "<br/>");
} else {
response.getWriter().println(
"完成: " + bytesProcessed + " 总大小: " + sizeTotal
+ " bytes<br/>");
response.getWriter().println("上传完成.<br/>");
}
//如果文件已经上传完毕
if (fileUploadStats.getBytesRead() == fileUploadStats
.getTotalSize()) {
response.getWriter().println("<b>上传完成,正在保存文件... ...</b><br/>");
//写文件
bytesProcessed =Long.parseLong((String) session.getAttribute("bytesRead"));
percentComplete = (long) Math
.floor(((double) bytesProcessed / (double) sizeTotal) * 100.0);
if (bytesProcessed != sizeTotal) {
response.getWriter().println(
"<div class=/"prog-border/"><div class=/"prog-bar/" style=/"width: "
+ percentComplete + "%;/"></div></div>");
response.getWriter().println(
"保存: " + bytesProcessed + " 总大小: " + sizeTotal
+ " bytes (" + percentComplete + "%) <br/>");
} else {
response.getWriter().println(
"保存: " + bytesProcessed + " 总大小: " + sizeTotal
+ " bytes<br/>");
response.getWriter().println("保存完成.<br/>");
}
}
}
}
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文件上传核心代码
- ASP文件上传核心代码
- java sftp文件上传核心代码
- ajax+php文件上传代码 - php上传下载
- formdata ajax上传文件前后台代码
- ajax核心代码
- AJAX核心代码
- 手写Ajax核心代码
- ajax核心代码(转载)
- Ajax后台核心代码
- Ajax核心代码
- Ajax核心代码
- jquery ajax实现上传文件代码,带进度条
- jquery ajax实现上传文件代码,带进度条
- php+ajax实现图片文件上传实例代码
- 阿里云oss post上传文件 JavaScript代码示例 $.ajax()
- FileUpload上传组件核心代码
- 文件下载核心代码
- 儒豹牛年元月手机搜索关键词
- Template Method模式
- 使用define_method
- Redhat Linux9 DHCP服务的安装及配置
- 用vc实现对qq发送按钮消息的截获
- ajax文件上传核心代码
- DES加密解密
- 归并排序
- 分析mirand事件
- java 数据结构 遍历链表程序
- DotNetNuke
- C#调用 C++
- java 五子棋中的AI技术
- Meditation(part 1)