文件上传

1. 页面

<form name="checkForm" id="checkForm" action="/file/uploadFile" method="post" enctype="multipart/form-data" class="newWorkForm">
  <label class="labelLeft"><span class="required">*</span>请选择文件:</label>
  <div class="labelRight">
   <input type="file" class="inputText" name="file" id="file">
   <span style="color: red;">$!msg</span>
  </div>
  <div class="labelLeft">
   <input type="button" class="centerBtn subBtn" onclick="submittheform();" value="上传">
  </div>
</from>

2. 上传操作

@RequestMapping("/uploadFile")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file,
ModelMap model,String pids) {
LoginUser loginUser= LoginUtil.getLoginUser();
String fileName = file.getOriginalFilename();
String path = WebConstants.FILE.UPLOAD_PATH+loginUser.getNuId()+"/";
File targetFile = new File(path , fileName);
try {
List<Map<String,String>> productList=productService.getProductByLogin(loginUser);
model.addAttribute("productList",productList);
if (targetFile.exists()){
model.addAttribute("msg","请不要重复上传,或修改文件名称继续上传。");
return ("/file/uploadFile");
}
if (!(new File(path).isDirectory())) {
new File(path).mkdirs();
}
//保存
file.transferTo(targetFile);
if (handleFileService.checkDate(targetFile,pids)){
targetFile.delete();
model.addAttribute("msg","文件中数据校验失败,请重新检查数据。");
return ("/file/uploadFile");
}
log.info("file upload success! path="+path+fileName);
handleFileService.insert(fileName,pids,loginUser);
} catch (Exception e) {
targetFile.delete();
log.error("file.upload error", e);
model.addAttribute("msg", "上传文件失败,请重试。");
return ("/file/uploadFile");
}
model.addAttribute("msg", "上传成功");
return ("/file/uploadFile");
}


 

posted @ 2017-04-25 11:03  小妮儿玩博客  阅读(153)  评论(0编辑  收藏  举报