浏览 1267 次
锁定老帖子 主题:临时上传文件的代码
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-01
import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; 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; public class MutiFileTempUpload extends FileUploadBase { private Map<String, FileItem> files; private long filesSize = 0; @SuppressWarnings("unchecked") public Map<String, FileItem> parseRequest(HttpServletRequest request) throws UnsupportedEncodingException { files = new HashMap<String, FileItem>(); // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(sizeThreshold); if (repository != null) factory.setRepository(repository); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding(encoding); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { String fieldName = item.getFieldName(); String value = item.getString(encoding); parameters.put(fieldName, value); } else { if (!super.isValidFile(item)) { continue; } if(item.getSize()>0){ String fieldName = item.getFieldName(); files.put(fieldName, item); filesSize += item.getSize(); } } } } catch (FileUploadException e) { //e.printStackTrace(); } return files; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |