资源简介
jsupload实现js上传文件,并显示文件上传进度
代码片段和文件信息
package com.sohu.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.FileUploadbase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class Upload
*/
@WebServlet(“/Upload“)
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Upload() {
// TODO Auto-generated constructor stub
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
doPost(request response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
response.setContentType(“text/html“);
//设置字符编码为UTF-8,支持汉字显示
response.setCharacterEncoding(“UTF-8“);
final long MAX_SIZE = 1024 * 1024 * 1024;//设置上传文件最大为1G
//上传文件路径
String filePath = “e:\\upload\\“;
String tempPath = “e:\\upload\\temp\\“;
//jsonp上传
String jsonp = request.getParameter(“callback“);
// 实例化一个硬盘文件工厂用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(1024 * 5);// 设置上传文件时用于临时存放文件的内存大小这里是5k.多于的部分将临时存在硬盘
dfif.setRepository(new File(tempPath));// 设置存放临时文件的目录
//用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
//设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
final HttpSession session = request.getSession();
sfu.setProgressListener(new ProgressListener() {
private long temp = -1;
public void update(long readBytes long totalBytes int item) {
long size = readBytes / 1024 * 1024 * 10;
if(temp == size){
return;
}
temp = size;
if (readBytes != -1) {
session.setAttribute(“readBytes“ ““ + readBytes);
session.setAttribute(“totalBytes“ ““ + totalBytes);
}
}
});
//从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {//处理文件尺寸过大异常
if (e instanceof SizeLimitE
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-01-30 16:15 jsupload\
文件 601 2012-01-30 09:17 jsupload\.classpath
文件 1041 2012-01-29 18:32 jsupload\.project
目录 0 2012-01-30 16:15 jsupload\.settings\
文件 503 2012-01-29 18:32 jsupload\.settings\.jsdtscope
文件 395 2012-01-29 18:32 jsupload\.settings\org.eclipse.jdt.core.prefs
文件 454 2012-01-29 18:32 jsupload\.settings\org.eclipse.wst.common.component
文件 305 2012-01-29 18:32 jsupload\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2012-01-29 18:32 jsupload\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2012-01-29 18:32 jsupload\.settings\org.eclipse.wst.jsdt.ui.superType.name
目录 0 2012-01-30 16:15 jsupload\src\
目录 0 2012-01-30 16:15 jsupload\src\com\
目录 0 2012-01-30 16:15 jsupload\src\com\sohu\
目录 0 2012-01-30 16:15 jsupload\src\com\sohu\action\
文件 6414 2012-01-29 17:48 jsupload\src\com\sohu\action\Upload.java
文件 2839 2012-01-20 16:48 jsupload\src\com\sohu\action\UploadFileMessage.java
文件 1578 2012-01-29 17:00 jsupload\src\com\sohu\action\UploadProgress.java
目录 0 2012-01-30 16:15 jsupload\WebContent\
目录 0 2012-01-30 16:15 jsupload\WebContent\me
文件 39 2012-01-29 18:32 jsupload\WebContent\me
目录 0 2012-01-30 16:15 jsupload\WebContent\page\
目录 0 2012-01-30 16:15 jsupload\WebContent\page\sc
文件 10511 2012-01-30 15:20 jsupload\WebContent\page\sc
文件 6505 2012-01-30 14:27 jsupload\WebContent\page\upload.jsp
目录 0 2012-01-30 16:15 jsupload\WebContent\WEB-INF\
目录 0 2012-01-30 16:15 jsupload\WebContent\WEB-INF\classes\
目录 0 2012-01-30 16:15 jsupload\WebContent\WEB-INF\classes\com\
目录 0 2012-01-30 16:15 jsupload\WebContent\WEB-INF\classes\com\sohu\
目录 0 2012-01-30 16:15 jsupload\WebContent\WEB-INF\classes\com\sohu\action\
文件 1325 2012-01-30 09:19 jsupload\WebContent\WEB-INF\classes\com\sohu\action\Upload$1.class
文件 5702 2012-01-30 09:19 jsupload\WebContent\WEB-INF\classes\com\sohu\action\Upload.class
............此处省略7个文件信息
相关资源
- WEB页面一个上传Excel文件并将内容保存
- oss文件上传
- webservice文件上传.zip
- K8_Struts2_EXP S2-045 & 任意文件上传 201
- Axure实现文件上传效果
- 一个简单的木马程序源代码,实现文
- 文件上传组件FileUpload
- 调用百度网盘接口进行文件上传
- jfinal下的多文件上传以及对excel的导入
- 使用QT实现文件上传和----ftp服务器
- bootstrap fileinput完整插件
-
1次ajax请求(xm
lHttpRequest)上传多个 - ossUploader阿里云OSS对象云存储上传控件
- 带进度条文件上传springboot+vue+axios+m
- FTP文件上传及日志配置和使用源码
- UDP文件上传和
- Qt5.8用FTP实现文件上传和(带进度条)
- flash 多文件上传组件
- Webapi 文件上传
- spring mvc 初始环境搭建,前后台数据的
- SOCKET实现FTP文件上传
- easyui webserver 实现文件上传
- springboot单文件和多文件上传
- 在单片机上实现http upload 小文件上传
- socket读取文件上传并在服务器从新写
评论
共有 条评论