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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值