1. 前端
enctype="multipart/form-data"必须加
input类型时type
<form id="plan-add-edit-form" action="" method="post" enctype="multipart/form-data">
<input type="file" id="planFileUpload" name="planFileUpload" >
</form>
2.controller
@RequestMapping(value = "/insertPlan", method = RequestMethod.POST)
public void insertPlan(MultipartHttpServletRequest request, HttpServletResponse response
, @RequestParam(value="planFileUpload",required = false) MultipartFile planFileUpload}
使用planFileUpload表示上传的文件
创建文件
File serverFile = new File(fileSrc);
if (!serverFile.exists()) {
try {
serverFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
写入文件
planFileUpload.transferTo(serverFile);