核心代码,在control层实现
@RequestMapping(value="upload") public String upLoad(@RequestParam(value="thefile") CommonsMultipartFile files) throws IOException{ if(!files.isEmpty()){ try { FileOutputStream os = new FileOutputStream("D:/" + files.getOriginalFilename()); System.out.println("上传的文件名 :"+files.getOriginalFilename()); //files.getOriginalFilename() 获得上传文件的名字 InputStream in = files.getInputStream(); int b = 0; while((b=in.read()) != -1){ os.write(b); } os.flush(); os.close(); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } return "/success"; }
JSP代码
同时在配置文件中需要添加MultipartResolver处理器