依赖jar:
commons-fileupload commons-fileupload 1.3.1
采用fileupload组件进行上传,jsp页面:
对应的控制层的代码:
protected String storeFile(HttpServletRequest request, String baseDir) throws Exception { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); Listitems = upload.parseRequest(request); Iterator itr = items.iterator(); String file = "common_data_file_"; while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (!item.isFormField()) { System.out.println("上传文件的大小:" + item.getSize()); System.out.println("上传文件的类型:" + item.getContentType()); System.out.println("上传文件的名称:" + item.getName()); file = file + item.getFieldName(); item.write(new File(baseDir + File.separator + file)); break; } } return file; }