HttpServletRequest获取参数和文件
从HttpServletRequest中获取上传文件及参数的方法
try { //下面这句必须加,不然报错 MultipartResolver resolver = new CommonsMultipartResolver(hsq.getSession().getServletContext()); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) hsq; //获取一起跟文件传过来的其他参数值 String id = multipartRequest.getParameter("id"); //获取上传上来的文件 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) { MultipartFile file = entry.getValue(); String fileName = file.getOriginalFilename(); System.out.println(fileName); String path = "C:\\Users\\user\\Desktop\\" + fileName; file.transferTo(new File(path)); } } catch (Exception e) { e.printStackTrace(); }
转载自: