首先把文件上传至服务器,可以直接获得输入流。
1、需要在配置文件里加入:
[code]<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>204800000</value>
</property>
</bean>[/code]
2、提交页面。注意要加上encType="multipart/form-data"
[code]
<form id="form1" name="form1" action="<c:url value='/login.do'/>" method="post" encType="multipart/form-data">
<input type="file" name="file1"/>
<input type="submit" value="提交"/>
</form>
[/code]
3、控制器中:
[code]protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
InputStream in = multipartRequest.getFile("file1").getInputStream();
}[/code]
4、处理Excel文件,我用到jxl.jar包[url]http://sourceforge.net/project/showfiles.php?group_id=79926[/url]。可以用3中的in获得工作簿。
[code]Workbook ws = Workbook.getWorkbook(in);[/code]
1、需要在配置文件里加入:
[code]<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>204800000</value>
</property>
</bean>[/code]
2、提交页面。注意要加上encType="multipart/form-data"
[code]
<form id="form1" name="form1" action="<c:url value='/login.do'/>" method="post" encType="multipart/form-data">
<input type="file" name="file1"/>
<input type="submit" value="提交"/>
</form>
[/code]
3、控制器中:
[code]protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
InputStream in = multipartRequest.getFile("file1").getInputStream();
}[/code]
4、处理Excel文件,我用到jxl.jar包[url]http://sourceforge.net/project/showfiles.php?group_id=79926[/url]。可以用3中的in获得工作簿。
[code]Workbook ws = Workbook.getWorkbook(in);[/code]