SpringMVC 后台怎么获取前台jsp页面中file中的文件
展开全部
SpringMVC默认是关闭fileupload功能的,开启该能够并验证文件上传,需要做如下几件事情:
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,
会报错org.springframework.web.multipart.MultipartException: The current
request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,
会报错org.springframework.web.multipart.MultipartException: The current
request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
展开全部
SpringMVC默认是关闭fileupload功能的,开启该能够并验证文件上传,需要做如下几件事情:
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,会报错org.springframework.web.multipart.MultipartException: The current request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
第一:打开SpringMVC的文件上传功能:
***-servlet.xml中配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型,
第二步:创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"><input type="file" name="myfile" id="myfile" value="" /><br/><input type="submit" value="确认提交"></form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,会报错org.springframework.web.multipart.MultipartException: The current request is not a multipart request。
第三步:Controller层创建代码:
@RequestMapping(params = {"method=uploadFile"})
public ModelAndView uploadFile(@RequestParam("myfile") MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response) throws Exception {
if(!myfile.isEmpty()){
logger.info(myfile.getName());
byte[] bs= myfile.getBytes() ;
logger.info(new String(bs));
}
return null;
}
部署web应用运行后,能够看到控制台中打印出上传文件的内容,BINGO搞定(SpringMVC依赖common-fileupload.jar,需要加载该jar包)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询