我在spring mvc project工作。
我可以在我的本地运行我的项目而没有任何问题。我使用控制器做了一个文件上传页面。
虽然我在制作时使用了这个,但我无法得到结果并收到错误
"org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
我的代码:(我在上传屏幕中收到此用户ID)
@Resource
private UserService userService;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Long userId = userService.getLoggedInUser(request).getId();
ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadmain);
modelAndView.addObject("userId", userId);
return modelAndView;
}
上传jsp
<form method="POST" enctype="multipart/form-data" action="upload.htm">
File to upload:
<input type="file" name="file"><br />
<input type="hidden" name="userId" value="${userId}"><br /> <br />
<input type="submit" value="Upload">
</form>
FileUploadController
@Controller
public class FileUploadController {
@Resource
private CollectionsRepository collectionsRepository;
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload.htm", method=RequestMethod.POST)
public ModelAndView handleFileUpload(@RequestParam("userId") String userId, @RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
String filePath = "/home/ubuntu/analyzer/LOS/";
byte[] bytes = file.getBytes();
File newFile = new File(filePath+""+file.getOriginalFilename());
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(newFile));
stream.write(bytes);
stream.close();
List<BankStatementError> errorList = new ArrayList<BankStatementError>();
Excelsheetreader esr = new Excelsheetreader();
List<String> listaddSN = esr.GetCalculation(Long.parseLong(userId), filePath+""+file.getOriginalFilename());
newFile.delete();
for (int i = 0; i < listaddSN.size(); i++) {
String bank = listaddSN.get(i);
BankStatementError error = collectionsRepository.getErrorByBank(bank);
errorList.add(error);
}
ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadsuccess);
modelAndView.addObject("errorList", errorList);
return modelAndView;
} catch (Exception e) {
ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadexecption);
return modelAndView;
}
} else {
ModelAndView modelAndView = new ModelAndView(UploadDocView.uploadempty);
return modelAndView;
}
}
}
我认为问题是userId为null,即上传屏幕。但不确定。有人能指出我这方面的正确方法吗?