文件上传在 spring mvc 和 tomcat6 中不起作用
File upload not works in spring mvc and tomcat6
我在 spring mvc 和 tomcat6 中工作。我尝试上传文件并能够使用 tomcat8 进行 war 部署。
但是我在使用相同的 war 部署 tomcat6 时遇到以下错误。
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
我的完整代码如下,
在 pom 中添加了 jar
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
在 ApplicationContext.xml
中添加了用于上传的 bean
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
</bean>
控制器return userId
public class UploadDocController extends AbstractController {
@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.java
@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(value ="userId") String userId,
@RequestParam(value ="file") MultipartFile file){
System.out.println("userId--------"+userId);
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;
}
}
}
我收到的错误是
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
有没有其他方法可以使用 tomcat6 来做到这一点?希望有人在这方面提出建议。
提前致谢..
截图..
当你调用你的 jsp 文件时,你的响应没有附加参数。
见-
<input type="hidden" name="userId" value="${userId}"><br /> <br />
您在此处尝试访问 userId 的值,但您正在查找的参数可能不可用。
因此可能在响应中添加参数将消除错误。
我在 spring mvc 和 tomcat6 中工作。我尝试上传文件并能够使用 tomcat8 进行 war 部署。
但是我在使用相同的 war 部署 tomcat6 时遇到以下错误。
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
我的完整代码如下,
在 pom 中添加了 jar
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Apache Commons Upload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
在 ApplicationContext.xml
中添加了用于上传的 bean <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
</bean>
控制器return userId
public class UploadDocController extends AbstractController {
@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.java
@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(value ="userId") String userId,
@RequestParam(value ="file") MultipartFile file){
System.out.println("userId--------"+userId);
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;
}
}
}
我收到的错误是
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
有没有其他方法可以使用 tomcat6 来做到这一点?希望有人在这方面提出建议。
提前致谢..
截图..
当你调用你的 jsp 文件时,你的响应没有附加参数。
见-
<input type="hidden" name="userId" value="${userId}"><br /> <br />
您在此处尝试访问 userId 的值,但您正在查找的参数可能不可用。
因此可能在响应中添加参数将消除错误。