public class FileUploadManager { /** 允许上传的扩展名*/ public static String [] extensionPermit = {"txt", "xls", "zip"}; public FileUploadManager(User user, HttpServletRequest request) throws Exception { super(); session = request.getSession(); DiskFileItemFactory factory = new DiskFileItemFactory(); upload = new ServletFileUpload(factory); SystemManager systemManager = ComponentFactory.getSystemManager(); int docSize = Integer.parseInt(systemManager.getSystemConfig(user.getCompanyID(), "DOCUMENT_SIZE_LIMIT").getValue()); int maxFileSize = 1024 * 1024 * docSize; upload.setFileSizeMax(maxFileSize); // 设置单个文件的最大上传大小 upload.setSizeMax(maxFileSize + 500); // 设置整个request的最大大小 upload.setProgressListener(new FileUploadListener(request)); extensionPermit = systemManager.getSystemConfig(user.getCompanyID(), "DOCUMENT_EXT_LIMIT").getValue().split(",");//设置文件类型 files = upload.parseRequest(request); } class QCServletFileUpload extends ServletFileUpload { private Map textHash = new HashMap();//表单数据 private int fileItemCount = 0; // 重载parseRequest方法 public List parseRequest(HttpServletRequest request) throws FileUploadException { List items = new ArrayList();//文件 try { FileItemIterator iter = getItemIterator(request); FileItemFactory fac = getFileItemFactory(); while (iter.hasNext()) { FileItemStream item = iter.next(); FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName()); Streams.copy(item.openStream(), fileItem.getOutputStream(), true); //fileItem.getOutputStream().close(); if (fileItem.isFormField()) { // 处理form field textHash.put(fileItem.getFieldName(), fileItem); } else if (fileItem.getName() != null && fileItem.getName().length() > 0) {// 处理file field String fileExtension = FilenameUtils.getExtension(fileItem.getName()); if(!ArrayUtils.contains(FileUploadManager.extensionPermit, fileExtension)){ fileItemCount++; items.add(fileItem); }else{ throw new FileUploadException("文件" + fileItem.getName() + "类型不支持."); } } } } catch (Exception e) { e.printStackTrace(); throw new FileUploadException(e.getMessage(), e); } return items; } }