用Springmvc传文件
来源:互联网 发布:知乎怎么提问 编辑:程序博客网 时间:2024/06/14 19:35
今天需要用到文件上传,用的是spring框架。在网上搜了好久没找到合适的代码。经过师傅的指点5分钟搞定。觉得挺简单的!
1.首先在applicationContext.xml文件中配置文件上传解决器
- <!-- 文件上传拦截 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- </bean>
2.上传的jsp页面、很简单
- <form action="/gouwuche3/goods/add.htm" method="post" enctype="multipart/form-data" >
- <table align="center" border="1">
- <tr>
- <td colspan="2">商品信息</td>
- </tr>
- <tr>
- <td>商品名字</td><td><input name="goodsName" id="goodsName" type="text" /></td>
- </tr>
- <tr>
- <td>商品价格</td><td><input name="goodsPrice" id="goodsPrice" type="text" /></td>
- </tr>
- <tr>
- <td>商品描述</td><td><input name="goodsDes" id="goodsDes" type="text" /></td>
- </tr>
- <tr>
- <span style="color:#ff6666;"> <td>上传照片</td><td><input type="file" name="file" /></td>
- </span> </tr>
- <tr><td colspan="2"><input type="submit" value="确认" /></td></tr>
- </table>
- </form>
注意红色的代码,是上传文件用的。
3.在controller中执行文件上传功能。
- @RequestMapping(value = "/add")
- /MultipartFile来自:org.springframework.web.multipart.MultipartFile;
- public ModelAndView addGoods( HttpServletRequest request, HttpSession session,
- <span style="color:#ff0000;"> @RequestParam("file") MultipartFile file</span>) {
- ModelAndView mav = new ModelAndView();
- if (!file.isEmpty()) {
- String path = request.getContextPath() + "/jsp/";
- String fileName = file.getOriginalFilename();
- try {
- String tomcatPath = request.getServletContext().getRealPath("/image//"); //得到保存的路径
- FileCopyUtils.copy(file.getBytes(), new File(tomcatPath +"/" + fileName));//FileCopyUtils来自org.springframework.util.FileCopyUtils
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- mav.setViewName("test");
- return mav;
- }
这就完事了。。。
0 0
- 用Springmvc传文件
- springMVC 用query.ajax文件上传
- springmvc 用common-fileupload 上传文件
- 用plupload+springMVC实现文件的上传
- 用SpringMVC搭建文件上传和下载
- SpringMVC 中 .classpath文件有什么用?
- springMVC(11) springMVC文件上传
- SpringMVC-----8、SpringMVC上传文件
- SpringMVC:用MultipartFile上传单个文件,多个文件
- springmvc文件上传总结
- SpringMVC multipart文件上传
- SpringMVC中的文件上传
- springmvc的文件上传
- SpringMVC—文件下载
- springMVC上传文件
- springMVC 文件上传
- SpringMVC - 文件上传篇
- springMVC文件上传
- 近况工作总结
- Jedis connection refused
- hdu 4901 The Romantic Hero (dp)
- Postgresql function
- 使用Python脚本编写网络爬出程序,来获取Web数据的方法实现(1)——流程图
- 用Springmvc传文件
- 利用chrome自带工具分析http包
- MVC中实现加载更多
- UVA 12186 Another Crisis(树形DP)
- HDOJ 1166 -----线段树
- jquery学习笔记
- 拿来主义:分享给大家,关于SQL中数据库设置,约束、外键、主键等
- 输入框的有用的特效
- c++中带空格路劲的处理