commons-fileupload 上传实例
来源:互联网 发布:ff14好看的捏脸数据 编辑:程序博客网 时间:2024/05/21 07:15
使用commons-fileupload 上传
在通过使用FileUpload组件上传的过程中,通过自己的调试,总结如下:
1)使用之前的准备,我用的是commons-fileupload-1.1-dev.jar和commons-io-1.1-dev.jar。由于在parseRequest(request)的类有关继承于DiskFileItem
类。而他有private org.apache.commons.io.output.DeferredFileOutputStream dfos。这样的就必须使用到commons-io-1.1-dev.jar。因此需要导入该包。否则就出classNotFound:.DeferredFileOutputStream的错误。
package com.cms.ui;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class AddUser extends HttpServlet {
public final static Log log = LogFactory.getLog(FileUpload.class);
String regExp = ".+////(.+)$";
String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
Pattern p = Pattern.compile(regExp);
Map userInfoMap = new HashMap();
public Map getUserInfoMap() {
return userInfoMap;
}
public void setUserInfoMap(Map userInfoMap) {
this.userInfoMap = userInfoMap;
}
public void destroy() {
super.destroy();
}
private void processFormField(FileItem item) {
String name = item.getFieldName();
String value = item.getString();
log.info(name + " : " + value);
this.getUserInfoMap().put(name, value);
// String submit = name;
/*
* username : tt password : ttttt password1 : ttttttttt sex : 1 birthday :
* birthday_month : -1 birthday_day : -1 birthday_year : -1 tel : tt
* email : e address : a question : answer : aaa resume : ddddd submit :
* submit if (StringUtils.isNotEmpty(name) && StringUtils.equals(name,
* "submit")) { String username = request.getParameter("username");
* BasetableFactory bf = BasetableFactory.getInstance(); String sql =
* "insert into user(username) value('" + username + "')"; if
* (bf.insertUser(sql)) { log.info("insert user success"); } else {
* log.info("insert user fail"); } }
*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filepath = URLDecoder.decode(this.getClass().getClassLoader()
.getResource("../../uploads").getPath(), "UTF-8");
String tempPath = filepath + "/temp";
log.info(filepath);
log.info(request.getContentLength());
log.info(request.getContentType());
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000000);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
}else{
processUploadedFile(item, filepath);
//continue;
}
}
} catch (FileUploadException e) {
log.info(e);
}
log.info(this.getUserInfoMap());
}
private void processUploadedFile(FileItem item, String filepath) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
return;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
log.info(name + ": wrong type");
}
}
try {
item.write(new File(filepath + "/" + m.group(1)));
log.info(name + "/r/n" + size + "<br>");
} catch (Exception e) {
log.info(e);
}
} else {
log.info("fail to upload");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
- commons-fileupload 上传实例
- commons-fileupload上传简单实例
- commons-fileupload-1.0上传组件使用实例.
- commons-fileupload-1.0上传组件使用实例.
- commons-fileupload实现文件上传功能实例
- commons-fileupload实现文件上传功能实例
- Commons-FileUpload上传组件
- 文件上传 commons-FileUpload
- Commons FileUpload上传文件
- Commons FileUpload上传文件
- commons-fileupload-文件上传
- commons fileupload 上传方法
- commons fileupload 文件上传
- commons-fileupload上传文件
- 文件上传 commons-fileupload
- commons-fileupload上传失败
- 利用commons-fileupload组件实现文件上传-实例
- 利用commons-fileupload组件实现文件上传-实例
- SDK 实现程序自删除
- 注入IE后使用内存不过4500K
- IRAF安装
- SQL Server补丁安装常见问题
- windows vs linux
- commons-fileupload 上传实例
- 感觉还不错的声音 ——YUI 吉冈 唯
- 李阳疯狂英语突破对话(25)-谢谢你的盛情邀请
- vc++ 2005 发布程序
- 字符串输入输出函数
- 笔记本电池保养
- MM—公司委托加工流程(转贴)
- 技术揭密之:DedeCMS存储过程
- September 3th Monday (九月 三日 月曜日)