`
wuzijingaip
  • 浏览: 331778 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring mvc 上传 The current request is not a multipart request

 
阅读更多
spring mvc 上传 The current request is not a multipart request

加: enctype="multipart/form-data" 这个解决

<form name="uploadform" id="uploadformid" method="post" enctype="multipart/form-data"> 
			    <input data-allowblank="false" type="file" id="file" name="excelFile" />
			</form>  



后台:

	@RequestMapping(value = "/import.do", method = RequestMethod.POST)
	@ResponseBody
	public String importExcel( 
			@RequestParam(value = "excelFile",required = false) MultipartFile excelFile, 
			HttpServletRequest request
			) throws IOException, Exception{  
        String result = "" ;
        System.out.println("=======xlkhgl=======import=========");
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        MultipartFile excelFile = (MultipartFile) multipartRequest.getFileMap();
		if (null == excelFile) {  
            result = "模板文件为空,请选择文件";  
            return result;  
        }  
                  
//      String path = request.getSession().getServletContext().getRealPath("demo2");  
          
        String path = "D:\\testupload";  
        //容错处理  
        File dir = new File(path);  
        if(!dir.exists()) {  
            dir.mkdirs();  
        }  
        String fileName = excelFile.getOriginalFilename();//report.xls  
        String fileName2 = excelFile.getName();//excelFile  
          
        InputStream fis = excelFile.getInputStream();  
         
       List<Map<String,String >> list  = XlkhglExcel.parseExcel(fis);  
       
       
       for( Map<String, String> map : list ){
    	   xlkhglService.insertXlkhgl(map);
       }
       
       
       return "success";  
    }
	



	public static List<Map<String, String>> parseExcel(InputStream fis) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		;
		try {
			HSSFWorkbook book = new HSSFWorkbook(fis);
			HSSFSheet sheet = book.getSheetAt(0);
			int firstRow = sheet.getFirstRowNum();
			int lastRow = sheet.getLastRowNum();
			// 除去表头和第一行
			// ComnDao dao = SysBeans.getComnDao();
			for (int i = firstRow + 1; i < lastRow + 1; i++) {
				Map map = new HashMap();

				HSSFRow row = sheet.getRow(i);
				int firstCell = row.getFirstCellNum();
				int lastCell = row.getLastCellNum();

				for (int j = firstCell; j < lastCell; j++) {

					HSSFCell cell2 = sheet.getRow(firstRow + 1).getCell(j);
					String key = cell2.getStringCellValue();

					HSSFCell cell = row.getCell(j);

					if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					}
					String val = cell.getStringCellValue();

					System.out.println("====parseExcel val:"+val);

					if (i == firstRow + 1) {
						break;
					} else {
						map.put(key, val);

					}
					System.out.println("====parseExcel val:"+ map);
				}
				if (i != firstRow + 1) {
					data.add(map);
					System.out.println(map);
				}
			}
			System.out.println("==========" + data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
分享到:
评论
Global site tag (gtag.js) - Google Analytics