基于fileupload文件批量上传
来源:互联网 发布:mindjet mac 破解版 编辑:程序博客网 时间:2024/05/17 23:08
执行类:
/**
* @author LXY
* @date 2008=11-11
*/
public class UploadAction extends HttpServlet {
private static final long serialVersionUID = 1L;
String uploadPath = ""; // 用于存放上传文件的目录
String tempPath = ""; // 用于存放临时文件的目录
@Override
public void init() throws ServletException {
super.init();
//从配置文件中读取文件路径
uploadPath = this.getInitParameter("uploadPath");
tempPath = this.getInitParameter("tempPath");
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//用于存放上传文件的原路径
List<String> fileList = new ArrayList<String>();
//创建存放上传文件的文件夹
File fileDir = new File(uploadPath);
if (!fileDir.exists()) {
fileDir.mkdir();
}
//创建临时文件夹
File tempDir = new File(tempPath);
if (!tempDir.exists()) {
tempDir.mkdir();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(tempPath)); // 设置临时目录
factory.setSizeThreshold(1024 * 1024); // 设置缓冲区大小
ServletFileUpload upload = new ServletFileUpload(factory);
//设置字符集
upload.setHeaderEncoding("UTF-8");
try {
List<FileItem> list = upload.parseRequest(request); // 得到所有的文件
for (FileItem fileItem : list) {
// 如果是表单信息
if (fileItem.isFormField()) {
String name = fileItem.getFieldName(); // 获得表单中按钮的类型
String value = fileItem.getString("UTF-8"); // 获得表单中按钮的值
request.setAttribute(name, value);
} else {
String name = fileItem.getFieldName(); //获得表单中域的名字
String value = fileItem.getName(); // 获得从浏览器中取得的文件全路径
//将上传文件的路径放到列表里
fileList.add(value);
int start = value.lastIndexOf("//");
String fileName = value.substring(start + 1); // 由于不同的浏览器可能取得的文件的名字不同,有的浏览器将整个路径取道,有的浏览器只取到文件名
request.setAttribute(name, fileName); // IE里取的是文件明,如XXX.doc等
// 使用fileupload提供的上传
try {
// 写入文件,也可以从fileName中提取文件名:
fileItem.write(new File(uploadPath, fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
//将获得的文件路径放回请求
request.setAttribute("fileList", fileList);
//页面跳转
request.getRequestDispatcher("success.jsp").forward(request, response);
}
}
http://episode.javaeye.com/blog/265478
- 基于fileupload文件批量上传
- fileupload实现多文件批量上传
- swfupload + commons-fileupload实现文件批量上传,带百分比进度条
- java servlet common-fileupload 实现的文件批量上传
- apache commons-fileupload实现进度条大文件批量上传
- beego文件上传,基于fileupload
- 文件批量上传
- 基于Fileupload控件的FTP上传
- 基于common-fileupload实现文件上传
- .NET多文件批量上传
- js实现文件批量上传
- struts 文件批量上传
- struts2文件批量上传
- 文件批量上传的实现
- java操作文件批量上传
- Struts2实现文件批量上传
- struts 文件批量上传
- 文件批量上传--smartUpload
- 通过命令行开启和关闭oracle
- 水晶头接法
- 【转帖】社会生活中十二大著名法则 也话对你有帮助
- 理解VS2005 编译程序后出现的 manifest 后缀文件的问题
- 什么是ROM、RAM、DRAM、SRAM和FLASH的区别
- 基于fileupload文件批量上传
- 古代人身高
- 简繁体文字转换工具
- 页面内容自动隐藏 css+javascript
- Unicode详解
- 动态加载用户自定义控件
- 复制文章时自动加入版权的网页特效
- 记录一下删除表分区的语句
- atof在EVC下的方便替换为strtod