1、JSP页面代码
<form enctype=""multipart/form-data" method="post"> <input type="file" name="file"> <input type="submit" value="上传" > </form>
2、controller中代码
@RequestMapping("/sysfindheimingdan/excelmobanupload") @ResponseBody public void uploadExcelModel(HttpServletRequest request,HttpServletResponse response) throws Exception { //开始上传excel MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("file"); String sourceName = multipartFile.getOriginalFilename(); // 原始文件名 Long size = multipartFile.getSize(); String fileType = sourceName.substring(sourceName.lastIndexOf(".")); System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType+"大小:"+size); String base = request.getSession().getServletContext().getRealPath("/upload//"); //获取文件上传的路径,在webapp下的upload中 File file = new File(base); if(!file.exists()){ file.mkdirs(); } String path=base + File.separator + sourceName; multipartFile.transferTo(new File(path)); }
时间: 2024-11-08 00:26:20